SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting data when fetching individually

    I have a list of items, and a list of places, and a list which links the items to places.

    My code takes a search input of the place id, and then looks for all items in the table that just links items and places, then takes the line of info from the table which holds the item info (id, name, cost etc).

    But i want to sort the data by cost. How can i do this?

    ----------------
    tables:
    items (id, name, cost, type, sell)
    places (id, name)
    item_places (item id, shop id)

    Some items belong in multiple places, all places have multiple items. So i can not put shop id into item table.

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    add order by cost to the end of your query

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can not. Because Cost is not in the table i am fetching the list from.

    PHP Code:
    $get_items_query=mysql_query("SELECT xItemid FROM shops_items WHERE xShopid='".$userinput["xShopid"]."'");

        while (
    $item_list=mysql_fetch_assoc($get_items_query)) {

            
    $item_info=mysql_fetch_assoc(mysql_query("SELECT xName,xCost,xSale,xType FROM items WHERE xId='".$item_list["xItemid"]."'"));
            echo 
    "<tr><td>".$item_info['xName']."</td><td>".get_item_type($item_info['xType'])."</td><td>";
            if (
    $item_info['xCost']>0) echo "$".$item_info['xCost']; else echo "Cannot Buy";
            echo 
    "</td><td>";
            if (
    $item_info['xSale'] > -1) echo "$".$item_info['xSale']; else echo "Cannot Sell";
            echo 
    "</td></tr>";
        } 
    As you can see i collect a list of items that are found in a place, then fetch the info about that item from the item table. Due to database normalisation!

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it possible to use sub queries?

    eg
    Code:
    SELECT * FROM items WHERE id IN (SELECT xItemid FROM shops_items WHERE xItemid=$item_id);
    ?


Bookmarks

Posting Permissions

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