Search PHP/MySQL with Pagination - php

I wrote this code to search in rows of the table:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$page_max = 8;
if (isset($_POST['search'])){
$search = $_POST['searchValue'];
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers WHERE name LIKE '%$search%'");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
$numberOfRecords = $page_max;
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = $page * $page_max;
}
else{
$page = 0;
$start = $page * $page_max;
}
$customers = $database->getUsers("SELECT * FROM customers WHERE name LIKE '%$search%' LIMIT $start, $numberOfRecords");
}
else{
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
$numberOfRecords = $page_max;
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = $page * $page_max;
}
else{
$page = 0;
$start = $page * $page_max;
}
$customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
if(isset($_GET['page']) && $_GET['page'] > 0){
$previous = $_GET['page'] - 1;
echo '<li class="page-item"><a class="page-link" href="?page='. $previous.'">Previous</a></li>';
}
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
if(isset($_GET['page']) && $_GET['page'] < $numberOfPages - 1){
$page = $_GET['page'];
$next = $page + 1;
echo '<li class="page-item"><a class="page-link" href="?page='.$next.'">Next</a></li> ';
}
elseif(!isset($_GET['page'])){
echo '<li class="page-item"><a class="page-link" href="?page=1">Next</a></li> ';
}
?>
</ul>
</div>
Searching works fine when I'm on page 0, but when I'm at page 2 I can only search for records on that page (I think this is because of the limit in my query) besides that when I search for 'A' it returns 7 pages but if I click on the seconde page it won't load the search results but the main list with customers on page 2.
Could anyone help me fix these issues?

