Jump to content

[SOLVED] a simple loop question


cluce

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/68389-solved-a-simple-loop-question/
Share on other sites

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>";
}


?>

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.

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???

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.