pagination are not working on same page? - php

index.php:
<script>
$(document).ready(function(){
$("#submit2").click(function(){
course = $("#courses").val();
field2 = $("#field2").val();
$("#imagen").show();
$.ajax({
type:"POST",
data:{"courses":course,"field2":field2},
url:"college.php",
success:function(data){
$("#imagen").hide();
$("#popular_colleges").html(data);
}
});
});
});
</script>
<img id="imagen" src="please.gif">
<div id ="popular_colleges"></div>
college.php
<?php
$course = $_POST['courses'];
$field2 = $_POST['field2'];
$per_page=10;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1) * $per_page;
$sql = "select * from college where course like '%,$course,%' LIMIT $start_from, $per_page";
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_array($result))
{
echo $row['logo'];
echo $row['collegename'];
echo $row['stream'];
}
?>
<?php
$query = "select * from colleges";
$result = mysqli_query($link, $query);
$total_records = mysqli_num_rows($result);
$total_pages = ceil($total_records / $per_page);
echo "<center><a href='index.php?page=1' style='padding:10px;'>".'First Page'."</a>";
$skipped = false;
for ($i = 1; $i <= $total_pages; $i++) {
if ($i < 3 || $total_pages- $i < 3 || abs($page - $i) < 3) {
if ($skipped)
echo '<span> ... </span>';
$skipped = false;
echo "<a href='index.php?page=" . $i . "' style='padding:5px;'>" . $i . "</a>";
} else {
$skipped = true;
}
}
echo "<a href='index.php?page=$total_pages' style='padding:10px;'>".'Last Page'."</a></center>";
?>
In this code when I click on submit2 button it showing pagination but when I click on any pagination number it will reload the same page but I want that when clicking on pagination number don't load the page and showing the result on the same page.

index.php?page=, but you are not doing anything with page variable in index.php. As I can see you are using it in college.php, but college.php doesn't receive page variable.
You are sending a POST request and trying to get page from $_GET. You can only have one verb (POST, GET, PUT, ...) when doing an HTTP Request.
You are sending data through AJAX to college.php.
data:{"courses":course,"field2":field2}
As you can see, you are not sending page. You need to send page as well.
data:{"courses":course,"field2":field2, "page": page}
And use $_POST['page'] instead of $_GET['page']

Related

url generate multiple time in pagination

My pagination is working good .But problem is that when I click on pagination then its does not generate new link .Its add a page id with old link .Like
videos.php?page=2page=3 and if again i cliked on 4th number pagination .Its show like this videos.php?page=2page=3page=4.
<?php
$limit = 20;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "$que LIMIT $start_from, $limit";
$rs_result = mysql_query($sql);
$result = mysql_query($que);
$total_bookss = mysql_num_rows($result);
$full_linkp = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$full_linkEx=explode('&page',$full_linkp);
$full_link=$full_linkEx[0];
if($total_bookss>$limit){
$total_records = $total_bookss;
$total_pages = ceil($total_records / $limit);
$pagLink = "<nav><ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
//$pagLink .= "<li><a href='$full_link.php&page=". $i."'>".$i."</a></li>";
$pagLink .= "<li><a href='$full_link&page=".$i."'>".$i."</a></li>";
}
//show pagination variable
$show_pagination=$pagLink . "</ul></nav>";}
?>
<script>
jq(document).ready(function(){
jq('.pagination').pagination({
items: <?php echo $total_records;?>,
itemsOnPage: <?php echo $limit;?>,
cssStyle: 'light-theme',
currentPage : <?php echo $page;?>,
hrefTextPrefix : '<?=$full_link?>page='
});
});
</script>
Try that
<?php
$limit = 20;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "$que LIMIT $start_from, $limit";
$rs_result = mysql_query($sql);
$result = mysql_query($que);
$total_bookss = mysql_num_rows($result);
$full_linkp = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$full_linkEx=explode('?page',$full_linkp);
$full_link=$full_linkEx[0];
if($total_bookss>$limit){
$total_records = $total_bookss;
$total_pages = ceil($total_records / $limit);
$pagLink = "<nav><ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
//$pagLink .= "<li><a href='$full_link.php&page=". $i."'>".$i."</a></li>";
$pagLink .= "<li><a href='$full_link?page=".$i."'>".$i."</a></li>";
}
//show pagination variable
$show_pagination=$pagLink . "</ul></nav>";}
?>
<script>
jq(document).ready(function(){
jq('.pagination').pagination({
items: <?php echo $total_records;?>,
itemsOnPage: <?php echo $limit;?>,
cssStyle: 'light-theme',
currentPage : <?php echo $page;?>,
hrefTextPrefix : '<?=$full_link?>?page='
});
});
</script>