First of all: sanitize your input. Using POST or GET in database queries can be very, very dangerous.
Second: DRY, you probably can do this with half the code you are using.
The reason why you are "losing" your search is because your pagination doesn't carry over the search term.
Add it to the links in the pagination and switch from POST to GET:
<?php
$searchString = '';
if($_GET['searchValue']){
$searchString = '&searchValue=' . $_GET['searchValue'];
}
if(isset($_GET['page']) && $_GET['page'] > 0){
$previous = $_GET['page'] - 1;
echo '<li class="page-item"><a class="page-link" href="?page='. $previous.$searchString.'">Previous</a></li>';
}
// more code
'... href="?page='.$i.$searchString'" ...'
'... href="?page='.$next.$searchString'" ...'
Note that I use $_GET['searchValue'] instead of POST. in most cases there is no advantage in using POST for searches (quite the opposite, if you use GET you can track it in Google Analytics f.ex.). So make sure you change your form method to get and if (isset($_POST['search'])){ to $_GET['searchValue']. If you insist on using POST, you can use $_REQUEST, which holds values from GET and POST.

Related

Holding PHP/AJAX filter selections during pagination

I have a page which allows you to filter results using an AJAX call which works fine, I have added pagination which work fine initially but as soon as you move to another page, the checkbox becomes unchecked and it just shows all results again. I assume this is because the page is reloading when it moves to page 2, is there a way of keep the filter setting set and continue to show the results from the filter AJAX. The pagination obvisouly works fine when no filter is selected but my brain just doesn't seem to be working and can't work this out.
Any help would be appreciated!
My code is below, I am also aware that currently my code is open to sql injection but just trying to get everything to work and then will go back through it:
<body>
<?php include("PHP/header.php"); ?>
<div class="container-fluid">
<div class="container" style="margin-top: 2%; text-align: center;">
<h1> Reviews</h1>
On This page you will find our reviews on music tech and software
<br/>
<br/>
<br/>
Filter Reviews:
<ul class="list-group">
<?php
$search = $conn->prepare("SELECT DISTINCT reviewcat FROM review_db ORDER BY reviewcat");
$search->execute();
while ($row = $search->fetch(PDO::FETCH_ASSOC)) {
?>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="<?=$row['reviewcat'];?>" id="reviewcat"> <?=$row['reviewcat']; ?>
</label>
</div>
</li>
<?php } ?>
</ul>
</div>
<br/><br/>
<div class="row-fluid ">
<h5 class="text-center" id="textChange"> All Reviews </h5>
<hr>
<div class="text-center">
<img src="Images/loader.gif" id="loader" width="100" style="display: none">
</div>
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 8;
$offset = ($pageno-1) * $no_of_records_per_page;
// Prev + Next
$prev = $pageno - 1;
$next = $pageno + 1;
?>
<div id="result" class="card-deck card_group_style pt-4" >
<?php
$stmt = $conn->prepare("SELECT COUNT(*) FROM review_db");
$stmt->execute();
$total_rows = $stmt->fetchColumn();
$total_pages = ceil($total_rows / $no_of_records_per_page);
$result = $conn->prepare("SELECT * FROM review_db ORDER BY reviewsub DESC LIMIT $offset, $no_of_records_per_page ");
$result->execute();
?>
<?php while ($row = $result->fetch(PDO::FETCH_ASSOC)) {// Important line !!! Check summary get row on array .. ?>
<?php
$my_date = $row['reviewsub'];
$date = DATE("d/m/Y",strtotime($my_date));
?>
<div class="col-sm-6 col-lg-3 py-2">
<div class="card mb-4">
<img class="card-img-top card-images " src="Images/Reviews/<?php echo $row['reviewimage1'];?>" alt="<?php echo $row['reviewtitle'];?>" >
<div class="card-body">
<h5 class="card-title"><?php echo $row['reviewtitle'];?></h5>
<p class="card-text"><?php echo $row['reviewsynop'];?></p>
<a href="Reviews/review-content.php?id=<?php echo $row['id'];?>&reviewtitle=<?php echo $row['reviewtitle'];?>" class="btn btn-primary my-4" >Read More</a>
<div class="card-footer" style="padding: 1%;">
<small class="text-muted">Submitted: <?php echo $date; ?></small>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<div class="container">
<!-- Pagination Controller -->
<?php
if($total_pages <= 1){
$hidepage = 'none';
}else{
$hidepage = 'flex';
}
?>
<ul class="pagination justify-content-center pagination-mb" style="display: <?php echo $hidepage; ?>">
<li><a class="page-link" href="?pageno=1">First</a></li>
<li class="page-item <?php if($pageno <= 1){ echo 'disabled'; } ?>">
<a class="page-link" href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
</li>
<?php for($i = 1; $i <= $total_pages; $i++ ): ?>
<li class="page-item <?php if($pageno == $i) {echo 'active'; } ?>">
<a class="page-link" href="?pageno=<?= $i; ?>"> <?= $i; ?> </a>
</li>
<?php endfor; ?>
<li class="page-item <?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
<a class="page-link" href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a>
</li>
<li><a class="page-link" href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
</ul>
<!-- Pagination end -->
</div>
</div>
</div>
</div>
<?php include("PHP/footer.php"); ?>
</div>
</body>
<?php include("PHP/js.php"); ?>
<script>
$(document).ready(function(){
$('#link-review,#link-footer-review').addClass('active');
});
</script>
<script type="text/javascript">
$(document).ready(function(){
function get_filter_text(text_id){
var filterData = [];
$('#'+text_id+':checked').each(function(){
filterData.push($(this).val());
});
return filterData;
}
$(".product_check").click(function(){
if ($(this).prop('checked')) {
$("#loader").show();
var action = 'data';
var reviewcat = get_filter_text('reviewcat');
$.ajax({
method:'POST',
url:'reviewaction.php',
data:{action:action,reviewcat:reviewcat},
success:function(response){
$("#result").html(response);
$("#loader").hide();
$("#textChange").text("Filtered Reviews");
}
});
} else {
$("#loader").show();
var action = 'data';
var reviewcat = get_filter_text('reviewcat');
$.ajax({
method:'POST',
url:'reviewaction.php',
data:{action:action,reviewcat:reviewcat},
success:function(response){
$("#result").html(response);
$("#loader").hide();
$("#textChange").text("All Reviews");
}
});
}
});
});
</script>
reviewaction.php:
<?php
if(isset($_POST['action'])){
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 8;
$offset = ($pageno-1) * $no_of_records_per_page;
// Prev + Next
$prev = $pageno - 1;
$next = $pageno + 1;
$checksql = "SELECT COUNT(*) FROM review_db WHERE reviewcat !=''";
$sql = "SELECT * FROM review_db WHERE reviewcat !=''";
if(isset($_POST['reviewcat'])){
$reviewcat = implode("','", $_POST['reviewcat']);
$checksql .="AND reviewcat IN('".$reviewcat."')";
$sql .="AND reviewcat IN('".$reviewcat."')";
}
$resultpag = $conn->prepare($checksql);
$resultpag->execute();
$total_rows = $resultpag->fetchColumn();
$total_pages = ceil($total_rows / $no_of_records_per_page);
$sql .="ORDER BY reviewsub DESC LIMIT $offset, $no_of_records_per_page ";
$result = $conn->prepare($sql);
$result->execute();
$output='';
if (count($result) > 0) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$my_date = $row['reviewsub'];
$date = DATE("d/m/Y",strtotime($my_date));
$output .= '
<div class="col-sm-6 col-lg-3 py-2">
<div class="card mb-4">
<img class="card-img-top card-images" src="Images/Reviews/'.$row['reviewimage1'].'" alt="'.$row['reviewtitle'].'" >
<div class="card-body">
<h5 class="card-title">'.$row['reviewtitle'].'</h5>
<p class="card-text">'.$row['reviewsynop'].'</p>
<a href="Reviews/review-content.php?id='.$row['id'].'&reviewtitle='.$row['reviewtitle'].'" class="btn btn-primary my-4" >Read More</a>
<div class="card-footer" style="padding: 1%;">
<small class="text-muted">Submitted: '.$date.'</small>
</div>
</div>
</div>
</div>
';
} //While Loop End
}else{
$output = "<h3>No Reviews Found!</h3>";
}
echo $output;
}
?>
You can do it a couple of ways. One is to add the filter to the GET URI parameters of each page link at the end of your filter function, and add code that marks the filters as selected/checked if the parameters exist in the URI before running the POST request. The other is to change the code so that pagination is done with the same POST request instead of actually navigating to a new URL.

