Reputation: 255
<?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
Upvotes: 1
Views: 81
Reputation: 141839
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)
Upvotes: 0
Reputation: 1537
You need to make sure the index exists before echoing it, as so:
while($j<=$i)
{
if (isset($dish[$j])) echo $dish[$j];
$j++;
}
Upvotes: 0
Reputation: 81988
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;
}
Upvotes: 2