How to do pagination in PHP?

I'm trying to do pagination in PHP, everything seems to be working, except that next and previous links don't work, it's only when I manually insert the page number in the URL that it displays data from the database on the next page.
Here is my code:
This is where I initialised the perpage and page. These are at the beginning of the page.
<?php
$per_page=4;
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page=1;
}
$page;
echo $start_from = ($page-1) * $per_page;
//$search = $_POST['search'];
?>
And this is for the next and previous links, those ones that display the results depending on what the user wants to see.
<?php
$query = "select * from services";
$result = mysqli_query($link, $query);
$total_records = mysqli_num_rows($result);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
$prev = $page - 1;
if($page == 1){
echo "<span align='right' class='inactive'>← Prev</span>";
}else{
echo "<a href='livebands.php?page=$prev'><span class='paging-prev'>".'← Prev'."</span></a>";
}
for ($i=1; $i<=2; $i++) {
for ($i=1; $i<=$page; $i++) {
echo "<a href='livebands.php?page=$i'><span class='paging'>" .$i. "</span></a>";
}
}
$page++;
if ($page>$total_pages){
echo "<span align='right' class='inactive'>→ Next</span>";
}else{
echo "<a href='livebands.php?page=$page&per_page=$per_page'><span align='right' class='paging-next'>".'Next →'."</span></a>";
}
?>
If I use you code and hardcode the $total_records variable to 5 for example, the links seem to work.
// $query = "select * from services";
// $result = mysqli_query($link, $query);
// $total_records = mysqli_num_rows($result);
$total_records = 5;
Are you sure that your $total_records is more than 4?

Remove id from url

I have few records in MySQL DB table.
Displayed those records with pagination PREV and NEXT.
Page ids are displaying in url bar.
http://localhost/pagination.php?page=5
I don't want them while pagination clicking.
How can i do this?
Here is my code.
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("moodle");
$per_page = 10;
$pages_query = mysql_query("SELECT COUNT('id') FROM question");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysql_query("SELECT * FROM question LIMIT $start, $per_page");
while($query_row = mysql_fetch_assoc($query)){
echo $query_row['id']."<br />";
}
$prev = $page - 1;
$next = $page + 1;
echo "<a href='pagination.php?page=$prev'>Prev</a> ";
if($pages >= 1){
for($x=1; $x<=$pages; $x++){
echo ''.$x.' ';
}
}
echo "<a href='pagination.php?page=$next'>Next</a> ";
?>
Use id in session and don't display it in url . other method is to use Ajax call in pagination on click of previous or next button . i think these are two simple solutions for you that you can easily manage .

Pagination All from sql and Pagination of Alphabetical navigate