Pagination for search page

I want to make a pagination for search results. I can see the results in first page when I use the search box but I can't see the other results in other pages. What am I doing wrong?
I would be really glad if you could help me
Here' my search page codes.
Codes for pagination
<?php
if(isset($_GET["page"])) {
$page = $_GET["page"];
}else {
$page = "";
}
if($page == "" || $page == 1) {
$starter_post = 0;
} else {
$starter_post = ($page * 6) - 6;
}
$sql_query2 = "SELECT * FROM posts ";
$look_all_post = mysqli_query($conn, $sql_query2);
$all_post_count = mysqli_num_rows($look_all_post);
$page_number = ceil ($all_post_count / 6);
Codes for search results
if(isset($_POST["searchbtn"])) {
$search = $_POST["search"];
$query = "SELECT * FROM posts WHERE post_tags LIKE '%$search%' ORDER BY post_id DESC LIMIT $starter_post, 6";
$search_query = mysqli_query($conn, $query);
if(!$search_query) {
die("QUERY FAILED:". mysqli_error($conn));
}
$search_count = mysqli_num_rows($search_query);
if($search_count == 0) {
echo "<h3> No Result </h3>";
} else {
while ($row = mysqli_fetch_assoc($search_query)){
$post_id = $row["post_id"];
$post_date = $row["post_date"];
$date = strtotime($post_date);
$newdate = date("d/m/Y", $date);
$post_title = $row["post_title"];
$post_text = $row["post_text"];
$post_image = $row["post_image"];
?>
<div class="col-md-6">
<div class="blog">
<div class="blog-img ">
<img src="images/<?php echo $post_image; ?>" class="img-fluid" >
</div>
<div class="blog-content">
<ul class="blog-meta">
<li><i class="far fa-calendar-alt"></i><span class="writer"><?php
echo $newdate; ?></span></li>
</ul>
<h3><?php echo $post_title; ?></h3>
<p><?php echo $post_text; ?></p>
<div class="blog-content2 text-center">
</div>
</div>
</div>
</div>
<?php }
}
}
?>
Codes for pagination
</div>
<div class="row">
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
<li <?php if ($page == 1 or $page == "") {echo "class='page-item disabled'";} ?>>
<a class="page-link" href="search.php?page=<?php if ($page > 1) {echo $page - 1;} ?>">Previous</a>
</li>
<?php //Pagination Continue
for($i=1; $i<=$page_number; $i++) {
echo "<li class='page-item'><a class='page-link' href='search.php?page=$i'>{$i}</a></li>";
}
?>
<li <?php if ($page == $page_number) {echo "class='page-item disabled'";} ?>>
<a class="page-link" href="search.php?page=<?php if ($page == "") {echo $page = 2;} else if($page_number!=$page){echo $page+1;} else if($page_number==$page){echo $page;}?>">Next</a>
</li>
</ul>
</nav>
</div>

