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>
I have do a pagination with php&html,the problem is my $page is always equal 1 even I click another page and the url is already become ?page=2.The value $p cannot pass as $page.How can I solve this?
<ul class="nospace clear" style="width:1310px;">
<?php
if(isset($_GET['page'])&& $_GET['page']!=""){$page = $_GET["page"];}else{$page=1;}
$current=$page;
$end=12;
if($page=1){$start=0;$previous=$page;$next=$page+1;}
else if($page<=12){$start=$page*12-12;$previous=$page-1;$next=$page+1;}
else{$start=0;$previous=$page-1;$next=12;}
$sql = "Select * from item where Gender='women' AND Category='cloth' LIMIT $start,$end";
$result = mysqli_query($connect,$sql);
while($row=mysqli_fetch_assoc($result)){?>
<img src="../images/demo/<?php echo $row["Pic"]; ?>" style="width:300px;height:280px"><br><p></p><h3><strong><?php echo $row["Name"]; ?></strong></h3><p><?php echo $row["Description"]; ?></p></li>
<?php } ?></ul><figcaption>Page <?php echo"$page ";?> end...</figcaption>
</figure>
</div>
<nav class="pagination">
<ul>
<li>« Previous</li>
<?php
for ($p=1;$p<13;$p++){
if ($page == $p) {?>
<li class="current btn1"><strong><?php echo $p ?></strong></li><?php }
else{?><li><a href="?page=<?php echo $p ?>" class='btn1'><?php echo $p ?></a></li><?php }}?>
<li> Next » </li>
</ul>
I have listing in my HTML which is displayed from database. My listing code is like below:
<?php
require('admin/db_config.php');
$sql = "SELECT * FROM image_gallery";
$images = $mysqli->query($sql);
while($image = $images->fetch_assoc()){
?>
<div class="aamir"><span><img style="height:40px; width:55px; " class="img-responsive" alt="" src="admin/uploads/<?php echo $image['image'] ?>" /></span><small><?php echo $image['title']; ?></small><strong style="width:40%; height: 35%;"><em class="icon icon-chevron-down"></em><p style="margin-top: -5%;"> <?php echo $image['description']; ?> </p></strong>
<a
class="zayan" target="_blank" href="<?php echo $image['url']; ?>">VISIT</a>
</div>
<?php } ?>
Now I have given pagination for the same because the data is too much and I want it to go to next page. So I have given pagination. Pagination code is below:
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$conn=mysqli_connect("localhost","root","","sample");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$total_pages_sql = "SELECT COUNT(*) FROM image_gallery";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
$sql = "SELECT * FROM image_gallery LIMIT $offset, $no_of_records_per_page";
$res_data = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($res_data)){
//here goes the data
}
mysqli_close($conn);
?>
<ul class="pagination">
<li>First</li>
<li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
Prev
</li>
<li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
Next
</li>
<li>Last</li>
</ul>
But still the pagination is not working, no error is shown, the whole data is being displayed in one page and the pagination is like static.
I'm using bootstrap pagination, and a combination PHP/mysql method of displaying my results, (ex. results.php?results=1 for each page). This is working fine, but actually displaying the pagination div at the bottom of my page, i.e. which one is active, whether or not you can click the following or preceding items (if there actually are results on those pages)
So I've done a specific case for just about every combination I could think of that would affect anything, but it is certainly not efficient. Can anyone suggest a better way to do it? Some of the cases will be obvious, like if I'm on page one, I don't want to show page 0 and -1 in the pagination, others are less obvious, like if I'm on page 3, and there are no more pages, I want page 3 to be in the middle, as there are 5 numbers displayed, but you should not be able to click on 4 and 5.
I'm using an exact replica of the plugin showed here except I've added left and right increase / decrease by one in addition to the first / last page.
<div id="pagination" style="width: 340px; margin-left: auto; margin-right: auto;">
<? if ($results == 1) { ?>
<ul class="pagination">
<li class="disabled"> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li class="disabled"> <i class="fa fa-lg fa-angle-left"></i> </li>
<li class="active"><? echo $results; ?> <span class="sr-only">(current)</span></li>
<li <?if ($num_rows < $num_res) { echo "class=\"disabled\""; } ?>><? echo $results + 1; ?></li>
<li <?if ($num_rows < ($num_res * 2)) { echo "class=\"disabled\""; } ?>><? echo $results + 2; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><? echo $results + 3; ?></li>
<li <?if ($num_rows < ($num_res * 4)) { echo "class=\"disabled\""; } ?>><? echo $results + 4; ?></li>
<li <?if ($num_rows < ($num_res)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-right"></i> </li>
<li <?if ($num_rows < ($num_res)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
<? if ($results == 2) { ?>
<ul class="pagination">
<li> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li> <i class="fa fa-lg fa-angle-left"></i> </li>
<li><? echo $results - 1; ?></li>
<li class="active"><? echo $results; ?> <span class="sr-only">(current)</span></li>
<li <?if ($num_rows < ($num_res * 2)) { echo "class=\"disabled\""; } ?>><? echo $results + 1; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><? echo $results + 2; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><? echo $results + 3; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-right"></i> </li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
<? if (($results == 3) && (($last_page == 3))) { ?>
<ul class="pagination">
<li> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li> <i class="fa fa-lg fa-angle-left"></i> </li>
<li><? echo $results - 2; ?> </li>
<li><? echo $results - 1; ?></li>
<li class="active"><? echo $results; ?><span class="sr-only">(current)</span></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><? echo $results + 1; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><? echo $results + 2; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-right"></i> </li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
<? if (($results > 2) && ($results < ($last_page - 1))) { ?>
<ul class="pagination">
<li> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li> <i class="fa fa-lg fa-angle-left"></i> </li>
<li><? echo $results - 2; ?> </li>
<li><? echo $results - 1; ?></li>
<li class="active"><? echo $results; ?><span class="sr-only">(current)</span></li>
<li><? echo $results + 1; ?></li>
<li><? echo $results + 2; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-right"></i> </li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
<? if (($results > 2) && ($results == ($last_page - 1))) { ?>
<ul class="pagination">
<li> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li> <i class="fa fa-lg fa-angle-left"></i> </li>
<li><? echo $results - 3; ?></li>
<li><? echo $results - 2; ?></li>
<li><? echo $results - 1; ?></li>
<li class="active"><? echo $results ?><span class="sr-only">(current)</span></li>
<li><? echo $results + 1; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-right"></i> </li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
<? if (($results > 2) && ($results == $last_page) && ($results != 3)){ ?>
<ul class="pagination">
<li> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li> <i class="fa fa-lg fa-angle-left"></i> </li>
<li><? echo $results - 4; ?> </li>
<li><? echo $results - 3; ?></li>
<li><? echo $results - 2; ?></li>
<li><? echo $results - 1; ?></li>
<li class="active"><? echo $results ?><span class="sr-only">(current)</span></li>
<li class="disabled"> <i class="fa fa-lg fa-angle-right"></i> </li>
<li class="disabled"> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
</div>
If no one feels like helping with the first portion, I understand, it works as it is as far as I can tell.
But I also am wondering if there's a way that on my class="disabled", I can remove the hrefs? Or do I just have to put an if statement on every one? Thanks.
Edit: Based on #Scopey's recommendations. I changed a few things as I saw fit, as well as a couple variable names.
$numberOfPages = ceil($num_rows / $num_res);
$numberOfPages = (int)$numberOfPages;
$url = explode('&', $_SERVER['REQUEST_URI']);
function myFilter($string) {
return strpos($string, 'results=') === false;
}
$url = array_filter($url, 'myFilter');
$url = implode('&', $url);
?>
<ul class="pagination">
<li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
echo $url.'&results=' . 1;
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-double-left"></i> </a></li>
<li<?php if($results === 1): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== 1){
echo $url.'&results=' . ($results - 1);
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-left"></i> </a></li>
<?php
// Print the pagination...
// Minimum of 5 pages in the pagination, even if there aren't 5 pages...
$pageCount = ($numberOfPages < 5) ? 5 : $numberOfPages;
// Loop through from page 1 until the last page ($pageCount)
for($i = 1; $i <= $pageCount; $i++)
{
// Echo out just the beginning of the <li> tag as we don't yet
// know if this needs to be disabled...
echo '<li';
// Added to show if current page is active
if($i === $results) {
echo ' class="active"';
}
// Check if:
// - This current page is greater than the amount of pages of
// results we have
// - OR, this is the currently selected page
if($i > $numberOfPages || $results === $i)
{
// <li> tag needs the disabled class....
echo ' class="disabled"';
}
// Finish the <li> tag and start generating the link
echo '><a href="';
// Opposite logic from above... Only if this is a page we know about
// AND this is not the current page.
if($i < $numberOfPages && $results !== $i)
{
// The link will be your page, with the get param, and the
// current page number we're printing
echo $url.'&results=' . $i;
}
else
{
// Otherwise just a # (no link)
echo '#';
}
// Finish this page... Print the page number ($i)
echo '">' . $i . '</a></li>';
}
?>
<li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
echo $url.'&results=' . ($results + 1);
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-right"></i> </a></li>
<li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
echo $url.'&results=' . $numberOfPages;
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-double-right"></i> </a></li>
<?php
#Scobey, this works great, as intended, and same as my original post, except for a couple things. In my example, I was able to get the current page in the middle of the . For example, << 1 | 2 | 3 | 4 | 5 >>, that's why I had so many different statements with +1 and -1, +2, -2 etc.
Second, my intention was to display 5 page options at all times, not just a minimum of five. So if I was on page 6, I would only see << 4 | 5 | 6 | 7 | 8 >>, not 1-8. It may not be necessary to do it this way, it's a nice to have. But, I don't want to get to a point where I have over 50 different page numbers in the pagination.
Generally dynamic pagination works only when you have a few things
A count of all possible results (total)
An amount of results you want to show per page
The current page number
Information about the results that should appear on this page.
Imagine that you have a database table called Results that has a whole bunch of results you want on your paginated page
1. Count of all possible results
This is achievable with a simple SQL statement like:
SELECT count(*) FROM Results
2. Amount per page
This is a value you get to decide yourself.
3. The current page number
This is usually 1 by default, but when the pagination is changed, it gets passed through as a get parameter.
4. A slice of results from the table
Achievable with:
SELECT * FROM Results LIMIT 0, 20
This example retrieves the first 20 results in our results table...
5. The pagination
Now, using 20 results as a default "count per page"
$countPerPage = 20;
// Get the total number of results
$result = pg_query('SELECT count(*) FROM Results'); // I'm using PostgreSQL for this example
$totalResultCount = (int)pg_fetch_result($result, 0, 0);
// The ceil function will round floats up.
$numberOfPages = ceil($totalResultCount / $countPerPage);
// Check if we have a page number in the _GET parameters
if(!empty($_GET) && isset($_GET['page']))
{
$page = (int)$_GET['page'];
}
else
{
$page = 1;
}
// Check that the page is within our bounds
if($page < 0)
{
$page = 1;
}
elseif($page > $numberOfPages)
{
$page = $numberOfPages;
}
// Build the query for the results...
$query = 'SELECT * FROM Results LIMIT ' . ($page - 1) * $countPerPage . ', ' . $countPerPage;
$results = pg_fetch_all(pg_query($query));
// Deal with printing your results etc...
?>
<ul class="pagination">
<li<?php if($page === 1): ?> class="disabled"<?php endif; ?>><a href="<?php
if($page !== 1){
echo 'page.php?page=' . $page - 1;
} else { echo '#'; }
?>">«</a></li>
<?php
// Print the pagination...
// Minimum of 5 pages in the pagination, even if there aren't 5 pages...
$pageCount = ($numberOfPages < 5) ? 5 : $numberOfPages;
// Loop through from page 1 until the last page ($pageCount)
for($i = 1; $i < $pageCount; $i++)
{
// Echo out just the beginning of the <li> tag as we don't yet
// know if this needs to be disabled...
echo '<li';
// Check if:
// - This current page is greater than the amount of pages of
// results we have
// - OR, this is the currently selected page
if($i > $numberOfPages || $page === $i)
{
// <li> tag needs the disabled class....
echo ' class="disabled"';
}
// Finish the <li> tag and start generating the link
echo '><a href="';
// Opposite logic from above... Only if this is a page we know about
// AND this is not the current page.
if($i < $numberOfPages && $page !== $i)
{
// The link will be your page, with the get param, and the
// current page number we're printing
echo 'page.php?page=' . $i;
}
else
{
// Otherwise just a # (no link)
echo '#';
}
// Finish this page... Print the page number ($i)
echo '">' . $i . '</a></li>';
}
?>
<ul class="pagination">
<li<?php if($page === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($page !== $numberOfPages){
echo 'page.php?page=' . $page + 1;
} else { echo '#'; }
?>">»</a></li>
<?php
Disclaimer
I just sat here and wrote this, I have not tested it, but the general idea is there. Hopefully it makes sense. I'm running out of time and don't have time to test or improve it, but if you're interested I might fix it up later.
Hope this helps!
I use below code for pagination.How to add first,previous,next,last links in pagination.
Only 9 paging numbers should allow in pagination.
EX : First , Previous . ( 9 paging numbers) ,Next ,Last
I have attached image for reference.
<?php
$page = $_GET['url_page'];
$limit =5;
if($page==""){
$page =1;
$start_limit =0;
$end_limit = $page * $limit;
}
else {
$end_limit = $page * $limit;
$start_limit =$end_limit - $limit;
}
$array_count_res = count($result_array);
$choice = ceil($array_count_res /$limit);
$previous_page = $page-1;
$next_page = $page+1;
<?php if($page !=1) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $previous_page; ?>">
Previous</a>
<?php } ?>
<?php for($pa = 0 ;$pa < $choice;$pa++){ ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $pa+1; ?>">
<?php echo $pa+1;echo " "; ?></a>
<?php } ?>
<?php if($page !=$choice) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $next_page; ?>">
Next</a>
<?php } ?>
<?php
for($m=$start_limit;$m < $end_limit;$m++) {
if($result_array[$m]['name'] !="") {
?>
finaly i got answer
<?php
$page = $_GET['url_page'];
$limit =25;
if($page==""){
$page =1;
$start_limit =0;
$end_limit = $page * $limit;
}
else {
$end_limit = $page * $limit;
$start_limit =$end_limit - $limit;
}
$array_count_res = count($contacts);
$choice = ceil($array_count_res /$limit);
$previous_page = $page-1;
$next_page = $page+1;
$first_paging = $page - 2;
$second_paging = $page - 1;
$third_paging = $page + 1;
$four_paging = $page + 2;
$last_page =$choice -$page;
for($m=$start_limit;$m < $end_limit;$m++) {
?>
<tr><td> data</td></tr>
<?php } ?>
<!---pagination starts----->
<tr><td id="importcaontact_page" >
<?php if($page > 3) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/1">
«First</a>
<?php } ?>
<?php if($page !=1) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $previous_page; ?>">
«Previous</a>
<?php } ?>
<?php if($first_paging > 0 ) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $first_paging; ?>">
<?php echo $first_paging; ?></a>
<?php } ?>
<?php if($second_paging > 0 ) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?><?php echo $second_paging; ?>">
<?php echo $second_paging; ?></a>
<?php } ?>
<?php echo $page; ?>
<?php if($third_paging <= $choice ) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $third_paging; ?>">
<?php echo $third_paging; ?></a>
<?php } ?>
<?php if($four_paging <= $choice ) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $four_paging; ?>">
<?php echo $four_paging; ?></a>
<?php } ?>
<?php if($page !=$choice) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $next_page; ?>">
Next »</a>
<?php } ?>
<?php if($last_page > 1) { ?>
<a href="<?php echo $_SERVER['PHP_SELF'];?>/<?php echo $choice; ?>">
Last »</a>
<?php } ?>
</td></tr>