Jump to content

A Problem With Pagination


Rodekode

Recommended Posts

Hello world

 

Hope somebody can help a noob in php

 

I have a result.php page listing all posts from my database. For each post there is a link to a detail.php page for more info on each specific post. The link goes like this:

 

<a href="detail.php?id=<?php echo $row['id']; ?>">Details</a>

 

In detail.php I have some pagination that is working nicely as long as I dont filter by id. If I add WHERE id=$id to both $sql and $query the id is grabbed nicely but the pagination is lost. Please can somebody tell me how to get the filter by id and the pagination work together?

 

My detail.php looks like this:

 

******

 

<?php

$id = $_GET['id'];

$server = "server";
$user = "user";
$pass = "pass";
$databasename = "databasename";
$db = mysql_connect($server, $user, $pass);
mysql_select_db($databasename,$db);
$sql = "SELECT * FROM `table`";
$query = mysql_query($sql,$db);
$total_results = mysql_num_rows($query);
$limit = "1";
$total_pages = ceil($total_results / $limit);
if (empty($page))
{
$page = "1";
}
$offset = ($page - 1) * $limit;
$query = "SELECT * FROM `table` LIMIT $offset, $limit";
$result = mysql_query($query);
?>




<!DOCTYPE html>


<head></head>

<body>

<?php
while ($row = mysql_fetch_array($result)) {
?>

<div>

Data from base : <?php echo $row['field-01']; ?> and <?php echo $row['field-02']; ?> and so on...

</div>

<?php
}
?>

<?php
// Previous
if ($page != 1)
{
$prevpage = $page - 1;
echo "<a href=$PHP_SELF?page=$prevpage>Previous</a>";
}
?>

<?php
// Next
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo "<a href=$PHP_SELF?page=$nextpage>Next</a>";
}
?>

<?php

mysql_close();
?>

</body>

</html>

 

******

 

Any help make me superhappy thanks

Edited by PFMaBiSmAd
removed existing formating to make readable
Link to comment
Share on other sites

First post ever, so learning there as well :) Hope this is better:

 

Hello world

Hope somebody can help a noob in php

I have a result.php page listing all posts from my database. For each post there is a link to a detail.php page for more info on each specific post.

The link goes like this:

[/size][/font]
<a href="detail.php?id=<?php echo $row['id']; ?>">Details</a>

In detail.php I have some pagination that is working nicely as long as I dont filter by id. If I add

[/size][/font]
WHERE id=$id[font='Lucida Grande'][size=2]

to both $sql and $query the id is passed nicely but the pagination is lost. Please can somebody tell me how to get the filter by id and the pagination work together?

My detail.php looks like this:

******

 

<?php[/size]


[size=4]$id = $_GET['id'];[/size]


[size=4]$server = "server";
$user = "user";
$pass = "pass";
$databasename = "databasename";
$db = mysql_connect($server, $user, $pass);
mysql_select_db($databasename,$db);
$sql = "SELECT * FROM `table`";
$query = mysql_query($sql,$db);
$total_results = mysql_num_rows($query);
$limit = "1";
$total_pages = ceil($total_results / $limit);
if (empty($page))
{
$page = "1";
}
$offset = ($page - 1) * $limit;
$query = "SELECT * FROM `table` LIMIT $offset, $limit";
$result = mysql_query($query);
?>[/size]





[size=4]<!DOCTYPE html>[/size]



[size=4]<head></head>[/size]


[size=4]<body>[/size]


[size=4]<?php
while ($row = mysql_fetch_array($result)) {
?>[/size]


[size=4]<div>[/size]


[size=4]Data from base : <?php echo $row['field-01']; ?> and <?php echo $row['field-02']; ?> and so on...[/size]


[size=4]</div>[/size]


[size=4]<?php
}
?>[/size]


[size=4]<?php
// Previous
if ($page != 1)
{
$prevpage = $page - 1;
echo "<a href=$PHP_SELF?page=$prevpage>Previous</a>";
}
?>[/size]


[size=4]<?php
// Next
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo "<a href=$PHP_SELF?page=$nextpage>Next</a>";
}
?>[/size]


[size=4]<?php[/size]


[size=4]mysql_close();
?>[/size]


[size=4]</body>[/size]


[size=4]</html>[/size]
[size=4]
Link to comment
Share on other sites

you are passing post id through result.php page and it gets in details.php page. so how many result come to details.php page with post id? You have mentioned that so many posting in result page and there is a link to details page for more information. so do you need to add pagination to your result page or details page..?

Link to comment
Share on other sites

The code doesn't account for the GET variable being passed, so $page is always "1". Instead of

 

if (empty($page))
{
$page = "1";
}

 

Try the following:

 

if(isset($_GET['page'])) {
   $page = $_GET['page'];
} else {
   $page = "1";
}

 

 

Side Note: I would recommend avoiding PHP_SELF for href attributes. For more information, see Why PHP_SELF Should Be Avoided When Creating Website Links

Link to comment
Share on other sites

Thanks a lot cyberRobot - that was the trick. To make it work I also changed the link-part on result.php. Instead of

 

<a href="detail.php?id=<?php echo $row['id']; ?>">Details</a>

 

I use this:

 

<a href="detail.php?page=<?php echo $row['id']; ?>">Details</a>

 

Also thanks for your kind advice on avoiding PHP_SELF - and so easy to avoid too :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.