Hi, I need a little help. This might be something simple but hard for me. I am trying to list all the name of the product which is $description and the $quantity neatly by using a table.

My problem is that it is only showing one product information instead of all the product $description and $quantity that are in the database. This is the code I tried below. I thought to be smart and put a few <td> tags to see what will happen but it only repeated the same products.
Please help

echo "<h2>Current Items and Quantities:</h2>\n";
      echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n"; 
	   $query = "SELECT description, quantity from products";
      $result = mysql_query($query);
   $row = mysql_fetch_array($result, MYSQL_ASSOC);
	    $description = $row['description'];
   $quantity = $row['quantity']; 
	  
	  
	 echo "<tr><td>$description</td><td>$quantity</td></tr>\n";
			  
      echo "</table>\n";

Hey try it this way

echo "<h2>Current Items and Quantities:</h2>\n";
echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n";
$query = "SELECT description, quantity from products";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
       $description = $row['description'];
       $quantity = $row['quantity'];
         echo "<tr><td>$description</td><td>$quantity</td><tr>\n";
  
}
echo "</table>\n";

Then you can format the output however you want

Hi there, give this a go:

echo "<h2>Current Items and Quantities:</h2>\n";
      echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n"; 
	   $query = "SELECT description, quantity from products";
      $result = mysql_query($query);
   while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
 {
	    $description = $row['description'];
            $quantity = $row['quantity']; 
	    echo "<tr><td>$description</td><td>$quantity</td></tr>\n";
}		  
      echo "</table>\n";

This construct will loop through all the result sets return by the SQL and stop automatically after it has read the last one.

Good luck

Thank you "both". It works. I can see everything from my database without continue going into it. I was thinking about looping but when I tried it gave me errors because I echo my <tr> on the top before the while statement. Now I know do it at the end.

Something I wont forget.. Thank you both for your time and helping this newby to php out.

Still tryign to get the hang of it,
without hanging myself first
vanessia

No worries. Thats why these forums are here.I started with them too

Thank you "both". It works. I can see everything from my database without continue going into it. I was thinking about looping but when I tried it gave me errors because I echo my <tr> on the top before the while statement. Now I know do it at the end.

Something I wont forget.. Thank you both for your time and helping this newby to php out.

Still tryign to get the hang of it,
without hanging myself first
vanessia

Hello Vanessia

<h2>Current Items and Quantities:</h2>
 <table width="100%" cellpadding="1" border="1">
<tr>
<? $query = "SELECT description, quantity from products";
$result = mysql_query($query) or die("query error".mysql_error());

while ($row = mysql_fetch_array($result)
{ ?>
<td> <? echo $row['description'] ?></td>
<td><? echo $row['quantity']</td> ?> </td>
}?>
</tr>
</table>

Use Php code where you need it..!!! this will decrease your code and make it more clean..!!!

Thank u..!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.