mpavlovic89
mpavlovic89

Reputation: 769

Undefined offset notice because of array definition in php

I try to define an array, but although having read all of the suggestion here, I still got an error notice when execute.

My form consist among everything else, field min which define the number of textareas in

<input type="text" name="min[1]">

My PHP file has:

<?php
$min = $_POST['min'];
$area = array();

for($j=1; $j=<$length; $j++) {
if($_POST['row'][$j] != "") {

    if(($_POST['min'][$j])!="") {
    for($k=1;$k<=$min[$j];$k++) {
    $area[$j] .= '<textarea name="label'.$j.$k.'" rows="3"' ></textarea>';
    }}
    if(($_POST['min'][$j])=="") {
    $area[$j] = '<textarea name="label'.$j.$k.'" rows="3"'" ></textarea>';
    }
    $blah .= $j.') '.$row[$j].'<br/>'.$area[$j].'<div id="inner'.$j.'"></div><br/><br/>';
}}

I can see that the problem is in array, because the script try to locate the keys which are not there. So how to defne this in advance... This works but it's no solution as you can se:

$area = array("","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",);

Upvotes: 1

Views: 110

Answers (2)

user457015
user457015

Reputation: 981

$length is not initialized...

In PHP the first array index is 0.

You could iterate through array with the foreach operator.

If you copy $_POST['min'] in $min , use it. stop using $_POST['min'] it's confusing.

Upvotes: 0

John Conde
John Conde

Reputation: 219824

You need to check if that array element exists before you check its value.

Change

if(($_POST['min'][$j])!="") {

to

if(isset($_POST['min'][$j]) && $_POST['min'][$j])!="") {

Upvotes: 1

Related Questions