I have a site that has categories of images generated from the database, I am trying make it so when the user selects a category, the query is based off of that selection and the respective images load. Is that something that can be done?

Here is my current code.

$dynamiclist is all of the images that are in that category
$listcategories is what I'd like to have determine the query

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>

<?php 
include "storescripts/connect_to_mysql.php"; 
$dynamicList = "";
$linkquery = "*";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 10");
$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"];
             $subcategory = $row["subcategory"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $dynamicList .= 
    '
    <div>
          <li>
             <a href="product.php?id=' . $id . '"><img style="border:#FFF 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="180" height="255" border="1" /></a>
            <h4> <a name= ' . $id . '  id= ' . $id . '  value= ' . $id . '  href="product.php?id= ' . $id . '"> ' . $product_name . ' </a> </h4>
            <p>'. $subcategory .'</p> 
            <span>  $' . $price . '</span>
          </li>
    </div>
    ';
    }
} else {
    $dynamicList = "An error has occurred, call a mortician.";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<body>
<?php include ("../header.php");?>
<?php include ("../menu.php");?>

        <div class="body">
            <div class="sidebar">
                <div class="first">
                    <h2><a href="#">Dora Designs</a></h2>       

                    <!-- Dynamic Categories -->
                    <ul>
                        <? 
                include "storescripts/connect_to_mysql.php"; 
                $data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error());
                while($info = mysql_fetch_array( $data ))
                    { 
                    $listcategory = $info["category"];
                    print
                    '<li>
                    <a name=" " value="" id="" href=""> ' .  $listcategory . ' </a> 
                    </li>';
                    }
                mysql_close();  
                ?>
                    </ul>
                </div>
                <div>
                    <h2><a href="#">Weddings</a></h2>
                </div>
                <div>
                    <h2><a href="#">Birthdays</a></h2>
                </div>
            </div>
            <div class="content">
                <div class="figure">
                    <img src="/images/galleryholder.png" alt=""/>
                </div>
            <div class="products">
                    <div class="paging">
                        <div class="first">
                            <h2>Gallery</h2>
                        </div>
                    </div> 
                        <ul>
                        <?php echo $dynamicList; ?>
                        </ul>
                </div>
            </div>


            Thank you very much.
Member Avatar for LastMitch

@cheelo007

I have a site that has categories of images generated from the database, I am trying make it so when the user selects a category, the query is based off of that selection and the respective images load. Is that something that can be done?

What is your question? You didn't really explain it very clearly.

Did you got this script from this guy: Adam Khoury

You're like the 4th Daniweb member in 3 months (I think) that I encounter on Daniweb asking for assisting regarding about the code this guy put on his website.

The reason why I know because I still have this file on my computer because another Daniweb member had issues with uploading the website.

You have 5 main files:

cart.php
category.php
index.php
product.php
product_list.php

Yes, I got the code from his website.

I have 3 categories that have 5 images in each. Currently the script loads all images from all 3 categories.

I would like help with having images load based on what category the user selects. Since there are currently 15 images loading, I'm wondering how to have the category that is selected passed through the query so 15 images don't load but 5 images from the category that was selected.

Member Avatar for diafol
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{ 
$listcategory = $info["category"];
print
'<li>
 <a name=" " value="" id="" href=""> ' .  $listcategory . ' </a> 
</li>';
}

That's your category list. But it's hasn't got any hrefs which means that clicking on a category list won't filter the results.

I'd be looking for something like a category_id item in the $info array to populate the href attribute.

Is $info["category"] text or an integer? If it's text, this seems to be a poor way to relate tables. What's the MySQL structure? Could you give us that info (tablenames, fieldnames, datatypes) and any info that shows relates fields?

'<li>
 <a name=" " value="" id="" href=""> ' .  $listcategory . ' </a> 
</li>';

That looks particularly shoddy if you got it from an online example. Caveat emptor I suppose.

It definitely is shoddy, I wrote that part to generate all the categories I had in the database.

I'm just thinking that there are so many sites that list the categories to choose from in their sidebars, where when you click the categories, what you are viewing is updated. I'm trying to do the same thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.