keep post variable in php pagination - php

I have a php pagination page with post submit by user to search something, query mysql in first page is ok, but in NEXT page, i have get white blank page.
Below is the paging code.
$_SESSION['nationality'] = $_POST['nationality'];
$reclimit = 2;
if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$start = (($page-1) * $reclimit);
$sql = "SELECT userid, name, LEFT(hkid, 4) as hkid, description, nationality, photo_1, photo_2, photo_3 FROM $tbl_name WHERE `nationality` LIKE '%$_SESSION[nationality]%'";
$records = $con->query($sql);
$total = $records->num_rows;
$tpages = ceil($total / $reclimit);
$rec = "SELECT userid, name, LEFT(hkid, 4) as hkid, description, nationality, photo_1, photo_2, photo_3 FROM $tbl_name WHERE `nationality` LIKE '%$_SESSION[nationality]%' LIMIT $start, $reclimit";
$records = $con->query($rec);
while ($row = mysqli_fetch_assoc($records)){
// Loop record
}
// Paging
echo '<ul class="pagination pagination-lg">';
for( $i=1; $i <= $tpages; $i++ ) {
$active = $i == $page ? 'class="active"' : '';
echo "<li $active ><a href='$_SERVER[PHP_SELF]?page=" .$i. "'>" .$i. "</a></li>";
}
echo '</ul>';

This works for me:
<?php
$limit =3;
if (!isset($_GET['pg'])) {
$pg = 1;
} else {
$pg = $_GET['pg'];
}
$start = ($pg - 1 ) * $limit;
$sql = "SELECT * FROM tableName LIMIT $start , $limit";
?>

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.

Detect Previous Next ID

