Forum Moderators: coopster

Message Too Old, No Replies

Loading query results into an array

         

jonnym

4:05 pm on Jun 26, 2003 (gmt 0)

10+ Year Member



I have a products table that is set up kind of like this...

id ¦ item_name ¦ item_no ¦ item_size ¦ price
23 ¦ blue shirt ¦ 1001 ¦ small ¦ $10
24 ¦ blue shirt ¦ 1001 ¦ med ¦ $11
25 ¦ blue shirt ¦ 1001 ¦ large ¦ $12
26 ¦ red shirt ¦ 1002 ¦ small ¦ $10
27 ¦ red shirt ¦ 1002 ¦ med ¦ $11
28 ¦ red shirt ¦ 1002 ¦ large ¦ $12

'id' is an auto increment column.

I want to display the data like this...

Blue shirt 1001
- small $10
- med $11
- large $12

Red shirt 1002
- small $10
- med $11
- large $12

How do I load an array and then pull out the data from the array to display it like this?

jatar_k

4:29 pm on Jun 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld jonnym,

if you use
$q = "select * from products";
unless you need to reuse the data later on I would output it right away.

$query = mysql_query($q);
$lastprod = "";
while ($row = mysql_fetch_array($query)) {
if ($row['item_name']!= $lastprod) {
echo "<p>",$row['item_name']," ",$row['item_no'];
}
echo "<br>- ",$row['item_size']," ",$row['price'];
$lastprod = $row['item_name'];
}

I think that should work though I didn't test it at all.

jonnym

7:36 pm on Jun 26, 2003 (gmt 0)

10+ Year Member



Hey! That worked! I was pulling my hair out trying to figure that out. Thanks!