0

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";
4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by nish123
0

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

0

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

0

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

0

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..!! :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.