I used Pagination in php with mysqli and Alphabetical navigator in same page. They are working well. But if I click on any Alphabet like: B Its display 10 sql table's data with B and when I click page 2 to display rest of data Alphabet B, Its cannot display rest of data B but display next 10 data of all sql table's.
I have Total 100 row in sql where for A=20, b=30, c=5, d=15 ... etc. I used Pagination for display 10 data at a time And Alphabetical navigator to display data as Alphabet.
Now I want:
When user load my page, Pagination work for all data by name ASC limit 10, But when user click on any Alphabet, Pagination work for this Alphabet.
Here is my code:
<?php
include_once('db.php');
//for pagination
$limit = 10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
//for Alphabet
$sort = isset($_GET['firstLetter']) ? filter_input(INPUT_GET, 'firstLetter',FILTER_SANITIZE_URL) : "" ;
if($sort == "") {
$sql = "SELECT * FROM evideo ORDER BY name ASC LIMIT $start_from, $limit";
}else{
$sql = "SELECT * FROM evideo WHERE Name LIKE '$sort%' ORDER BY name ASC LIMIT $start_from, $limit" ;
}
$execute = $dbh->query("$sql");
//Display Alphabet
echo '<div class="well abc-pag"><b>Find Alphabetically:</b> ';
for ($i = 65; $i < 91; $i++) {
printf('%s ', $_SERVER['PHP_SELF'] , chr($i), chr($i));
}
printf('ALL', $_SERVER['PHP_SELF'] );
echo "</div>";
$rowcount = $execute->num_rows ;
$c = 1;
if ($rowcount > 0 ) {
$row = $dbh->query($sql) ;
while ($row = $execute->fetch_assoc()) {
$name = $row['name'];
$Detail = $row['Detail'];
$link = $row['link'];
$pic = $row['pic'];
if (empty($pic)) $pic = "../images/edir.jpg";
echo '<article class="white-panel"><a href="'.$link.'">';
echo '<img src="images/loader.gif" border="0" data-echo="'.$pic.'" class="emusicpro img-responsive"></a>';
echo '<div class="well"><strong>'.$name.'</strong><br>'.$Detail.'</div>';
echo '</article>';
}
} else {echo '<p align="center"><b>No Data Found.</b></p>';}
//Display pagination
$sql = "SELECT COUNT(id) FROM evideo";
$rs_result = mysqli_query($dbh,$sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / $limit);
$pagLink = "<ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li><a href='ebox2.php?page=".$i."'>".$i."</a></li>";
};
echo $pagLink . "</ul>";

Previous/Next Navigation issues with loading navigation links

I have used this code before with no issues but... now I can't seem to load the page request when pressing Previous/Next Navigation if you can see where I went wrong be a help.
<?php
include 'config.php';
$db = mysql_connect($host, $username, $password);
mysql_select_db($db_name,$db);
$sql = "SELECT u_id FROM users";
$query = mysql_query($sql,$db);
$total_results = mysql_num_rows($query);
$limit = "5"; //limit of archived results per page.
$total_pages = ceil($total_results / $limit); //total number of pages
if (empty($page))
{
$page = "1"; //default page if none is selected
}
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB
$query = "SELECT * FROM users ORDER BY u_id LIMIT $offset, $limit";
$result = mysql_query($query);
//This is the start of the normal results...
while ($row = mysql_fetch_array($result))
{
?>
<HR>
Username : <B><?echo $row['uname'] ?></B><BR>
user Message: <?echo $row['umess'] ?>
<BR>
<?
}
mysql_close();
// This is the Previous/Next Navigation stuff having issue with
echo "<font face=Verdana size=1>";
echo "Pages:($total_pages) "; // total pages
if ($page != 1)
{
echo "<a href=?&page=1><< First</a> "; // First Page Link
$prevpage = $page - 1;
echo " <a href=?page=$prevpage><<</a> "; // Previous Page Link
}
if ($page == $total_pages)
{
$to = $total_pages;
}
elseif ($page == $total_pages-1)
{
$to = $page+1;
}
elseif ($page == $total_pages-2)
{
$to = $page+2;
}
else
{
$to = $page+3;
}
if ($page == 1 || $page == 2 || $page == 3)
{
$from = 1;
}
else
{
$from = $page-3;
}
for ($i = $from; $i <= $to; $i++)
{
if ($i == $total_results) $to=$total_results;
if ($i != $page)
{
echo "<a href=?showold=yes&page=$i>$i</a>";
}
else
{
echo "<b><font face=Verdana size=2>[$i]</font></b>";
}
if ($i != $total_pages)
echo " ";
}
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo " <a href=?page=$nextpage>>></a> "; // Next Page Link
echo " <a href=?page=$total_pages>Last >></a>"; // Last Page Link
}
echo "</font>";
?>
After a test run you can see 1st page request is fine untill you click next or a page number it keeps going back to 1st page.
Instead of
if (empty($page))
{
$page = "1"; //default page if none is selected
}
Try :
if (isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = 1;
}

Categories