Results 1 to 2 of 2

Thread: please help!!! problem displaying data

  1. #1
    busby Guest

    please help!!! problem displaying data

    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:

    PHP 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

  2. #2
    Join Date
    May 2010
    Location
    College Station, TX
    Posts
    216
    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.

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

    All web designers hate the internet. If I spend all day making/updating/looking at websites, why the hell would I want to deal with it outside of work?

Similar Threads

  1. getting data from a website and displaying it in your own site
    By adams2887 in forum Web Design, HTML Reference and CSS
    Replies: 1
    Last Post: 14 Oct 2010, 01:16 AM
  2. Displaying data from a online text file. Using selection boxes to modify content.
    By EricH in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 30 Oct 2009, 06:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •