PHP pagination script not work for search - php

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

Related

PHP/SQL - pagination for fuzzy search results

I have created a search function for the products on my site and I tried to put in pagination so that it is not just a big long list of results. So I started off with something like this:
**Note: I just replaced $_GET['search_term'] with 'whatever' just for this example, and where I have var_dump() I have a function that displays the products for each id in the array it is given.
$term = 'whatever'; //$_GET['search_term'];
$new_term = '%'.$term.'%';
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page = 1;
}
$per_page = 20;
$last_page = ceil($resultCount/$per_page);
if($page<1){
$page = 1;
}else if($page>$last_page){
$page = $last_page;
}
$pagination = "";
$limit = "LIMIT ".($page-1)*$per_page.",".$per_page;
if($last_page!=1){
if($page!=1){
$prev = $page-1;
$pagination .= "<a class='pagination' href='store'><<</a>";
$pagination .= "<a class='pagination' href='store/$prev'><</a>";
}
for($i=$page-2; $i<=$page+2; $i++){
if($i>0 && $i<=$last_page){
if($i == $page){
$pagination .= "<a class='pagination selected'>$i</a>";
}else{
$pagination .= "<a class='pagination' href='store/$i'>$i</a>";
}
}
}
if($page!=$last_page){
$next = $page+1;
$pagination .= "<a class='pagination' href='store/$next'>></a>";
$pagination .= "<a class='pagination' href='store/$last_page'>>></a>";
}
}
if(isset($term)){
echo $pagination;
$ids = [];
$params = [$new_term];
$sql = "SELECT * FROM products WHERE name LIKE ? $limit";
$stmt = DB::run($sql,$params);
$resultCount = $stmt->rowCount();
if($resultCount > 0){
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$id = $row["pro_id"];
$params3 = [$id];
$sql3 = "SELECT * FROM products WHERE id=?";
$stmt3 = DB::run($sql3,$params3);
while($row = $stmt3->fetch(PDO::FETCH_ASSOC)){
$id = $row["id"];
array_push($ids,$id);
}
}
var_dump($ids);
}
echo $pagination;
}
This worked fine, but then I wanted to make it a fuzzy search so I did:
$term = 'whatever'; //$_GET['search_term'];
$new_term = '%'.$term.'%';
$params = [$new_term];
$sql = "SELECT * FROM products WHERE name LIKE ?";
$stmt = DB::run($sql,$params);
$resultCount = $stmt->rowCount();
if($resultCount < 1){
$sql = "SELECT * FROM products";
$stmt = DB::run($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$id = $row["pro_id"];
$result = $row[$lang];
similar_text($term,$result,$similarity);
$similar_array[$similarity][] = $id;
}
$closest_match = array_keys($similar_array);
rsort($closest_match);
$match_count = count($closest_match);
$similar_ids = [];
for($i=0; $i<$match_count; $i++){
foreach($similar_array[$closest_match[$i]] as $id){
array_push($similar_ids,$id);
}
}
$resultCount = count($similar_ids);
}
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page = 1;
}
$per_page = 20;
$last_page = ceil($resultCount/$per_page);
if($page<1){
$page = 1;
}else if($page>$last_page){
$page = $last_page;
}
$pagination = "";
$limit = "LIMIT ".($page-1)*$per_page.",".$per_page;
if($last_page!=1){
if($page!=1){
$prev = $page-1;
$pagination .= "<a class='pagination' href='store'><<</a>";
$pagination .= "<a class='pagination' href='store/$prev'><</a>";
}
for($i=$page-2; $i<=$page+2; $i++){
if($i>0 && $i<=$last_page){
if($i == $page){
$pagination .= "<a class='pagination selected'>$i</a>";
}else{
$pagination .= "<a class='pagination' href='store/$i'>$i</a>";
}
}
}
if($page!=$last_page){
$next = $page+1;
$pagination .= "<a class='pagination' href='store/$next'>></a>";
$pagination .= "<a class='pagination' href='store/$last_page'>>></a>";
}
}
if(isset($term)){
echo $pagination;
$ids = [];
$params = [$new_term];
$sql = "SELECT * FROM products WHERE name LIKE ? $limit";
$stmt = DB::run($sql,$params);
$resultCount = $stmt->rowCount();
if($resultCount > 0){
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$id = $row["pro_id"];
$params3 = [$id];
$sql3 = "SELECT * FROM products WHERE id=?";
$stmt3 = DB::run($sql3,$params3);
while($row = $stmt3->fetch(PDO::FETCH_ASSOC)){
$id = $row["id"];
array_push($ids,$id);
}
}
var_dump($ids);
}else{
var_dump($similar_ids);
}
echo $pagination;
}
There was probably a much better way of doing this but this is what I have. My question then is how can I get the pagination to work here for the fuzzy results ($similar_ids)? I was thinking of some sort of function that will splice the array depending on the page number but I am not sure how I would go about this.
This does not answer your question, but I'm going to say these anyway:
Looking closely, you have a possible sql injection bug in handling of $limit.
Also, using a search index like Apache Solr or ElasticSearch would give you pagination nearly for free anyway. You might want to look into that. Setting up a dedicated index is another can of worms, yes, but then you would also have more and better options to handle that fuzzy search part too.

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>

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

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