PDA

View Full Version : please help!!! problem displaying data



busby
07 Nov 2010, 02:20 PM
ok so im sure this is only a small problem but still here it is:

im making a shopping list app where users can create a list...when they view the list they can populate it with categories such as frozen food, fruit, veg etc etc...they can then populate categories with items such as apples, potatoes or ice cream etc etc.

now i have some data in the database already...and i wanted to display it on the page like this.


ASDA SHOPPING LIST

fruit
apples
bananas
plums

veg
potatoes
carrots

frozen
burgers
chips
ice cream

however at the moment with my code it displays like this:

ASDA SHOPPING LIST

fruit
apples
bananas
plums
potatoes
carrots
burgers
chips
ice cream

veg

frozen


here is my code:


include_once("config_class.php");

$db = new db(); // open up the database object
$db->connect(); // connect to the database

//getting id of the data from url
$id = $_GET['id'];
$sql=mysql_query("SELECT listname FROM list WHERE listid=$id") or die("cannot select: ".mysql_error());

$sql2=mysql_query("SELECT catid, category FROM cat WHERE listid=$id") or die("cannot select: ".mysql_error());

$sql3=mysql_query("SELECT items.itemname, items.itemid, cat.catid FROM items, cat WHERE cat.catid=items.catid") or die("cannot select: ".mysql_error());

$temp_cat = "";

$res=mysql_fetch_array($sql);
echo "<b>" . $res['listname'] . "</b>" . "<br><br>";
echo "<form action='addcat.php?id=$id' method='post'>";
echo "<input type='text' id='addcat' name='addcat'>";
echo "<input type='submit' value='Add Category'>";
echo "</form>";

while($res2=mysql_fetch_array($sql2))
{
echo "<table cellpadding='2' cellspacing='2' width='800'>";
echo "<tr>";



if($res2['category'] != $temp_cat )
{
echo "<td width='20%'>";
echo "<b>" . $res2['category'] . "</b>" . "</td>";
echo "<td width='20%'><a href='delcat.php?id=$res2[catid]&id2=$id'>Delete Category</a></td>";
echo "<form action='additem.php?id=$res2[catid]&id2=$id' method='post' name='form1'>";
echo "<td width='20%'>";
echo "<input type='text' name='itemname'></td>";
echo "<td width='20%'>";
echo "<input type='submit' name='Submit' value='Add Item'></td>";
echo "</form>";
echo "</tr>";



$temp_cat=$res2['category'];

}

while($res3=mysql_fetch_array($sql3))
{
echo "<tr>";
echo "<td width='20%'>";
echo "$res3[itemname]" . "</td>";
echo "<td width='20%'>";
echo "<a href='delitem.php'>Delete Item</a>" . "</td>";
echo "</tr>";

}
echo "</table>";
}


could someone please help me display this correctly?

thanks in advance :)

tivy
09 Nov 2010, 10:23 AM
mysql_fetch_array only grabs ONE row out of the query result. You need to use a for loop to cycle through the result and grab the rows one by one. So you might use something like the following.



for( i=0; i<mysql_num_rows($sql1); $i++) {
$res = mysql_fetch_array($sql1);
// do stuff
}