Hi, I don't know if this is possible or a really stupid question. I was wondering if after a select query has been executed can the results be filtered for specific values, and not just a single value but 7 different values. with what i want to accomplish its either im making it difficult for myself or the solution is pretty simple.

okay for example i have a select query like so:

$stmt = $conn->prepare("SELECT id, name, price, type, img FROM table");
$stmt->execute();
$result = $stmt->fetchAll();

now i have a page that has buttons which represent different types of "type". each button when clicked displayes images that corresponds to the type, i am using bootstrap for this. example pic:

1.JPG

so its like if bowls and feeders is clicked then the corresponding images will appear which were selected from database, and same goes to the rest of the buttons. right now i have succeeded in displaying the images but because there isnt a "filter" function or "where" clause in the query all the images are displayed regardless of the "type"

page.php:

$stmt = $conn->prepare("id, name, price, type, img FROM table ");
$stmt->execute();
$result = $stmt->fetchAll();

<div class="container">
    <div class="row">
        <div class="gallery col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <h1 class="gallery-title">Products</h1>
            <div class="button-create">
                <button class="btn btn-default" type="button" name="create"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add new product</button>  
            </div>
        </div>

        <div align="center">
            <button class="btn btn-default filter-button" data-filter="all">All</button>
            <button class="btn btn-default filter-button" data-filter="bf">Bowls and Feeders</button>
            <button class="btn btn-default filter-button" data-filter="fur">Beds and Furnitures</button>
            <button class="btn btn-default filter-button" data-filter="cats">Adopt a Cat</button>
            <button class="btn btn-default filter-button" data-filter="food">Cat Food</button>
            <button class="btn btn-default filter-button" data-filter="groom">Grooming Supplies</button>
            <button class="btn btn-default filter-button" data-filter="litter">Litter Boxes</button>
            <button class="btn btn-default filter-button" data-filter="toys">Toys</button>
        </div>
        <br/>

        <?php 
        foreach($result as $row ) {
            if ($row['img'] == "" ) { ?>
                    <div class="gallery_product col-lg-4 col-md-4 col-sm-4 col-xs-6 filter bf">
                        <img src="products/noimage.png" class="img-responsive">

                <?php }
                else { ?>
                    </div>
                    <div class="gallery_product col-lg-4 col-md-4 col-sm-4 col-xs-6 filter bf">
                        <a href="products_details.php?pid=<?php echo $row['id']; ?>" class="btn btn-warning btn-xs" role="button"><img src="products/<?php echo $row['img'] ?>" alt="<?php echo $row['name'] ?>" class="img-responsive"></a>
                        <p class="product-name"><?php echo $row['name'] ?></p>
                        <p class="product-price">RM <?php echo $row['price'] ?></p>
            <?php } 
        }?>
                    </div>
        //I DID THIS SECOND ONE THINKING IT WILL DIFFERENTIATE TYPES            
        <?php 
        foreach($result as $row ) {
            if ($row['img'] == "" ) { ?>
                    <div class="gallery_product col-lg-4 col-md-4 col-sm-4 col-xs-6 filter fur">
                        <img src="products/noimage.png" class="img-responsive">

                <?php }
                else { ?>
                    </div>
                    <div class="gallery_product col-lg-4 col-md-4 col-sm-4 col-xs-6 filter fur">
                        <a href="products_details.php?pid=<?php echo $row['id']; ?>" class="btn btn-warning btn-xs" role="button"><img src="products/<?php echo $row['img'] ?>" alt="<?php echo $row['name'] ?>" class="img-responsive"></a>
                        <p class="product-name"><?php echo $row['name'] ?></p>
                        <p class="product-price">RM <?php echo $row['price'] ?></p>
            <?php } 
        }?>
                    </div>

I hope i am explaining myself properly as im sleep deprived, please let me know if my explaination isn't clear. I also hope that wtv i am doing actually makes sense because i do need the "category menu" which shows thumbnail of products respectively. or if someone has a better idea of doing that, that would be great.

thanks in advance.

Recommended Answers

All 3 Replies

Option one is to make a new query that does include the filter clicked as a WHERE clause and get the subset products returned.

Seeing as this is purely PHP and you need to postback regardless of what the user does, appending the selected buttons onto the query as a WHERE clause is probably your best option so forget option 2.
On clicking a button postback the form with the array of currently selected buttons as a parameter, adjust your query apropriately and you're done.

If you need more in depth explanations I'll be happy to help.
Also, if this is a custom coded job can I suggest Magento, Wordpress or Joomla (in order of my personal preference) to you?

its not a job. its a project for my web development class and we have been specified to use what we have learnt throughout the semester.

hmm. i didn't put the WHERE clause at first because i wanted the page by default to show all the images, or in this case products, that are in the database.

"appending the selected buttons onto the query as a WHERE clause" - i don't understand, sorry.

I was thinking of adding the [type=submit] and [name] attribute to the button. then use if(isset()) then do select query but then this would have to be done for each button which is tedious and messy. something like:

<button type="submit" name="bf" class="btn btn-default filter-button" data-filter="bf">Bowls and Feeders</button>

<?php
  if(isset($_GET['bf'])){
     $bf = "Bowls & Feeders";
     $query = $conn->prepare("SELECT columns FROM table WHERE column = :type");
     $query->bindParam(':type', $bf, PDO::PARAM_STR);
     $query->execute();
     $result = $query->fetchAll();
   }
?>

or have a att value in the button tag instead of $bf = "Bowls & Feeders";, would it read the value though?

thoughts?

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.