url generate multiple time in pagination - php

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>

Related

php postgres SQL pagination

I need a pagination based on php/postgres.
With the code below, I can break de records but only shows the page 1 (link).
Any idea to fix this?
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 5;
$query = pg_query($dbconn,"select * from my table limit 5 offset 0") or die(pg_result_error($dbconn));
$total_query = pg_num_rows($query);
$total_pages = ceil($total_query / 5);
the query result:
while($row = pg_fetch_assoc($query)){
...
}
for the pagination:
for ($i=1; $i<=$total_pages; $i++) {
echo "".$i." ";
}
I don't understand your code very well but the basic logic for a pagination sql query is
Select * from pages limit $page_size offset $page_size*($page_no-1);
I've fix it.
The code:
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$recortds = 10; // change here for records per page
$start_from = ($page-1) * $records;
$qry = pg_query($dbconn,"select count(*) as total from table");
$row_sql = pg_fetch_row($qry);
$total_records = $row_sql[0];
$total_pages = ceil($total_records / $records);
$select = pg_query($dbconn,"select * from table limit $records offset $start_from");
the select result:
while($row = pg_fetch_assoc($select )){
echo $row['col1'].' | '.$row['col2'].' | '.$row['col3'].'<br />';
}
the pagination links:
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='index.php?page=".$i."' class='yourclass'>".$i."</a> ";
}

php pagination code is not working

Can anyone point me out why my pagination is not working, What I am doing wrong here ? Working on it for a long time. my url generates like : http://localhost/medapp/admin/medorder.php?page=%209.
<?php
//pagination
$perpage = 3;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1)*$perpage;
//pagination
$medorder = "SELECT * FROM `medorder` WHERE status='1' order by ID desc";
$result = $db->select($medorder);
if($result){
$i=0;
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$row["uid"]."</td>";
echo "<td>".$row["fullname"]."</td>";
echo "</tr>";
}
// pagination
$query = "select * from medorder";
$result = $db->select($query);
$total_rows = mysqli_num_rows($result);
$total_pages = ceil($total_rows/$perpage);
echo "<span class='pagination'><a href='medorder.php?page=1'>".'First Page'."</a>";
for ($i=1; $i <= $total_pages; $i++) {
echo "<a href='medorder.php?page=".$i."'>".$i."</a>"; }
echo "<a href='medorder.php?page=$total_pages'>".'Last Page'."</a></span>";
//pagination
}
?>
Change your query like this by using LIMIT and OFFSET
$medorder = "SELECT * FROM `medorder` WHERE status='1' order by ID desc LIMIT $start_from,$perpage"; //
in this following line, you have a space before the $i variable:
echo "<a href='medorder.php?page= ".$i."'>".$i."</a>";
should be
echo "<a href='medorder.php?page=".$i."'>".$i."</a>";

PHP pagination script not work for search

I used this code in my all pages. Everywhere this code work well. In case of my search page this code count all page by search words but when I click next(2nd or 3rd) page, its cannot display any item. That means sql query for 2nd or 3rd or any others pages not worked. No error also. Pagination page url goes well also.
My Search code:
if(isset($_POST['searchword'])){
$w = mysqli_real_escape_string($dbh, $_POST['searchword']);
$q = strip_tags($w);
$q = trim ($w);
$limit = 12;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM search_data WHERE MATCH(detail) AGAINST('+$q*' IN BOOLEAN MODE) ORDER BY id ASC LIMIT $start_from, $limit";
$execute = $dbh->query("$sql");
$rowcount = $execute->num_rows ;
if ($rowcount > 0 ) {
$row = $dbh->query($sql) ;
while ($row = $execute->fetch_assoc()) {
$detail = $row['detail'];
$url = $row['url'];
echo $detail;
}
}
}
//Pagination
$sql = "SELECT COUNT(id) FROM search_data WHERE MATCH(detail) AGAINST('+$q*' IN BOOLEAN MODE)";
$result = mysqli_query($dbh,$sql);
$row = mysqli_fetch_row($result);
$total_records = $row[0];
if($total_records > 0){$total_pages = ceil($total_records / $limit); }
$pagLink = "<ul class='pagination'>";
if(!empty($total_pages)){for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li><a href='".$thispage."?page=".$i."'>".$i."</a> </li>";
};
echo $pagLink . "</ul>";
}

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?

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>";

Categories