Pagination Force Correct ID
Posted: Wed Aug 31, 2005 5:20 pm
I've been playing with pagination and have found a big problem.
I am currently using it to display all articles in db, with comments
and blah blah......
The problem comes when and how it displays the ID number based on
how it pulls the article from the database.
For example, if I have it pull just one article starting from the first one
found in DB, it isn't a problem and the comments and id all work in unison.
Because it will display the first result shown as id=1 always and work its way up.
However, If I want to display... for example, the 5 newest entries, which in turn would be
the last ones in the DB, then pagination wants to create the display as starting from id=1
instead of the last id, and the last id would be the correct result if the last article is being displayed
first.
I'm sure this problem has been discussed many times over before, but pagination is only way
I know how to display results, and right now I am extremly limited to what I can display.
How do you force pagination to always show the proper ID Number regardless of the order
the content is displayed?
Here is the code I am currently using to show One Article linked to comments, starting from
first article found in DB.
As you can see, the way i made this is so that the comment is related directly to the displayed Bash ID, and not the actual DB ID of the bash, since pagination wont show it, plus it would look nasty if you have deleted some articles and the id numbers jump from each other.
If I want to change this to show the last 5 entries
then the starting id of the displayed results start from id=1 which throws everything off
since the id should equal the last available id.
Any Suggestions?
I am currently using it to display all articles in db, with comments
and blah blah......
The problem comes when and how it displays the ID number based on
how it pulls the article from the database.
For example, if I have it pull just one article starting from the first one
found in DB, it isn't a problem and the comments and id all work in unison.
Because it will display the first result shown as id=1 always and work its way up.
However, If I want to display... for example, the 5 newest entries, which in turn would be
the last ones in the DB, then pagination wants to create the display as starting from id=1
instead of the last id, and the last id would be the correct result if the last article is being displayed
first.
I'm sure this problem has been discussed many times over before, but pagination is only way
I know how to display results, and right now I am extremly limited to what I can display.
How do you force pagination to always show the proper ID Number regardless of the order
the content is displayed?
Here is the code I am currently using to show One Article linked to comments, starting from
first article found in DB.
Code: Select all
function NewsPage() {
if(!isset($_GET['bash'])){
$bash = 1;
} else {
$bash = $_GET['bash'];
}
$max_results = 1;
$from = (($bash * $max_results) - $max_results);
$sql = mysql_query("SELECT * FROM news LIMIT $from, $max_results");
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM news"),0);
$total_bashs = ceil($total_results / $max_results);
/* get number of comments */
$comment_query = "SELECT count(*) FROM comments WHERE newsid='$bash'";
$comment_result = mysql_query ($comment_query);
$comment_row = mysql_fetch_row($comment_result);
if($bash > 1){
$prev = ($bash - 1);
echo "<center><a href=\"display.php?bash=$prev\"><<-Previous</a> ";
}
if($bash < $total_bashs){
$next = ($bash + 1);
echo " <a href=\"display.php?bash=$next\">Next->></a><br />";
}
if($bash > 1){
$send = ($bash - 0);
$com = ($bash - 0);
echo "<a href=\"comment.php?bash=$bash\">($comment_row[0]) Comments</a> <a href=\"tell.php?bash=$com\">Send This Bash To A Friend</a></center><br /><br />";
}
while ($row = mysql_fetch_assoc($sql))
{
$newposts = array ( "Name" => $row['name'], "Past Loves" => $row['lova'], "Bash" => $row['news'] );
foreach ( $newposts as $postfield => $postinput )
{
$postinput = submitTags($postinput, 1, 1);
echo "<b>{$postfield}:</b> {$postinput}<br />";
}
echo "<p> </p>";
}
}If I want to change this to show the last 5 entries
Code: Select all
$sql = mysql_query("SELECT * FROM news ORDER BY id DESC LIMIT 5");since the id should equal the last available id.
Any Suggestions?