Rizvi
Rizvi

Reputation: 283

Link to another page in php pagination?

I have a code with query for in php paging

$page = (isset($_GET['page'])) ? $_GET['page'] : 1;

$startPoint = $page - 1;
$sql="SELECT * FROM ` admin_crmf_poc_event_history` 
where $condition
order by event_date asc
LIMIT $startPoint,30";
$result = mysql_query($sql);

and for creating link to next page, i use

<a href="index.php?page=<?php echo $page - 29?>">Prev</a>
<a href="index.php?page=<?php echo $page + 29?>">Next</a>

but I give link index.php which shows the whole values from the start. dont know how to give link of next page so the rest of values are shown. Please help??

Upvotes: 0

Views: 760

Answers (1)

CodeBird
CodeBird

Reputation: 3858

try it like this

$page = (isset($_GET['page']) && (int)$_GET['page']>0) ? (int)$_GET['page'] : 1);

$startPoint = ($page*30) - 30;
$sql="SELECT * FROM ` admin_crmf_poc_event_history` 
where $condition
order by event_date asc
LIMIT $startPoint,30";
$result = mysql_query($sql);

<?php if($page>1){?><a href="index.php?page=<?php echo $page - 1; ?>">Prev</a><?php } ?>
<a href="index.php?page=<?php echo $page + 1; ?>">Next</a>

so what I did is first I added (int) before your $_GET['page'] to cast the $_GET value to int, second thing is I multiplied $page by how many rows per page you want then subtracted rows per page so, if you are at page 1 your start point will be 1*30-30=0 at page 2 it will be 2*30-30=30 etc... Then all you have to do with page links is subtract 1 for previous page and add 1 for next page.

Upvotes: 2

Related Questions