Jump to content

Recommended Posts

On my web page, I'm trying to have a list of clickable links on the left side of the page and then when you click one of those links(categories), it will list the items associated with that category on right.  I'm able to have the category links come up on the left...no problem.  When I click the category the items drop below it and show all the specific items.  I want it to show up in the right/middle of the page...so I moved the code appropriately.  Now when I click a category link on the left it only shows the very LAST item in the database rather then all of them associated with the category.

 

So I'm wondering how I can make it show all the items rather than just the last one when I move the code to a different part of the page.

 

Here is my code:

 

//show categories first
$get_cats_sql = "SELECT cat_id, cat_title FROM category ORDER BY cat_id";
$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['cat_id'];
        $cat_title = ($cats['cat_title']);
        

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

        if (isset($_GET["cat_id"])) {
		if ($_GET["cat_id"] == $cat_id) {
		   //get items
		   $get_items_sql = "SELECT id, photo, title, price FROM product WHERE cat_id = '".$cat_id."' ORDER BY date";
		   $get_items_res = mysqli_query($mysqli, $get_items_sql) or die(mysqli_error($mysqli));

		   if (mysqli_num_rows($get_items_res) < 1) {
				$display_block = "<p><em>Sorry, no items in this category.</em></p>";
		   } else {
				$display_block .= "<ul>";

				while ($items = mysqli_fetch_array($get_items_res)) {
				   $item_id  = $items['id'];
				   $item_photo = $items['photo'];
				   $item_title = stripslashes($items['title']);
				   $item_price = $items['price'];
				   

              $display_block .= "<a href=\"items3.php?id=".$item_id."\">".$item_title."</a></strong> (\$".$item_price.")</li>";
				  
				}

				$display_block .= "</ul>";
			}

Link to comment
https://forums.phpfreaks.com/topic/204888-not-displaying-results-correctly/
Share on other sites

Have you actually checked the raw HTML data? You have some problems with the HTML and that may prevent the data from being displayed in the browser. Namely, the items are in an unordered list and when you generate the links there is no opening <LI> tag, but there is a closing </LI> tag.

There were some inefficiencies in that code. Try this

<?php

//Set the selected category
$selected_cat = (isset($_GET["cat_id"])) ? $_GET["cat_id"] : false;

//show categories first
$get_cats_sql = "SELECT cat_id, cat_title FROM category ORDER BY cat_id";
$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
{
    //Display the categories
    while ($cats = mysqli_fetch_array($get_cats_res))
    {
        $display_block .= "<a href=\"{$_SERVER['PHP_SELF']}?cat_id={$cats['cat_id']}\">{$cats['cat_title']} <br /></a>\n";
        if ($cats['cat_id']==$selected_cat)
        {
            //get items
            $get_items_sql = "SELECT id, photo, title, price FROM product WHERE cat_id = '{$selected_cat}' ORDER BY date";
            $get_items_res = mysqli_query($mysqli, $get_items_sql) or die(mysqli_error($mysqli));
            if (mysqli_num_rows($get_items_res) < 1)
            {
                $display_block = "<p><em>Sorry, no items in this category.</em></p>\n";
            }
            else
            {
                $display_block .= "<ul>\n";
                while ($items = mysqli_fetch_array($get_items_res))
                {
                    $item_url = "items3.php?id={$items['id']}";
                    $item_title = stripslashes($items['title']);
                    $item_price = $items['price'];
                    $item_photo = $items['photo'];
                    $display_block .= "<li>";
                    $display_block .= "<a href=\"{$item_url}\">{$item_title}</a> (\${$item_price})";
                    $display_block .= "</li>\n";
                }
                $display_block .= "</ul>\n";
            }
        }
    }
}

?>

Thanks  mjdamato for the changes.  I posted it but is still giving me the same problem, the items under the main category are still dropping underneath.  I've tried using the "echo" and placing the "$display_block .= "</ul>";" out into the right center of the page it shows ALL the menu information, including the categories.  I'm rather new to PHP and this has got me in a stump.

 

I'm trying to get the information from:

$item_url = "items3.php?id={$items['id']}";

                    $item_title = stripslashes($items['title']);

                    $item_price = $items['price'];

                    $item_photo = $items['photo'];

                    $display_block .= "<li>";

                    $display_block .= "<a href=\"{$item_url}\">{$item_title}</a> (\${$item_price})";

                    $display_block .= "</li>\n";

 

to be displayed to another area of the page rather than right underneath the particular category but it doesn't want to let go. ;)

 

OK, the problem is in how you are constructing your pages. Not so much a logic problem. It is usually easier to build a framework for your page and then do all th elogic up front to determine the content for each section and THEN output the html. Here is a simple example using the above code:

<?php

//Set the selected category
$selected_cat = (isset($_GET["cat_id"])) ? $_GET["cat_id"] : false;

//show categories first
$get_cats_sql = "SELECT cat_id, cat_title FROM category ORDER BY cat_id";
$get_cats_res =  mysqli_query($mysqli, $get_cats_sql) or die(mysqli_error($mysqli));

if (mysqli_num_rows($get_cats_res) < 1)
{
   $categoryList = "<p><em>Sorry, no categories to browse.</em></p>";
}
else
{
    //Display the categories
    while ($cats = mysqli_fetch_array($get_cats_res))
    {
        $categoryList .= "<a href=\"{$_SERVER['PHP_SELF']}?cat_id={$cats['cat_id']}\">{$cats['cat_title']} <br /></a>\n";
        if ($cats['cat_id']==$selected_cat)
        {
            //get items
            $get_items_sql = "SELECT id, photo, title, price FROM product WHERE cat_id = '{$selected_cat}' ORDER BY date";
            $get_items_res = mysqli_query($mysqli, $get_items_sql) or die(mysqli_error($mysqli));
            if (mysqli_num_rows($get_items_res) < 1)
            {
                $content = "<p><em>Sorry, no items in this category.</em></p>\n";
            }
            else
            {
                $content .= "<ul>\n";
                while ($items = mysqli_fetch_array($get_items_res))
                {
                    $item_url = "items3.php?id={$items['id']}";
                    $item_title = stripslashes($items['title']);
                    $item_price = $items['price'];
                    $item_photo = $items['photo'];
                    $content .= "<li>";
                    $content .= "<a href=\"{$item_url}\">{$item_title}</a> (\${$item_price})";
                    $content .= "</li>\n";
                }
                $content .= "</ul>\n";
            }
        }
    }
}

?>
<html>
<body>

<div id="categories" style="width:200px;float:left;">
<?php echo $categoryList; ?>
</div>

<div id="content" style="width:600px;">
<?php echo $content; ?>
</div>

</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.