I have a little script that have a Previous & Next button.
My problem is I want to detect & place the ID in those buttons
this is the code from the pagination
<body>
<?php include_once 'data.php'; ?>
<center>
<ul class="pagination">
<?php
if($page_counter == 0){
echo "<li><a href=?start='0' class='active'>0</a></li>";
for($j=1; $j < $paginations; $j++) {
echo "";
}
}else{
echo "<a href=?start=$previous><button>Previous</button></a>";
for($j=0; $j < $paginations; $j++) {
if($j == $page_counter) {
echo " ";
}else{
echo " ";
}
}if($j != $page_counter+1)
echo "<a href=?start=$next><button>Next</button></a>";
}
?>
</ul>
</center>
In this part I have the ID but the problem is I can`t get it with this example to place it into the pagination.
<?php
foreach($result as $row) {
echo '
<div class="card"><button1>
'. $row['notice_id'] .'
<div class="time-left">
<div class="dropdown1">
' ;
}
}
else {
echo '';
}
$conn->close();
?>
</div></div>
This is the code from data.php I think I need to place some code into for detection from the ID
<?php
//include configuration file
require 'configuration.php';
$start = 0; $per_page = 1;
$page_counter = 0;
$next = $page_counter + 1;
$previous = $page_counter - 1;
if(isset($_GET['start'])){
$start = $_GET['start'];
$page_counter = $_GET['start'];
$start = $start * $per_page;
$next = $page_counter + 1;
$previous = $page_counter - 1;
}
// query to get messages from messages table
$q = "SELECT * FROM group_notice LIMIT $start, $per_page";
$query = $db->prepare($q);
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
// count total number of rows in students table
$count_query = "SELECT * FROM group_notice";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
// calculate the pagination number by dividing total number of rows with per page.
$paginations = ceil($count / $per_page);
?>
You can use some formulas for calculate limit and offset of results.
For example you have 100 records, and you want to paginate it into 10 records per page, and the formula is like below.
// Get Page from Query String
$page = 1;
if(!empty($_GET["page"])){
$page = $_GET["page"];
}
// Get record count
$count_query = "SELECT * FROM group_notice";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
$per_page = 10; // records per page
$pages = ceil($count/$per_page); // get total page
$start = $page * $per_page - $per_page; // get offset
// Do something with your records
$query = db->prepare("SELECT * FROM group_notice LIMIT ?, ?");
$query->bind_param("ii", $start, $per_page); // you should use bind param if you use prepared statement
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
if($page > 1){
echo 'Prev'; // Print Prev Page
}
for($i = 1; $i <= $pages){
echo ''.$i.''; // Print Page Number
}
if($page < $pages){
echo 'Next'; // Print Next Page
}
Hope it helps.

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

Pagination not displaying PHP PDO

Im trying to paginate my query only nothing is being outputted, can anybody see where im going wrong? Im very new to PHP/PDO
else {
try
{
$per_page = '3';
$result = $conn->prepare("SELECT * FROM directory WHERE user_active != ''");
$rows = $result ->fetchAll();
$total_records = count($rows);
$pages = ceil($total_records / $per_page);
$page = (isset ($_GET['page'])) ? (int) $_GET['page'] : 1 ;
$start = ($page - 1) * $per_page;
$query = $conn->prepare("SELECT * FROM directory WHERE user_active != ''");
while($row = $query->fetch(PDO::FETCH_ASSOC)){
echo '<br>' . $row['id'] . '<br>' . $row['First_Name'] . '<br>' . $row['Surname'] . '<br>';
} ?>
<br><br>
<?php
if ($pages >= 1 && $page <= $pages){
//if ($page>1 && $page <= $pages){$previous=($page -1); echo 'Previous';}
for($x=1; $x<=$pages; $x++){ echo ($x == $page) ? ' <strong>'.$x.'</strong> ' : ' '.$x.' ';}
//if ($page>1 && $page <= $pages){ $next=($page+1) ; echo 'Next';}
}
####################### Close Database #######################
$db = NULL;
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
};
You never execute the query.
$result->execute();
$rows = $result ->fetchAll();
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
Also it's better to use LIMIT in your query. Something like that:
//to get need page
$per_page = 3;
$page = (isset ($_GET['page'])) ? (int) $_GET['page'] : 1 ;
$result = $conn->prepare(
"SELECT * FROM directory
WHERE user_active != ''
LIMIT ".(($page - 1) * $per_page).", ".$per_page
);
$result->execute();
$rows = $result ->fetchAll();
//to get count of pages
$result = $conn->prepare(
"SELECT COUNT(*) FROM directory
WHERE user_active != ''"
);
$result->execute();
$total-pages = reset($result->fetch(PDO::FETCH_NUM));

PHP paging, viewing a certain page first

This is my script for the paging on my site when the user clicks on a league.
The league is then echoed to the screen, and if the league is over 3 rows then it splits it up in to several pages.
What I am doing after is depending on where the user is in the league (the SQL query is using ORDER BY the total points column in the table), e.g if the user is on page one of the league table then for it to display that page first, but if the user is on page 3 of the league table then for that page to displayed first.
Does anyone know a way in order for me to achieve this?
//Recently updated from answer
$sql="SELECT members.username, members.total_points FROM members, members_leagues WHERE members.username = members_leagues.username AND
members_leagues.sub_league = '$chosenleague' ORDER BY members.total_points DESC";
$result=mysql_query($sql);
$i = 0;
$found = false;
$team_position = 0;
while (!$found && $row = mysql_fetch_row($result)){
if ($row[username] == $_SESSION[username]) {
$team_position = $i;
$found = true;
}
$i++;
}
$rowsPerPage = 3;
$pageNum = ceil($i/$rowsPerPage);
//end of recently updated
if(isset($_GET['page']))
$pageNum = $_GET['page'];
$offset = ($pageNum - 1) * $rowsPerPage;
$counter = $offset + 1;
$query = " SELECT members.username, members.teamname, members.total_points, FROM members, members_leagues WHERE members.username = members_leagues.username AND members_leagues.sub_league = '$chosenleague' ORDER BY members.total_points DESC " . " LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');
echo "<h3 style=\"color:red;\">$chosenleague</h3>";
echo "<table>";
echo "<tr><th>Position</th>";
echo "<th>Team</th>";
echo "<th>Points/Overall</th>";
echo "<th>Points/Last Race</th>";
echo "<th>Team Setup</th></tr>";
while($row = mysql_fetch_array($result))
{
if($row[username] == $_SESSION[username])
echo "<tr style=\"color:red;\"><td>";
else
echo "<tr><td>";
echo $counter;
echo "</td><td>";
echo $row[teamname];
echo "</td><td>";
echo $row[total_points];
echo "</td><td>";
echo "</td><td>";
echo "</td></tr>";
$counter++;
}
echo "</table>";
$query = "SELECT COUNT(members.username) AS numrows FROM members, members_leagues WHERE members.username = members_leagues.username
AND members_leagues.sub_league = '$chosenleague'";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
$nav = '';
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " << Prev ";
$first = " First ";
}
else
{
$prev = '';
$first = '';
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " Next >> ";
$last = " Last ";
}
else
{
$next = '';
$last = '';
}
echo "<div id=\"pagenum\">Page $pageNum of $maxPage ". $first . $last . $prev . $next ."</div>";
You can do it via mysql or php:
With PHP:
Found the position of the requested record in the array, then calculate the page you have to extract and execute the corresponding query. Something like.
$i = 0;
$found = false;
$team_position = 0;
while (!$found && $row = mysql_fetch_row) {
if ($row['team'] == 'team_your_searching_for') {
$team_position = $i;
$found = true;
}
$i++;
}
//calculate $top and $bottom
...
$sql = "SELECT * FROM members LIMIT $top, $bottom;";
...
With MySQL:
You can create a query that generates an autoincrement value and another that selects from the other's result. I mean
-- get the the selected member's position
SELECT team, pos FROM (SELECT team, points, #position = #position + 1 AS pos FROM members ORDER BY points) WHERE team = #the_team_your_searching_for;
-- get the nr of members
SELECT COUNT(*) FROM members;
...
-- calculate the page you wanna extract (#top, #bottom), and extract it
SELECT * FROM members LIMIT #top, #bottom;

Categories