hello guys can you help me out ?
i have a problem in pagination
i just want to disable the next and previous
button in my pagination if it is
no item left
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "final";
$con= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysqli_select_db($con, "final") or die ("no database");
$pagination_sql = "SELECT * FROM `ongoing` WHERE approved='approve'";
$run_pagination = mysqli_query($con, $pagination_sql);
$count = mysqli_num_rows($run_pagination);
$total_pages = ceil($count/$per_page);
echo "<ul class='pagination'>";
echo "<li class='page-item'><a class='page-link' href='ongoing.php?page=".($page-1)."' class='button'>
<span aria-hidden='true'>«</span>
<span class='sr-only'>Previous</span>
</a></li>";
for($i=1;$i<=$total_pages;$i++){
echo'<li><a class="page-link" href="ongoing.php?page='.$i.'">'.$i.'</a></li>';
};
echo "<li class='page-item'><a class='page-link' href='ongoing.php?page=".($page+1)."' class='button'>
<span aria-hidden='true'>»</span>
<span class='sr-only'>Next</span>
</a></li>";
echo "</ul>";
?>
and here's the condition of per page
$per_page = 10;
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page= 1;
}
$start_from = ($page-1) * $per_page;
I think you just need to adjust your code as below:
echo "<ul class='pagination'>";
if($page == 1) {
$disable_prev = 'disabled';
$prev_url = "javascript:void(0);";
} else {
$disable_prev = '';
$prev_url = "ongoing.php?page=".($page-1);
}
echo "<li class='page-item ".$disable_prev."'><a class='page-link' href='".$prev_url."' class='button'>
<span aria-hidden='true'>«</span>
<span class='sr-only'>Previous</span>
</a></li>";
for($i=1;$i<=$total_pages;$i++){
echo'<li><a class="page-link" href="ongoing.php?page='.$i.'">'.$i.'</a></li>';
};
if($page+1 == $total_pages) {
$disable_next = '';
$next_url = "ongoing.php?page=".($page+1);
} else {
$disable_next = 'disabled';
$next_url = "javascript:void(0);";
}
echo "<li class='page-item ".$disable_next."'><a class='page-link' href='".$next_url."' class='button'>
<span aria-hidden='true'>»</span>
<span class='sr-only'>Next</span>
</a></li>";
echo "</ul>";
Then you can also add some CSS to make disabled li little visible or faded than other
Try:
if($i != NULL){ //NULL or Empty
//Code here
}
Related
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 am learning PHP and was trying to created dynamic pagination to show record form MySQL.
The issue I'm facing is when I change number of record to be show by using SELECT tag it only works once after that it goes back to default value that i have set -> $limit = isset($_POST['records-count'])?$_POST['records-count']:"10"; which is 10.
PHP
include('../config/DbFunction.php');
$obj = new DbFunction();
$limit = isset($_POST['records-count'])?$_POST['records-count']:"10";
$page = isset($_GET['page'])? $_GET['page']:"1";
$start_data = ($page-1)*$limit;
$rs = $obj->view_course($limit,$start_data);
//print_r($limit); // to check limit value
$course_row = mysqli_fetch_row($obj->view_course1());
$total_records = $course_row[0];
$total_page = ceil($total_records/$limit);
$next = $page + 1 > $total_page? $total_page : $page +1;
$prev = $page - 1 == 0? 1 : $page - 1;
?>
Form
<form method="post" action="#">
Records: <select name="records-count" id="records_count">
<option disabled="disabled" selected="selected">Limit</option>
<?php foreach([20,50,100,200] as $limit): ?>
<option <?php if( isset($_POST["records_count"]) && $_POST["records_count"] == $limit) echo "selected" ?> value="<?= $limit; ?>"><?= $limit; ?></option>
<?php endforeach; ?>
</select>
</form>
Pagination
<div class="pagination">
<ul class="pagination pagination-default">
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $prev; ?>">Previous</a></li>
<?php
for ($i=1; $i<=$total_page; $i++) {
echo "<li class='page-item'><a class='page-link' href='view-course.php?page=".$i."'>".$i."</a></li>";
}
?>
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $next; ?>">Next</a></li>
</ul>
</div>
you may need to store and retrieve it from session. Session will allow to store variable in server
you need to start the session before send anything to client.
session_start();
if(isset($_POST['records-count']))
$_SESSION["records-count"] = $_POST['records-count'];
last thing to note is:
session variable will be updated in next requests.
After so many try I got the solution by using GET method
So i Change
$limit = isset($_POST['records-count'])?$_POST['records-count']:"10";
<div class="pagination">
<ul class="pagination pagination-default">
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $prev; ?>">Previous</a></li>
<?php
for ($i=1; $i<=$total_page; $i++) {
echo "<li class='page-item'><a class='page-link' href='view-course.php?page=".$i."'>".$i."</a></li>";
}
?>
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $next; ?>">Next</a></li>
</ul>
</div>
to
if (isset($_POST['records-count'])) {
$limit = $_POST['records-count'];
} else {
$limit = empty($_GET['records'])? "2":$_GET['records'];
}
<div class="pagination">
<ul class="pagination pagination-default">
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $prev; ?>&records=<?= $limit; ?>">Previous</a></li>
<?php
for ($i=1; $i<=$total_page; $i++) {
echo "<li class='page-item'><a class='page-link' href='view-course.php?page=".$i."& records=".$limit."'>".$i."</a></li>";
}
?>
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $next; ?>&records=<?= $limit; ?>">Next</a></li>
</ul>
</div>
If anyone know more efficient or better way then this plz let me know.
I currently am having trouble using pagination. The first page is great, and the display on the second and third pages are correct, but the actual $_Get variables are not being carried over onto the second page and third page. Where would I put the actual $_Get Variables to go onto the next pages? Also I am using a form to pass the variables as a hidden input, that again work on the first page, but revert to empty on the second and third pages. I have read about decoupling and using sessions, but have tried implementing session variables at the top to no avail. How would I get the session to go to the second and third page. Thank you!
session_start();
include "dbhReal.inc.php";
include "header.php";
$timeSelected = $_GET['time'];
$dateSelected = $_GET['date'];
$ShownDate = date('M-d-y', strtotime("$dateSelected"));
$Null = '00:00:00';
<!--Form is below, left out the queries for space sake-->
echo '
<div class="col-sm-6">
<div class="card h-100">
<img class="card-img-top" src="../PhotoUploads/uploads/'.$userPic['Link1'].'" height="350" width="400" alt="anotherOne"/>
<div class="card-body">
<h5 class="card-title">'.$row['firstNameP'].' '.$row['lastNameP'].'</h5>
<p class="card-text">'.$row['briefDescription'].'</p>
<form action="settingTheSession.php" method="GET">
<input type="hidden" name="time" value='.$timeSelected.'>
<input type="hidden" name="date" value='.$dateSelected.'>
<input type="hidden" name="idNumber" value='.$boookingNumber.'>
<button type="submit" class="btn btn-primary" name="buttonBookprofileSearch">See Profile</button>
</form>
</div>
</div>
</div>';
<!--Pagnation-->
<ul class="pagination justify-content-center">
<li class="page-item" <?php if($page_no <= 1){ echo "class='page-item disabled'"; } ?>>
<a class='page-link' <?php if($page_no > 1){ echo "href='?page_no=$previous_page'"; } ?>>Previous</a>
</li>
<?php
if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'>$counter</a></li>";
}else{
echo "<li class='page-item'><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
}
elseif($total_no_of_pages > 10){
if($page_no <= 4) {
for ($counter = 1; $counter < 8; $counter++){
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'>$counter</a></li>";
}else{
echo "<li class='page-item' ><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
echo "<li class='page-item'><a>...</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$second_last'>$second_last</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$total_no_of_pages'>$total_no_of_pages</a></li>";
}
elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {
echo "<li class='page-item'><a class='page-link' href='?page_no=1'>1</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=2'>2</a></li>";
echo "<li class='page-item'><a class='page-link'>...</a></li>";
for ($counter = $page_no - $adjacents; $counter <= $page_no + $adjacents; $counter++) {
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'counter</a></li>";
}else{
echo "<li class='page-item'><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
echo "<li class='page-item'><a class='page-link'>...</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$second_last'>$second_last</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$total_no_of_pages'>$total_no_of_pages</a></li>";
}
else {
echo "<li class='page-item'><a class='page-link' href='?page_no=1'>1</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=2'>2</a></li>";
echo "<li class='page-item'><a class='page-link'>...</a></li>";
for ($counter = $total_no_of_pages - 6; $counter <= $total_no_of_pages; $counter++) {
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'>$counter</a></li>";
}else{
echo "<li class='page-item'><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
}
}
?>
<li class="page-item" <?php if($page_no >= $total_no_of_pages){ echo "class='disabled'"; } ?>>
<a class="page-link" <?php if($page_no < $total_no_of_pages) { echo "href='?page_no=$next_page'"; } ?>>Next</a>
</li>
<?php if($page_no < $total_no_of_pages){
echo "<li class='page-item'><a class='page-link' href='?page_no=$total_no_of_pages'>Last ››</a></li>";
} ?>
</ul>
<div class="text-center">
<strong>Page <?php echo $page_no." of ".$total_no_of_pages; ?></strong></div>
<br />
At first try not to reinvent the wheel. There are a lot of packages out there which deal with pagination: https://packagist.org/packages/voku/pagination?query=pagination
Secondly, considering your question, as PHP is server side you need to pass the needed data to every page/url you navigate.
So like the following example from your code:
echo "<li class='page-item'><a class='page-link' href='?page_no=1'>1</a></li>";
You will need the add the wanted arguments to the href query string. Like you are already doing with the 'page_no' parameter.
echo "<li class='page-item'><a class='page-link' href='?page_no=1&date=". $dateSelected ."'>1</a></li>";
In that manner you will have access to the wanted arguments on the next pages.
Am new to php, i am try to use pagenation , where i post a unique id to a page then use that to load content with the respective id then pagenate all the content with that respective id.
Here below is what have been trying
<?php
if (isset($_GET["post"]))
{
$page = $_GET["post"];
} else
{
$page = 1;
};
$limit = 2;
$total_records = $pagination;
$total_pages = ceil($total_records/$limit);
$lastpage = ceil($total_pages/$limit);
$page != 0;
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
//lastpage is = total pages / items per page, rounded up.
if($prev == 0)
$prev = 1;
$next <= $lastpage;
//if no page var is given, default to 1.
$pagLink = "<ul class='pagination pagination-circle' class='justify- content-center'>";
$pagLink .= "<li class='page-item active'>
<a class='page-link black' href='home.php?post=".$prev."' aria-label='Back'>
<span aria-hidden='true'>«</span>
<span class='sr-only'>Next</span>
</a>
</li>";
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li class='page-item active'><a class='page-link' href='home.php?post=".$i."'>".$i."</a></li> ";
};
$pagLink .= "<li class='page-item pg-red active'>
<a class='page-link black' href='home.php?post=".$next."' aria-label='Next'>
<span aria-hidden='true'>»</span>
<span class='sr-only'>Next</span>
</a>
</li>";
echo $pagLink . "</ul>";
?>
Here is how i want to pagenate contents per id
enter image description here
hi im trying to create a movie website.i've done almost everything but.there's an issue.this is a part of the load_data.php file that displays the movie links from the db
$query_pag_num = "SELECT COUNT(*) AS count FROM videos";
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);
and this is where movies are displayed
<ul class="nav nav-tabs responsive" id="myTab">
<li class="active"><a class="deco-none misc-class" href="#videos">All</a></li>
<li>Action</li>
<li>Adventure</li>
<li>Animation</li>
<li>Horror</li>
<li>Crime</li>
<li>Comedy</li>
<li>Romance</li>
<li>Fantasy</li>
<li>Drama</li>
<li>Mystery</li>
<li><a class="deco-none" href="#thriller">Thriller</a></li>
<li class="dropdown">
<a data-toggle="dropdown" href="#">More <span class="caret"></span></a>
<ul class="dropdown-menu pull-right" role="menu" aria-labelledby="dLabel">
<li>War</li>
<li>Science-fiction</li>
<li>Family</li>
</ul>
</li>
</ul>
<div class="tab-content responsive">
<div class="tab-pane fade in active" id="<?php $table='videos'; echo $table ?>">
<div class="row row1">
<div style="margin-top:10px;"></div>
<div align="center" style="padding-bottom:80px" id="loading"></div>
<div id="container">
<div class="data"></div>
<div class="pagination"></div>
</div>
</div>
</div>
<div class="tab-pane fade in" id="action">
<div class="row row1">
</div>
</div>
movies are loaded by jquery & ajax.now everything is working fine because i have the table videos that contains all the movies and im displaying all data from the videos table but what i want to do is that onclick of every category to display the movies of that category.
this is the jquery&ajax that does the job
<script type="text/javascript">
$(document).ready(function(){
function loading_show(){
$('#loading').html("<i class='fa fa-cog fa-spin fa-5x'>
</i>").fadeIn('fast');
}
function loading_hide(){
$('#loading').fadeOut('fast');
}
function loadData(page){
loading_show();
$.ajax
({
type: "POST",
url: "load_data.php",
data: "page="+page,
success: function(msg)
{
$("#container").ajaxComplete(function(event, request,
settings)
{
loading_hide();
$("#container").html(msg);
});
}
});
}
loadData(1); // For first time page load default results
$('#container .pagination li.active').live('click',function(){
var page = $(this).attr('p');
loadData(page);
});
$('#go_btn').live('click',function(){
var page = parseInt($('.goto').val());
var no_of_pages = parseInt($('.total').attr('a'));
if(page != 0 && page <= no_of_pages){
loadData(page);
}else{
alert('Enter a PAGE between 1 and '+no_of_pages);
$('.goto').val("").focus();
return false;
}
});
});
</script>
this is the full load-data.php file
<?php
ob_start();
if($_POST['page'])
{
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 3;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
include"database.php";
function html2txt($document){
$search = array('#<script[^>]*?>.*?</script>#si','#<[\/\!]*?[^<>]*?>#si','#<style[^>]*?>.*?</style>#siU','#<![\s\S]*?--[ \t\n\r]*>#');
$text = preg_replace($search, '', $document);
return $text;
}
$query_pag_data = "SELECT * from videos order by id desc LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result_pag_data)) {
$msg .= "<div class='col-sm-6 col-md-4'><div class='thumbnail'><img class='img-responsive' src='images/".html2txt($row['cover'])."'/><div class='caption'><h3>" . html2txt($row['title']) . "</h3><a href='movies.php?path=".html2txt($row['path'])."&title=".html2txt($row['title'])."&description=".html2txt($row['description'])."&trailer_title=".html2txt($row['trailer_title'])."&trailer_url=".html2txt($row['trailer_url'])."&imdb=".html2txt($row['imdb'])."&category_list=videos' class='btn btn-primary' role='button'><i class='fa fa-play-circle fa-1x'></i> Shiko filmin</a></div></div></div>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data
/* --------------------------------------------- */
$query_pag_num = "SELECT COUNT(*) AS count FROM videos";
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);
/* ---------------Calculating the starting and endign values for the loop----------------------------------- */
if ($cur_page >= 7) {
$start_loop = $cur_page - 3;
if ($no_of_paginations > $cur_page + 3)
$end_loop = $cur_page + 3;
else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
$start_loop = $no_of_paginations - 6;
$end_loop = $no_of_paginations;
} else {
$end_loop = $no_of_paginations;
}
} else {
$start_loop = 1;
if ($no_of_paginations > 7)
$end_loop = 7;
else
$end_loop = $no_of_paginations;
}
/* ----------------------------------------------------------------------------------------------------------- */
$msg .= "<ul class='pagination'><ul>";
// FOR ENABLING THE FIRST BUTTON
if ($first_btn && $cur_page > 1) {
$msg .= "<li p='1' class='active'>First</li>";
} else if ($first_btn) {
$msg .= "<li p='1' class='inactive'>First</li>";
}
// FOR ENABLING THE PREVIOUS BUTTON
if ($previous_btn && $cur_page > 1) {
$pre = $cur_page - 1;
$msg .= "<li p='$pre' class='active previous'>«</li>";
} else if ($previous_btn) {
$msg .= "<li class='inactive'>«</li>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {
if ($cur_page == $i)
$msg .= "<li p='$i' class='active'>{$i}</li>";
else
$msg .= "<li p='$i' class='active'>{$i}</li>";
}
// TO ENABLE THE NEXT BUTTON
if ($next_btn && $cur_page < $no_of_paginations) {
$nex = $cur_page + 1;
$msg .= "<li p='$nex' class='active'>»</li>";
} else if ($next_btn) {
$msg .= "<li class='inactive'>»</li>";
}
// TO ENABLE THE END BUTTON
if ($last_btn && $cur_page < $no_of_paginations) {
$msg .= "<li p='$no_of_paginations' class='active'>Last</li>";
} else if ($last_btn) {
$msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>";
}
//$goto = "<input type='text' class='goto' size='1'/><input type='button' id='go_btn' class='go_button' value='Go'/>";
$total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>";
$msg = $msg . "</ul>"/* . $goto*/ . $total_string . "</div>"; // Content for pagination
echo $msg;
ob_end_flush();
}
You can pass second attribute in function like :-
$('#container .pagination li.active').live('click',function(){
var page = $(this).attr('p');
loadData(page, category_id);
});