6
if($numrows>0)
{
    $i=0;
    while($i<count($result_page[$i]))         //This is line 68
    {
        echo "<tr>";
        echo "<td>".$result_page[$i]['product_id']."</td>";
        echo "<td>".$result_page[$i]['product_name']."</td>";
        echo "<td>".$result_page[$i]['product_price']."</td>";
        $i++;
    }
}

This is the notice:

Notice: Undefined offset: 10 in /home/jatin/web/www.exam.com/admin/productlist.php on line 68.

I am getting this notice because when the loop will be executed for the last time then $i will be incremented and it goes out of the length of the array.

Each time the number of elements in the 2nd dimension changes thus I have to use count function.

The notice occurs when the condition is checked for the last time, So all my elements are displayed but the notice occurs.

Please give an appropriate solution.

Yash M. Hanj
  • 495
  • 1
  • 3
  • 15

5 Answers5

4

well i found one solution myself also which is not a good option but it also works fine...........use the @ operator like given below ---

while($i<count(@$result_page[$i]))

Answers given by B.Desai and thavaamm are better options though.

Yash M. Hanj
  • 495
  • 1
  • 3
  • 15
2

You can also use the following function to suppress the warnings.

error_reporting(E_ERROR | E_PARSE);
Gamer
  • 21
  • 3
1

You can check whether the array key is set or not then continue loop

if($numrows>0)
{
    $i=0;
    while(isset($result_page[$i]) && $i<count($result_page[$i])         //This is line 68
    {
        echo "<tr>";
        echo "<td>".$result_page[$i]['product_id']."</td>";
        echo "<td>".$result_page[$i]['product_name']."</td>";
        echo "<td>".$result_page[$i]['product_price']."</td>";
    }
}
B. Desai
  • 16,414
  • 5
  • 26
  • 47
1

Can you try below one.

if($numrows>0)
{
    foreach($result_page as $row)         
    {
        echo "<tr>";
        echo "<td>".$row['product_id']."</td>";
        echo "<td>".$row['product_name']."</td>";
        echo "<td>".$row['product_price']."</td>";
    }
}
Thavaprakash Swaminathan
  • 6,226
  • 2
  • 30
  • 31
  • well i found one solution myself also which is not a good option but it also works fine...........use the @ operator like this `while($i – Yash M. Hanj Apr 19 '18 at 11:38
1

Using the @ operator will suppress the notice. If only that is what you are looking for. Or you can also use this:

while(isset($result_page[$i]) && $i<count($result_page[$i])