1
<?php
include 'db.php';
$i=0;
$result15=mysql_query("select c.dishes from c");
while($row=mysql_fetch_array($result15))
{
if($row['dishes']!=NULL)
{
$dish[$i]=$row['dishes'];
$i++;
}

 }
  mysql_close();
  $j=0;
  while($j<=$i)
 {
echo $dish[$j];
$j++;
}
?>

Getting notice: Undefined offset: 2 in F:\xampp\htdocs....on line 18

3 Answers 3

2

You mean while($j<$i) there.

Remember, you incremented $i after the last insert. This means that $i will be higher than the maximum key of $dish.

Some thoughts:

Any time you're testing for equality with null, you should consider using is_null (or !is_null). It is more accurate.

This:

$dish[$i]=$row['dishes'];
$i++;

Would be better as:

// obviously instead of $i you would use count($dish) later (or use foreach)
$dish[]=$row['dishes']; 

That final while loop would be better as a foreach:

foreach($dish as $val)
{
    echo $val;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to make sure the index exists before echoing it, as so:

 while($j<=$i)
 {
     if (isset($dish[$j])) echo $dish[$j];
     $j++;
 }

Comments

0

Your second while loop is going to far. You only define dishes up to i-1 and then you loop through it up to and including i. Change it to:

while($j<$i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.