Stan Howe
Stan Howe

Reputation: 116

while loop only returning 1 product row

Evening all,

I have the following script setup on the backend of an eCommerce platform I am developing. Essentially I want to pull the products through and also pull all of the current categories through so that products can be assigned to a particular category.

However my product edit page is only displaying 1 result.

Its interesting to note that if i remove the script relating to the last SQL query my products show but with no category options. With the script only 1 product shows with the correct categories.

<?php
$myusername= $_SESSION['login_user'];

include '../ecommerce/connection.php';

        $sql="SELECT * FROM products where status='yes' ";

        // Posting Result
        $result = mysqli_query($connection, $sql);

        // Counting Results 
        $count=mysqli_num_rows($result);

            if($count==0) {

                echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No products setup</h1></div>";

            } else {

                    if ($result = mysqli_query($connection, $sql)){

                          echo "<table id='order_table_small'>";                        
                            echo "<th>Image</th>";
                            echo "<th>Product ID</th>";
                            echo "<th>Name</th>";
                            echo "<th>Description</th>";
                            echo "<th>Price</th>";  
                            echo "<th>Weight</th>"; 
                            echo "<th>Options </th>";                                   


                            while($row = $result->fetch_array())

                                     {

                                     $product_no = $row['product_id'];

                                            echo "<tr>";

                                            echo '<td><img src="images/'. $row['image'] . ' " id="product_image_admin">';
                                            echo "</td> ";  

                                            echo "<td>" .$row['product_id'];
                                            echo "</td> ";                                  

                                            echo "<td>" .$row['product_name'];
                                            echo "</td> ";  

                                            echo "<td>" .$row['product_description'];
                                            echo "</td> ";

                                            echo "<td>" .'&pound'.$row['product_price'];
                                            echo "</td> ";  

                                            echo "<td>" .$row['product_weight'].'kg';
                                            echo "</td> ";

                                            echo "<td><form action='store_configuration/edit_product' method='post' id='delivery_change_form'>";
                                            echo "<input type='text' name='product' value='$product_no' style='opacity: 0;'/>";
                                            echo "<input type='Submit' value='Edit Product' >"; 
                                            echo "</form></td>";



                                            echo "<td><form action='store_configuration/change_parent_category' method='post' id='delivery_change_form'>";

                                            echo "Parent Category <select name='category' style='height: auto;' >"; 

                                            **include '../ecommerce/connection.php';

                                            $sql="SELECT category_name FROM categories where status='yes' ";

                                            // Posting Result
                                            $result = mysqli_query($connection, $sql);

                                            // Counting Results 
                                            $count=mysqli_num_rows($result);

                                                if($count==0) {

                                                    echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No categories setup</h1></div>";

                                                } else {

                                                if ($result = mysqli_query($connection, $sql)){

                                                    while($row = $result->fetch_array()) {

                                                        $category = $row['category_name'];

                                                        echo" <option value='$category' >".$category ."</option>";

                                                    }


                                                }                               


                                            }**                                                                                                             


                                            echo "</select>";
                                            echo "<br><br><input type='Submit' value='Update Category' >";                              
                                            echo "</form></td>";

                                            echo "</tr>";


                                      }

                                        echo "</table>";                        

                            } 

            }

?>

enter image description here

I would love any help on how I can show all products with all categories.

I have tried everything I can think of.

Thanks. Stan.

Upvotes: 0

Views: 122

Answers (1)

Maciej Wloch
Maciej Wloch

Reputation: 36

You've used the same names of variables for products and categories queries. While the categories are inside of products loop you reassign that variable with new query and that's why it's crashes. Change name of variables for second query and that should fix your script.

<?php
$myusername= $_SESSION['login_user'];

include '../ecommerce/connection.php';

        $sql="SELECT * FROM products where status='yes' ";

        // Posting Result
        $result = mysqli_query($connection, $sql);

        // Counting Results 
        $count=mysqli_num_rows($result);

            if($count==0) {

                echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No products setup</h1></div>";

            } else {

                    if ($result = mysqli_query($connection, $sql)){

                          echo "<table id='order_table_small'>";                        
                            echo "<th>Image</th>";
                            echo "<th>Product ID</th>";
                            echo "<th>Name</th>";
                            echo "<th>Description</th>";
                            echo "<th>Price</th>";  
                            echo "<th>Weight</th>"; 
                            echo "<th>Options </th>";                                   


                            while($row = $result->fetch_array())

                                     {

                                     $product_no = $row['product_id'];

                                            echo "<tr>";

                                            echo '<td><img src="images/'. $row['image'] . ' " id="product_image_admin">';
                                            echo "</td> ";  

                                            echo "<td>" .$row['product_id'];
                                            echo "</td> ";                                  

                                            echo "<td>" .$row['product_name'];
                                            echo "</td> ";  

                                            echo "<td>" .$row['product_description'];
                                            echo "</td> ";

                                            echo "<td>" .'&pound'.$row['product_price'];
                                            echo "</td> ";  

                                            echo "<td>" .$row['product_weight'].'kg';
                                            echo "</td> ";

                                            echo "<td><form action='store_configuration/edit_product' method='post' id='delivery_change_form'>";
                                            echo "<input type='text' name='product' value='$product_no' style='opacity: 0;'/>";
                                            echo "<input type='Submit' value='Edit Product' >"; 
                                            echo "</form></td>";



                                            echo "<td><form action='store_configuration/change_parent_category' method='post' id='delivery_change_form'>";

                                            echo "Parent Category <select name='category' style='height: auto;' >"; 

                                            **include '../ecommerce/connection.php';

                                            $sql2="SELECT category_name FROM categories where status='yes' ";

                                            // Posting Result
                                            $result2 = mysqli_query($connection, $sql2);

                                            // Counting Results 
                                            $count=mysqli_num_rows($result2);

                                                if($count==0) {

                                                    echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No categories setup</h1></div>";

                                                } else {

                                                if ($result2 = mysqli_query($connection, $sql2)){

                                                    while($row2 = $result2->fetch_array()) {

                                                        $category = $row2['category_name'];

                                                        echo" <option value='$category' >".$category ."</option>";

                                                    }


                                                }                               


                                            }**                                                                                                             


                                            echo "</select>";
                                            echo "<br><br><input type='Submit' value='Update Category' >";                              
                                            echo "</form></td>";

                                            echo "</tr>";


                                      }

                                        echo "</table>";                        

                            } 

            }
?>

Upvotes: 2

Related Questions