1 Replies - 385 Views - Last Post: 20 December 2017 - 03:21 AM Rate Topic: -----

#1 noobydoods  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 21-May 14

Filtering and paging

Posted 20 December 2017 - 12:58 AM

Hi, so this gonna a be a bit lengthy because i think my code is a mess. So thank you for reading, the questions i have will be clear.

I have a products page that needs to have thumbnail of the product, pagination, filtering by wtv attribute, mine is "type". And im using bootstrap.

This is how it looks like : Link to imgur

at the top of the page is this selection query so by default when the page loads it displays all products:
$per_page = 6;
if (isset($_GET["page"]))
  $page = $_GET["page"];
else
  $page = 1;
  $start_from = ($page-1) * $per_page;
  $stmt = $conn->prepare("SELECT fld_product_id, fld_product_name, fld_product_price, fld_product_type, fld_product_img FROM tbl_products LIMIT $start_from, $per_page");
  $stmt->execute();
  $result = $stmt->fetchAll();



the per_page codes are for pagination which will be at the bottom of this post as i am having trouble with that too.

these are the buttons that would be use for filtering:
<div align="center">
   <button type="submit" name="all" value="All" class="btn btn-default filter-button" data-filter="all">All</button>
   <button type="submit" name="type" value="Bowls & Feeders" class="btn btn-default filter-button" data-filter="bf">Bowls and Feeders</button>
   <button type="submit" name="type" value="Beds & Furnitures" class="btn btn-default filter-button" data-filter="bfur">Beds and Furnitures</button>
</div>



initially they didnt have the [type=submit] and [name] because I was wondering if after the top select query has been executed the results can be filtered for the specific types but obviously that would involve a WHERE clause which should be in the select query which brought me to the following code:
<?php
if(isset($_GET['type'])){
  if($_GET['type'] == "Bowls & Feeders"){
    $bf = $_GET['type'];
    $per_page = 6;
    if (isset($_GET["page"]))
      $page = $_GET["page"];
    else
      $page = 1;
      $start_from = ($page-1) * $per_page;
      $query = $conn->prepare("SELECT fld_product_id, fld_product_name, fld_product_price, fld_product_type, fld_product_img FROM tbl_products WHERE fld_product_type = :type LIMIT $start_from, $per_page");
      $query->bindParam(':type', $bf, PDO::PARAM_STR);
      $query->execute();
      $result = $query->fetchAll();
  } elseif ($_GET['type'] == "Beds & Furnitures") {
      $bfur = $_GET['type'];
      $per_page = 6;
      if (isset($_GET["page"]))
        $page = $_GET["page"];
      else
        $page = 1;
        $start_from = ($page-1) * $per_page;
        $query = $conn->prepare("SELECT fld_product_id, fld_product_name, fld_product_price, fld_product_type, fld_product_img FROM tbl_products WHERE fld_product_type = :type LIMIT $start_from, $per_page");
        $query->bindParam(':type', $bfur, PDO::PARAM_STR);
        $query->execute();
        $result = $query->fetchAll();
    }
}?>

<?php 
foreach($result as $row) {
   if ($row['fld_product_img'] == "" ) { ?>
     <div class="gallery_product col-lg-4 col-md-4 col-sm-4 col-xs-6 filter">
        <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">
        <a href="products_details.php?pid=<?php echo $row['fld_product_id']; ?>" class="btn btn-warning btn-xs" role="button"><img src="products/<?php echo $row['fld_product_img'] ?>" class="img-responsive"></a>
        <p class="product-name"><?php echo $row['fld_product_name'] ?></p>
        <p class="product-price">RM <?php echo $row['fld_product_price'] ?></p>
   <?php } 
} ?>
     </div>



as i mentioned my code is kinda messy. I don't know of a better way to do what i need, that's why i am here because i am certain there has got to be a more robust way of doing this. i have 5 more types left.

pagination:
<?php
$stmt = $conn->prepare("SELECT * FROM tbl_products_a154287");
$stmt->execute();
$result = $stmt->fetchAll();
$total_records = count($result);
$total_pages = ceil($total_records / $per_page);
?>
<?php if ($page==1) { ?>
    <li class="disabled"><span aria-hidden="true"></span></li>
    <?php } else { ?>
         <li><a href="products.php?page=<?php echo $page-1 ?>" aria-label="Previous"><span aria-hidden="true"></span></a></li>
    <?php
    }
    for ($i=1; $i<=$total_pages; $i++)
      if ($i == $page)
        echo "<li class=\"active\"><a href=\"products.php?page=$i\">$i</a></li>";
      else
        echo "<li><a href=\"products.php?page=$i\">$i</a></li>";
    ?>
    <?php if ($page==$total_pages) { ?>
        <li class="disabled"><span aria-hidden="true"></span></li>
    <?php } else { ?>
        <li><a href="products.php?page=<?php echo $page+1 ?>" aria-label="Previous"><span aria-hidden="true"></span></a></li>
    <?php } ?>



as i mentioned, im having trouble figuring out how to paginate according to the type selected as of right now if the second page was to be clicked the products displayed would actually be all the products and not the the specific type. I thought the $result would be taken from the selected type if the select query was removed but thougt wrong obviously.

assistance with the issues i mentioned will be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Filtering and paging

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6445
  • View blog
  • Posts: 26,066
  • Joined: 12-December 12

Re: Filtering and paging

Posted 20 December 2017 - 03:21 AM

Some general points:

Are your submit buttons in a form element?
Does the get array contain the values you expect?

Inspect the page, and look at the url's, to confirm the values you expect to see. Echo the $_GET content to confirm that it is populated as you expect.

Progressive Testing

Once you have confirmed these details I think you should provide a clearer question, there is too much information currently.

Question moved to PHP forum.




Note that elements no longer have an align attribute, use CSS instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1