How to paginate search result using php pdo?

I am creating a pagination for search result all looks fine on first loading page,
but when I click on paginated links then it show me a white page (search page but without entries in).
simply with no results found from my data
This is the site search box in index.php
<form action="search/" method="post">
<input type="text" name="search" placeholder="word..." required />
<input type="submit" name="submit_search" class="search-btn" value="" />
</form>
Here is my setup for htaccess pagination:
RewriteRule ^search/page/(.*)$ search.php?page=$1 [L,NC]
it looks fine on browser.
Here is my search page codes:
<?php
if(isset($_POST['submit_search']) && !empty($search)){
$search = htmlspecialchars(strip_tags($_POST['search']), ENT_QUOTES);
$perpage = intval("12");
$n_stmt = $pdo->prepare("SELECT * FROM posts");
$n_stmt->execute();
$total_posts = $n_stmt->rowCount();
$total_pages = ceil($total_posts/$perpage);
$page = !empty($_GET['page']) && intval($_GET['page']) ? (int) intval($_GET['page']) : intval("1");
if($page < intval("1")) $page = intval("1");
if($page > $total_pages) $page = $total_pages;
$limit = ($page - intval("1")) * $perpage;
$pages_to_show = intval("10");
$stmt = $pdo->prepare("SELECT * FROM posts WHERE title LIKE :search OR subject LIKE :search OR descriptions LIKE :search OR keywords LIKE :search ORDER BY created DESC LIMIT :limit, :perpage");
$stmt->bindValue(":search","%".$search."%", PDO::PARAM_STR);
$stmt->bindValue(":limit",$limit, PDO::PARAM_INT);
$stmt->bindValue(":perpage",$perpage, PDO::PARAM_INT);
if($stmt->execute()){
if($stmt->rowCount() > 0){
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){?>
<div class="news_box">
<a href="<?php echo htmlspecialchars($row['news_url']);?>/" title="<?php echo htmlspecialchars($row['title']);?>">
<div class="title"><h2><?php echo htmlspecialchars($row['title']);?></h2></div>
<div class="image">
<img src="images/posts/<?php echo htmlspecialchars($row['img']);?>" alt="<?php echo htmlspecialchars($row['title']);?>"/></div>
<div class="spot"><?php echo htmlspecialchars($row['subject']);?></div>
</a>
</div>
<?php
}
}
}
unset($stmt);
?>
Here is my pagination part:
<div class="pagination">
<?php if($page >1){?>
First
previous
<?php } for($i = $page - $pages_to_show; $i < $page + $pages_to_show + 1; $i++){
if($i > 0 and $i <= $total_pages){
if($i == $page){?>
<?php echo intval($i);?>
<?php }else{?>
<?php echo $i;?>
<?php
}
}
?>
<?php } ?>
<?php if($page != $total_pages){?>
Next
Last
<?php } ?>
</div>

Pagination in Codeigniter 3

