Need help displaying items by category? <-- Newbie! Help! :-o

Discussion in 'PHP' started by Joe Flynn, Jan 26, 2012.

  1. #1
    Hello everyone, i'll make this brief, as I'm sure its very simple...

    I am a designer first and foremost, so php/mySQL coding is relatively new to me, however, I am in the process of building an online store for a small business. I have used a table 'products' to hold all my products, with the following structure;

    |id|product_name|price|category|description|product_image

    I have been able to create a page which shows ALL the products, however, what I would like to do is have a list of categories down one side which, when clicked will go to a page showing all products with that category. My question, do I have to build a separate page for each category, or can I build one page, and carry through the category variable in the URL rather like the actual product view page I have... (i.e /product.php?id=2).

    The code that outputs the products on the main store page is as follows;

    <?php
    
    $dynamic_list = "";
    $sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 50");
    $productCount = mysql_num_rows($sql); // count the output amount
    if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){
    $id = $row["id"];
    $product_name = $row["product_name"];
    $price = $row["price"];
    $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
    $details = $row["details"];
    $dynamic_list .= '<table width="90%" border="0" cellspacing="0" cellpadding="5">
    <tr>
    <td width="16%" valign="top"><a href="product.php?id='. $id .'"><img src="/inventory_images/'. $id .'.jpg" width="100" height="100" /></a></td>
    <td width="84%" valign="top">'. $product_name .'<br />
    £'. $price .'<br />
    '. $details .'<br />
    <a href="product.php?id='. $id .'">View Product</a></td>
    </tr>
    </table>';
    }
    } else {
    $dynamic_list = "There are no products listed in your store yet";
    }
    mysql_close();
    
    ?>
    PHP:
    I apologise if i'm not using the correct terminology, as i said, i'm relatively new to php programming, and have been building this store and a gallery by using bits and bobs from various tutorials around the web and modifying the code to try and figure it all out, learn by doing and all that...

    Many thanks in advance!

    Joe
     
    Joe Flynn, Jan 26, 2012 IP
  2. anisbd

    anisbd Active Member

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    83
    #2
    If I understand your problem that is you want to show your product category wise in the same page.
    If am I right you can pass category is instance of id in url (i.e /product.php?category=3)
    <?php
    
    $condition = "";
    if(isset($GET['category']) && !empty($GET['category'])) {
            $condition = " WHERE category = {$GET['category']}";
    }
    $sql = mysql_query("SELECT * FROM products $condition ORDER BY date_added DESC LIMIT 50");
    
    ?>
    PHP:
    Hope this tricks may help you.
     
    anisbd, Jan 26, 2012 IP