Currently im having a problem with a rather old piece of script i had written for a guestbook.
The guestbook itself relies completely on the `id` field of the table to show the posts.
But if a post is deleted, then it screws up the pagination of the posts such that if you were to click "next page" the same results as the last page would show.
The query:
Code: Select all
$set = $numAllResults-$offset+1;
SELECT * FROM `posts` WHERE `id` <= '$set' ORDER BY `id` DESCCode: Select all
table - `posts`
`id`, `name`, `post`
1,'Joe','Hi there'
2,'Johanna','Love the guest book'
3,'Jeff','Great stuff'
4,'James','Could have more stuff'
8,'John','Excellent!'
9,'Joel','Not bad'As it stands, there should be 2 pages in the guestbook because there are more than 5 entries.
However, the first page will show entries:
9,8,4,3,2.
The second page:
8,4,3,2,1
Now in this example, the problem isnt too huge, although when i have say 20 entries with half deleted, it will show the same posts for about 4 pages, and then change after the $set is lower than the older posts.
Yes i am posting my code, but, this was written over a year ago now, so i know i am breaking some of my own today's standards for programming
Code: Select all
<?php
include_once("include/connect.php");
$guestbookconnect = connect();
mysql_select_db("alexast_db",$guestbookconnect);
$offset = $_GET['p'];
if(!$_GET['p']){$offset = 0;}
if($offset < 0){print "<script>window.location = 'guestbook.php';</script>";}
$results = 5; //display number of results per page.
$currentresults = 0;
$getAllPostsSQL = "SELECT * FROM `posts` ORDER BY `id` DESC";
$getAllPostsQuery = mysql_query($getAllPostsSQL, $guestbookconnect) or die(mysql_error($guestbookconnect));
$numAllResults = mysql_num_rows($getAllPostsQuery);
$set = $numAllResults-$offset+1;
$getPostsSQL = "SELECT * FROM `posts` WHERE `id` <= '$set' ORDER BY `id` DESC";
$getPostsQuery = mysql_query($getPostsSQL, $guestbookconnect) or die(mysql_error($guestbookconnect));
$numResults = mysql_num_rows($getPostsQuery);
while($getPostsResult = mysql_fetch_assoc($getPostsQuery)){
$currentresults++;
if($currentresults <= $results){
?>
<div class="guestbook_frame">
<div class="guestbook_box1">
<div id="style_name"><?php print $getPostsResult['name']; ?></div>
</div>
<div class="guestbook_box2">
<div id="style_date"><?php print $getPostsResult['date']; ?></div>
</div>
<div class="guestbook_box3">
</div>
<div class="guestbook_box4">
<div id="style_content"><?php print $getPostsResult['content']; ?></div>
</div>
</div>
<?php
}
}
mysql_close($guestbookconnect);
?>~Weiry