I'm trying to make a pagination for a query. I can't figure why is the reason that the pagination doesn't work, it shows the pagination links, right now I have 15 records and I want to show that in batches of 5, but in the page # 1 I see the 15 records and when I click in the second page link I have an error that says that the page doesn't exist.
This is my model "searchCases":
//Function to get the active cases
function getCases(){
$query = $this->db->
select('*')->
from('customer_complaint')->
where('active', 1)->
get();
return $query->result();
}
//Function to get the number of opened cases
function countCases(){
$query = $this->db->
select('*')->
from('customer_complaint')->
where('active', 1)->
get();
return $query->num_rows();
}
// Function to retrieve a list of all the records of the table an the params $limit and $start to
// determine the number of records to return and what record to start from.
function fetchCases($limit, $start){
$this->db->limit($limit, $start);
$query = $this->db->
select('*')->
from('customer_complaint')->
where('active', 1)->
get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
Now this is my Controller "opened_cases":
public function index(){
$this->load->view('headers');
$this->load->view('navbar');
$data['query'] = $this->searchCases->getCases();
$config['base_url'] = base_url().'index.php/opened_cases/';
$config['total_rows'] = $this->searchCases->countCases();
$config['per_page'] = 5;
$config['uri_segment'] = 2;
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data["results"] = $this->searchCases->
fetchCases($config["per_page"], $page);
$this->load->view('opened_cases', $data);
$this->load->view('footer');
}
and my View "opened_cases":
<div class="col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">Search Results:
</div><!--End of Panel Heading-->
<div class="panel-body">
<table class="table" style="text-align: center">
<tr>
<td>Case ID</td>
<td>Received Date</td>
<td>Folio</td>
<td>Transactions Date</td>
<td>Reason of Complaint</td>
<td>Source of Complaint</td>
<td>Files</td>
</tr>
<?php
foreach ($query as $row) {?>
<tr>
<td><?= $row->complaint_id?></td>
<td><?= date('m/d/Y', strtotime($row->received_date))?></td>
<td><?= $row->folio?></td>
<td><?= date('m/d/Y', strtotime($row->tx_date))?></td>
<?php
$this->load->model('searchCases');
$id=$row->complaint_id;
$reason_id=$this->searchCases->reason($id);
$source_id=$this->searchCases->source($id);
foreach ($reason_id as $rsn) {
if($rsn->reason == 'Others'){
echo "<td class='text-left'>".$rsn->reason.": ".$row->other_reason."</td>";
}else{
echo "<td class='text-left'>".$rsn->reason."</td>";
}
}
foreach ($source_id as $src) {
echo "<td>".$src->source."</td>";
}
?>
<td><?php
$folder = $row->complaint_id;
$path_root = "upload/".$folder."/";
$dir = scandir($path_root, 1);
$array_lenght = count($dir)-2;
if($array_lenght>=1){?>
<span class="glyphicon glyphicon-paperclip"></span>
<?php } ?></td>
</tr>
<?php }?>
</table>
</div><!--End of body panel-->
<div class="panel-footer text-center">
</div><!--End of Panel Footer-->
</div><!--End of Panel-->
<div class="col-sm-12 text-center">
<?= $this->pagination->create_links() ?>
</div> <!-- End of pagination -->
</div>
Here is an image of my view:
15 records instead of 5
UPDATE:
I realize my mistake:
$data['query'] = $this->searchCases->getCases(); //I deleted this line, because i was getting all the results of the query.
and now in the view I replace $results instead $query in the foreach cycle and now I can see the 5 results per page that I'm asking for.
foreach ($query as $row) {?>
<tr>...
Changed to:
foreach ($results as $row) {?>
<tr>...
But now when I click on the link for the second page I have the same error 404 that I had from the start.
Try this $config['base_url'] = base_url().'index.php/opened_cases/index';
And change this $config['uri_segment'] = 3;

Best method to add pagination to a gallery

I have clients website that is getting overloaded with images in his gallery. I was wondering if I could get some advice and see what you guys/girls think would be the best way to handle this current situation I'm in.
http://www.richsdockcompany.com/Gallery.php
This gallery is created by php and mysql. I would like to set a limit to 12 images then it would switch to a different page(s), but it can't refresh the page or else the gallery will reset.
Current Code For Gallery
<?php
include_once "header.php";
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$images = mysql_query("SELECT * FROM images");
while ($image=mysql_fetch_assoc($images))
?>
<div id="Wrap">
<div class="Titles"><h2 style="font-size:36px;">Rich's Dock Company Image Gallery</h2></div><br />
<hr />
<div id="PhotoBoxWrap">
<!--======START GALLERY======-->
<div class="row">
<div class="column grid_12">
<div class="row">
<div class="column grid_12">
<!-- start Filter categories -->
<ul id="filter">
<li class="active">All</li>
<li>Dock Builders On Shore</li>
<li>Commercial Docks</li>
<li>Residential Docks</li>
<li>Dock Repairs & Additions</li>
<li>Barge Life</li>
</ul>
<!-- End Filter categories -->
</div>
</div>
<!-- Divider -->
<div class="row">
<div class="column grid_12">
<div class="clear"></div>
<div class="divider spacer5"></div>
</div>
</div>
<!-- End divider -->
<div class="row">
<ul id="stage" class="portfolio-4column">
<?php
$images = mysql_query("SELECT * FROM images ORDER BY id DESC");
while ($image=mysql_fetch_array($images))
{
?>
<li data-id="id-<?=$image["id"] ?>" data-type="<?=$image["data_type"] ?>">
<div class="column grid_3 gallerybox">
<a class="fancybox" rel="<?=$image["data_type"] ?>" href="images/gallery/<?=$image["file_name"] ?>" title="<?=$image["title"] ?>">
<img src="images/gallery/<?=$image["file_name"] ?>" alt="<?=$image["title"] ?>" class="max-img-border"></a>
<h4 style="color:#2B368D; text-align:center;"><?=$image["title"] ?></h4>
<p style="text-align:center; font-size:15px;"><?=$image["description"] ?></p>
</div>
</li>
<?php
}
?>
</ul><!--END LIST-->
The only thing I can think of off the top of my head would be to create a slider that would contain all the images or use ajax with pagination so there would be no refresh problem.
I have never attempted pagination so please go easy on me here.
Any advice would be appreciated.
Thanks!
look at this example code, which handles the pagination in a simple way.
You can reuse the function getPagesNavi for every list which should be paginated.
It returns the html with the links to navigate through the pages.
If you like to load the pages with ajax you need to do some modifications by yourself. This is only an example to show you how it could work.
$page = intval($_GET['page']);
$myurl = 'index.php?action=list';
$db->select("select * from tablename");
$count_total = $db->getRecords();
$items_per_page = 10;
$start = $page * $items_per_page;
$limit = "limit $start, $items_per_page";
$db->select("select * from tablename $limit");
while($row = $db->fetchArray()) {
// your output here...
}
echo getPageNavi($myurl,$page,$count_total,$items_per_page);
function getPagesNavi($link, $current_page, $count_total, $items_per_page, $number_of_visible_pagelinks_updown = 5, $page_varname = "page") {
$result = "";
if ($count_total <= 0) {
return "";
}
$pages_float = $count_total / $items_per_page;
$number_of_pages = ceil($pages_float) - 1;
$start = $current_page - $number_of_visible_pagelinks_updown;
$end = $current_page + $number_of_visible_pagelinks_updown;
if ($end > $number_of_pages) {
$dif = -$number_of_pages + $end;
$end = $number_of_pages;
$start = $start - $dif;
}
if ($start < 0) {
$dif = -$start;
$end = $end + $dif;
$start = 0;
}
if ($end > $number_of_pages) {
$end = $number_of_pages;
}
$back = $current_page - 1;
$forward = $current_page + 1;
if ($current_page > 0) {
$result .= "
<span class=\"pageItem\"><<</span>
<span class=\"pageItem\"><</span>";
} else {
$result .= "<span class=\"pageItem\"><<</span>";
$result .= "<span class=\"pageItem\"><</span>";
}
for ($i = floor($start); $i <= floor($end); $i++) {
$j = $i + 1;
$class = "";
if ($i == $current_page) {
$class = " currentPageItem";
}
$result.= "<span class=\"pageItem$class\">$j</span>";
}
if ($current_page != $number_of_pages) {
$result .= "<span class=\"pageItem\">></span>";
$result .= "<span class=\"pageItem\">>></span>";
} else {
$result .= "<span class=\"pageItem\">></span>";
$result .= "<span class=\"pageItem\">>></span>";
}
return $result;
}

Categories