cluce Posted September 7, 2007 Share Posted September 7, 2007 I have a table with a number of records and I am trying to display them in a table on my web page. Most of my code works except it only displays the first record in the database. Can someone tell me how to get me loop to work properly? I know it has to do something with a counter to loop through the right amount of times but loops are my weakness in php prgramming. Here is my code... <?php //connect to database include'db.php'; //get all data from table $sql = "SELECT * from products"; $result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli)); //if authorized, get the values while ($info = mysqli_fetch_array($result)) { $ItemNo = stripslashes($info['Item_No']); $Man = stripslashes($info['Manufacturer']); $Cat = stripslashes($info['Category']); $Des = stripslashes($info['Description']); $Model = stripslashes($info['Model']); $Qty = stripslashes($info['Qty']); $Kw = stripslashes($info['Kw']); $Hours = stripslashes($info['Hours']); $Price = stripslashes($info['Price']); //create display string $display_block = " <table border='1' cellspacing='0' cellpadding='0'> <tr> <td>".$ItemNo."</td> <td>".$Man."</td> <td>".$Cat."</td> <td>".$Des."</td> <td>".$Model."</td> <td>".$Qty."</td> <td>".$Kw."</td> <td>".$Hours."</td> <td>".$Price."</td> </tr> </table>"; } //displays string with data echo "<p align=\"center\">"; echo "$display_block"; echo "</p>"; ?> thanks in advance Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted September 7, 2007 Share Posted September 7, 2007 You just need to put the $display_block inside the loop: <?php //connect to database include'db.php'; //get all data from table $sql = "SELECT * from products"; $result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli)); //if authorized, get the values while ($info = mysqli_fetch_array($result)) { $ItemNo = stripslashes($info['Item_No']); $Man = stripslashes($info['Manufacturer']); $Cat = stripslashes($info['Category']); $Des = stripslashes($info['Description']); $Model = stripslashes($info['Model']); $Qty = stripslashes($info['Qty']); $Kw = stripslashes($info['Kw']); $Hours = stripslashes($info['Hours']); $Price = stripslashes($info['Price']); //create display string $display_block = " <table border='1' cellspacing='0' cellpadding='0'> <tr> <td>".$ItemNo."</td> <td>".$Man."</td> <td>".$Cat."</td> <td>".$Des."</td> <td>".$Model."</td> <td>".$Qty."</td> <td>".$Kw."</td> <td>".$Hours."</td> <td>".$Price."</td> </tr> </table>"; //displays string with data echo "<p align=\"center\">"; echo "$display_block"; echo "</p>"; } ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 7, 2007 Share Posted September 7, 2007 Just a bit of info for you, you don't need to assign all those variable you could just say something like <?php echo $row['Var1']."<br/>".$row['Var2']; ?> In the while loop because you are rewriting the variables in each iteration of the while loop it is pointless to store them there unless you have some goal to use them before they are rewritten. Quote Link to comment Share on other sites More sharing options...
cluce Posted September 7, 2007 Author Share Posted September 7, 2007 Just a bit of info for you, you don't need to assign all those variable you could just say something like <?php echo $row['Var1']."<br/>".$row['Var2']; ?> In the while loop because you are rewriting the variables in each iteration of the while loop it is pointless to store them there unless you have some goal to use them before they are rewritten. Actually, I do have a goal which I am not sure how I am going to do it yet but I have some ideas. My goal is to have the table more user friendly. At the top of the table I will have my heading row such as manufacture, category, price, etc. and I want to allow the user to click on each heading and this will execute a query to sort the rows in that table by that heading in that colunm. I am just not sure how to do this on one page without having multiple webpages??? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.