0

Hello there!

I have a question - I'm trying to display store categories (ex: Apparel and Accessories) in this code. The categories display perfectly, but when I click on said categories, they do not display the items underneath it. I've populated the database with a few values, but they still do not display.

//this is a script to view categories
<?php
//connect to database
$mysqli = mysqli_connect("localhost", "meh", "blah", "hello");

$display_block = "<h1>My Categories</h1>
<p>Select a category to see its items.</p>";

//show categories first
$get_cats_sql = "SELECT id, cat_title, cat_desc FROM product_categories ORDER BY cat_title";

$get_cats_res = mysqli_query($mysqli, $get_cats_sql) or die(mysqli_error($mysqli));

if(mysqli_num_rows($get_cats_res) < 1) {
    $display_block = "<p><em>Sorry, no categories to browse.</em></p>";
} 

else {
    while ($cats = mysqli_fetch_array($get_cats_res)) {
        $cat_id = $cats['id'];
        $cat_title = strtoupper(stripslashes($cats['cat_title']));
        $cat_desc = stripslashes($cats['cat_desc']);

        $display_block .= "<p><strong><a href=\"".$_SERVER["PHP_SELF"]. 
        "?cat_id =".$cat_id."\">".$cat_title."</a></strong><br/>"
        .$cat_desc."</p>";

        if (isset($_GET["cat_id"])) {
            if ($_GET["cat_id"] == $cat_id) {
            //get items
            $get_products_sql = "SELECT product_num, product_name, price, cat_id FROM product_info
                      WHERE cat_id = '".$cat_id."' 
                      ORDER BY product_name";
            $get_products_res = mysqli_query($mysqli, $get_products_sql)
                     or die(mysqli_error($mysqli));

            if (mysqli_num_rows($get_products_res) < 1) {
                $display_block = "<p><em>Sorry, no products in this category.</em></p>";
            } else {
                $display_block .= "<ul>";
                while ($products = mysqli_fetch_array($get_products_res)) { 
                    $product_num = $products['product_num'];
                    $product_name = stripslashes($products['product_name']);
                    $product_price = $products['price'];

                    $display_block .= "<li><a href=\"viewitem.php?
                    product_num=".$product_num."\">".$product_name."</a>
                    </strong> (\$".$product_price.")</li>";

                }
                $display_block .= "</ul>";
            }
            //free results
            mysqli_free_result($get_products_res);
        }
    }
}
}
//free results
mysqli_free_result($get_cats_res);

//close connection to mysql
mysqli_close($mysqli);
?>
<html>
<head>
<title>My Categories</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>      

If anyone could help, this would be wonderful! Thank you!

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by ZZZubec
0

insert in the beginning of the code

<?php
print_r( $_GET );
...

and click of link. scrpit show you $_GET array, maybe it help you see the problem.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.