-1

The issue im having is when running a while loop inside of a while loop in php the html content after the second while loop is not being rendered

for a live demonstration http://naturalflame.altervista.org/home.php

I originally didnt use a continue after the second loop but then none of the other rows populate because it just stops after the second loop after adding the continue after the second loop the other rows populate but the description and action field are left out

<div id="main">
        <h1>Product List</h1> 
        <table> 
            <tr> 
                <th>Size</th>  
                <th>Price</th> 
                <th>Flavor Selection</th>
                <th>Flavor Description</th>
                <th>Blend Selection</th>
                <th>Action</th> 
            </tr> 
            <?
                $sql="SELECT * FROM products ORDER BY price ASC"; 
                $query=mysql_query($sql) or die(mysql_error());

                while($row = mysql_fetch_object($query)){
                    $id = $row->id;
                    $size = $row->size;
                    $price = $row->price;
                ?>
                <tr> 
                    <td><? echo $size; ?></td> 
                    <td>$<? echo $price; ?></td> 
                    <td>
                        <select onchange="ChangeDescription(<? echo $id; ?>)" id="dSelection<? echo $id; ?>">
                        <option value="Select a flavor">Select a flavor</option>
                        <?
                            $sql2 = "SELECT * FROM flavors ORDER BY id ASC";
                            $query2 = mysql_query($sql2) or die(mysql_error());

                            while($row2 = mysql_fetch_object($query2)){
                                $name = $row2->name;
                                echo "
                                <option value='$name'>$name</option>
                                ";
                            }
                        ?>
                        </select>
                    </td>
                    <td id="description<? echo $id; ?>">Flavor Description</td>
                    <td><a href="home.php?page=products&action=add&id=<?php echo $row['id'] ?>&flavor=">Add to cart</a></td>
                </tr>
            <?}?>
        </table>
        </div><!--end main-->
FlamingGenius
  • 216
  • 3
  • 22
  • remove `continue;` and check – Alive to die - Anant Dec 18 '17 at 18:44
  • Well, `mysql_` functions are deprecated and you [shouldn't be using them](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – FirstOne Dec 18 '17 at 18:44
  • removed continue now im back to the original problem none of the other rows load – FlamingGenius Dec 18 '17 at 18:47
  • Fatal error: Cannot use object of type stdClass as array in /membri/naturalflame/home.php on line 132
    This is in your source in the page.
    – Adam Forbis Dec 18 '17 at 18:47
  • `echo $row['id']` should be `echo $row->id` – Nigel Ren Dec 18 '17 at 18:51
  • your loading an older version of the site – FlamingGenius Dec 18 '17 at 18:51
  • 1
    You really need better organization and variable names. Do both queries up top, without nesting the loops, and put the results into variables with descriptive names. Then in the output blocks, you can loop over the data without intermingling the queries. You are repeating a sql query in a loop which is almost *always* a problem. – 000 Dec 18 '17 at 18:55
  • @JoeFrambach specially since the inner query doesn't change (same results). – FirstOne Dec 18 '17 at 19:00

1 Answers1

2

You seem to be using an object as an array causing an error on your page.

This code: $row['id'] is probably just supposed to be $id

If you hadn't of already put it in a placeholder it would be $row->id

Adam Forbis
  • 461
  • 3
  • 11