MySQL Query pagination with PHP -
how can add pagination system simple item display? , how can add pagination results filter? lost part , cant figure out! want apply css on menu too!
here's code:
<?php include('db.php'); if(isset($_post['filter'])) { $filter = $_post['filter']; $result = mysql_query("select * products product '%$filter%' or description '%$filter%' or category '%$filter%'"); } else { $result = mysql_query("select * products"); } while($row=mysql_fetch_assoc($result)) { echo '<li class="portfolio-item2" data-id="id-0" data-type="cat-item-4">'; echo '<div> <span class="image-block"> <a class="example-image-link" href="reservation/img/products/'.$row['imgurl'].'" data-lightbox="example-set" title="'.$row['product'].'""><img width="225" height="140" src="reservation/img/products/'.$row['imgurl'].'" alt="'.$row['product'].'" title="'.$row['product'].'" /> </a> </span> <div class="home-portfolio-text"> <h2 class="post-title-portfolio"><font color="#666666">'.$row['product'].'</font></h2> <p class="post-subtitle-portfolio"><font color="#666666">descrição: '.$row['description'].' <p class="post-subtitle-portfolio"><font color="#666666">categoria: '.$row['category'].' <p class="post-subtitle-portfolio">código: '.$row['price'].'</p><br/></font></p> </div> </div>'; echo '</li>'; } ?>
edited:
<?php include('db.php'); if(isset($_post['filter'])) { $filter = $_post['filter']; $result = mysql_query("select * products product '%$filter%' or description '%$filter%' or category '%$filter%'"); } else { $start=0; $limit=6; if(isset($_get['id'])) { $id = $_get['id']; $start = ($id-1)*$limit; } $result = mysql_query("select * products limit $start, $limit"); } while($row = mysql_fetch_array($result)) { echo '<li class="portfolio-item2" data-id="id-0" data-type="cat-item-4">'; echo '<div> <span class="image-block"> <a class="example-image-link" href="reservation/img/products/'.$row['imgurl'].'" data-lightbox="example-set" title="'.$row['product'].'""><img width="225" height="140" src="reservation/img/products/'.$row['imgurl'].'" alt="'.$row['product'].'" title="'.$row['product'].'" /> </a> </span> <div class="home-portfolio-text"> <h2 class="post-title-portfolio"><font color="#666666">'.$row['product'].'</font></h2> <p class="post-subtitle-portfolio"><font color="#666666">descrição: '.$row['description'].' <p class="post-subtitle-portfolio"><font color="#666666">categoria: '.$row['category'].' <p class="post-subtitle-portfolio">código: '.$row['price'].'</p><br/></font></p> </div> </div>'; echo '</li>'; } echo "</ul>"; $rows = mysql_num_rows(mysql_query("select * products")); $total = ceil($rows/$limit); if($id>1) { echo "<center><a href='?id=".($id-1)."' class='button'>anterior</a></center>"; } if($id!=$total) { echo "<center><a href='?id=".($id+1)."' class='button'>próximo</a></center>"; } ?>
you should have this:
// declare base query $q = "select * products"; if(isset($_post['filter'])) { $filter = $_post['filter']; // append filter query $q += "where product '%$filter%' or description '%$filter%' or category '%$filter%'"); } // check "page" url parameter, if not available, go first page $page = isset($_get['page']) ? $_get['page'] : 1; // check "pagesize" url parameter, if not available, fall 20 $pagesize = isset($_get['pagesize']) ? $_get['pagesize'] : 20; // append pagination query $q += sprintf("limit %d,%d;", ($page-1)*$pagesize, $pagesize); // execute constructed query $result = mysql_query($q);
note code "pseudoish", not tested, should give base idea.
also, can check this post pagination mysql.
update
in php if use uninitialized variable, have context-dependent default value. see documentation here. here extract:
it not necessary initialize variables in php practice. uninitialized variables have default value of type depending on context in used - booleans default false, integers , floats default zero, strings (e.g. used in echo) set empty string , arrays become empty array.
Comments
Post a Comment