The first page should be 1 but its -1 and the next page should be 2 but its 0.
How can I make this work? I have tried to edit the $page but when I do, the if/else get wierd.
I added the whole code...
...
$resultt= "SELECT * FROM users WHERE AND country='$okc' AND state='$oks'";
if(!empty($a1) && empty($a2)){
$resultt .= " AND aar>=";
...
}
There are more than 1 if statement.
$result = mysqli_query($con,"$resultt");
$rec_limit = 19;
$row = mysqli_fetch_array($result);
$rec_count = $row[0];
if( isset($_GET['page'] ) )
{
$page = $_GET['page'] + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$resultt .= "LIMIT $offset, $rec_limit";
$sql = $resultt;
$retval = mysqli_query( $con, "$resultt" );
if( $page > 0 )
{
$last = $page - 1;
echo "Last 10 Records |";
echo "Next 10 Records";
}
else if( $page == 0 )
{
echo "Next 10 Records";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "Last 10 Records";
}
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
}
You should change parts of code to get it to work. $_GET['page'] should give you the queried page number itself, so don't try to manipulate it.
$sql = "SELECT * FROM users WHERE country='$okc' AND state='$oks'"
$rec_limit = 10;
$result = mysqli_query( $con, $sql );
$row = mysqli_fetch_array( $result );
$rec_count = $row[0];
$left_rec = $rec_count - ( $rec_limit * $page );
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = $rec_limit * ( $page - 1 ) ;
$sql.= " LIMIT $rec_limit OFFSET $offset";
$result = mysqli_query( $con, $sql );
$row = mysqli_fetch_array( $result );
if( $page > 1 ){
$last = $page - 1;
echo "Last 10 Records |";
echo "Next 10 Records";
}else if( $page == 1 ){
echo "Next 10 Records";
}else if( $left_rec < $rec_limit ){
$last = $page - 2;
echo "Last 10 Records";
}
while( $row = mysqli_fetch_array( $result, MYSQL_ASSOC ) ){
// use $row to echo row contents ...
}
$_GET is an array like your $row.
to access array keys you always use [].
change your get into $_GET['page']
even better:
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
is page set then convert value to int, because you dont know whats incomming.
if not set take page 1.
btw. constructs like this:
$resultt = "LIMIT $offset, $rec_limit";
can/will cause sql injections bugs within you homepage!
never build a sql with variables!
Use parameters instead!
Related
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";
?>
I have following code to display only 10 records at one page. When I load the page for the first time, first 10 records are displayed well (see screenshot)
. But when I click on "Next 10 records", the same URL is not loaded with extra GET parameters using $_SERVER['PHP_SELF'].
My address bar shows this instead:
http://localhost/PHP/$_SERVER['PHP_SELF']?page=$page
Here is my complete code;
<?php
$con = #mysqli_connect( "localhost:3306", "root", "P#ssw0rd", "world" ) or die ("Couldn't connect to server");
//get total number of records
$query = "SELECT count(ID) FROM city";
$result = mysqli_query ( $con, $query) or die ("Couldn't execute SELECT query: ". mysqli_error($con));
$rec_count = mysqli_num_rows($result);
$rec_limit = 10; //number of records to display
if( isset($_GET['page']) ){ //when user clicks link
$page = $_POST['page'] + 1; //page count increment by 1
$offset = $rec_limit * $page;
}
else{ //user has not yet clicked any link
$page = 0;
$offset = 0;
}
//xxxxxxxx $rec_count is taken from result set
$left_rec = $rec_count - ($page * $rec_limit); //number of records remaining to display
$display_query = "SELECT * FROM city LIMIT $offset, $rec_limit";
$display_result = mysqli_query ( $con, $display_query) or die ("Couldn't execute SELECT query: ". mysqli_error($con));
echo "<table>";
echo "<th>ID</th> <th>Name</th> <th>Country Code</th> <th>District</th> <th>Population</th>";
while( $row = mysqli_fetch_assoc($display_result) ){
extract($row);
echo "<tr>
<td>$ID</td>
<td>$Name</td>
<td>$CountryCode</td>
<td>$District</td>
<td>$Population</td>
</tr>";
}
echo "</table>";
if( $page == 0 ){ //user has not yet clicked any link
echo ' Next 10 records ';
}
else if( $page>0 ){ //user has clicked at least once on link
$last = $page - 2; //here -2 because, in isset block again increment by 1
echo '<a href='. $_SERVER['PHP_SELF']. '?page=$last> Last 10 records </a>';
echo '<a href='. $_SERVER['PHP_SELF']. '?page=$page> Next 10 records </a>';
}
else if( $left_rec < $rec_limit ){ //when only records less than 10 remains
$last = $page - 2;
echo '<a href='. $_SERVER['PHP_SELF']. '?page=$last> Last 10 records </a>';
}
?>
if( $page == 0 ){ //user has not yet clicked any link
echo ' Next 10 records ';
}
This will fix your single and double quotes and will echo the values instead of the variables. You have to apply the same logic to all the inputs.
You can also do:
if( $page == 0 ){ //user has not yet clicked any link
echo " Next 10 records ';
}
echo ' Next 10 records ';
And similar $last.
I want to use pagination on sql fetched row. I am using this code:
<?php
$rec_limit = 4;
$sql = "SELECT count(id) FROM wp_wct8 ";
$retval = mysql_query( $sql);
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT id, name, insurance ".
"FROM wp_wct8 ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql);
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while ($b = mysql_fetch_array($retval)){
echo $b['name'];
echo $b['insurance'];
}
if( $page > 0 ) {
$last = $page - 2;
echo "Last 4 Records |";
echo "Next 4 Records";
}else if( $page == 0 ) {
echo "Next 4 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 4 Records";
}
?>
but pagination not working... when i click on show next 4 records it is showing same records not next..please helpme... thanks in advance
I don't recommend you to put this code online as it's vulnerable to SQL injection. (Use prepared statements with PDO instead or at least, escape your input in the SQL query.)
I assume the error are the whitespaces in your links. So try removing the whitespaces, e.g. echo "Last 4 Records |";
I've the following Pagination code :
$rec_limit = 3;
/* Get total number of records */
$query = "SELECT count(id) FROM news";
$result = mysql_query($query);
if(!$result)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$query2 = "SELECT * ".
"FROM news ".
"LIMIT $offset, $rec_limit";
$result2 = mysql_query( $query2 );
if(! $result2 )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($result2, MYSQL_ASSOC))
{
echo " ID :{$row['id']} <br> ".
" News : {$row['title']} <br> ".
" Date : {$row['date']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 )
{
$last = $page - 2;
echo "Last 3 Records |";
echo "Next 3 Records";
}
else if( $page == 0 )
{
echo "Next 3 Records";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "Last 3 Records";
}
The code works perfectly as needed, except that the a href part where it shows the Next 3 records link even when there is no news left. By that i mean, it keeps opening up pages with empty records ! How can i remove Next 3 Records link when it reaches the last news ID.
Thank You
Just bring the last condition above the other two and change < to <=:
if($left_rec <= $rec_limit)
{
$last = $page - 2;
echo "Last 3 Records";
} else if ( $page > 0 )
{
$last = $page - 2;
echo "Last 3 Records |";
echo "Next 3 Records";
}
else if( $page == 0 )
{
echo "Next 3 Records";
}
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));