Reply
Paging error
Old 05-06-2008, 04:41 PM Paging error
Sneakyheathen's Avatar
Ultra Talker

Posts: 259
Name: Corey Freeman
Okay, so, this script generates links and limits the results, but for some reason when I try to view past posts, nothing shows up. Help?

Code:
<?php
include('sources/connect.php');
$pagenum = 1;
$postperpage = 3;

if(isset($_GET['id'])) {
$id = $_GET['id'];
$query = "SELECT title, text FROM posts WHERE Id=$id";
$result = mysql_query($query);
    while ($row = mysql_fetch_array($result)){
    ?>
    <div class='post' id='post'>
    <div class='meta'>
    Posted by <a href='http://www.deadlyclever.com'>Corey</a>
    </div>
<h2><?php echo $row['title']; ?></h2>
<p><?php echo $row['text']; ?></p>
</div>
    <?
    }
}

elseif(isset($_GET['page'])){
$pagenum = $_GET['page'];
}

else{

$offset = ($pagenum - 1) * $postperpage;

$query = " SELECT id, title, text FROM posts " . " LIMIT $offset, $postperpage " . " ORDER BY id DESC ";

$result = mysql_query($query) or die("SQL Query failed ...");

  while ($row= mysql_fetch_array($result)) {
?>
    <div class='post' id='post'>
    <div class='meta'>
    Posted by <a href='http://www.deadlyclever.com'>Corey</a>
    </div>
<h2><a href='index.php?id=<?php echo $row['id']; ?>'><?php echo $row['title']; ?></a></h2>
<p><?php echo $row['text']; ?></p>
</div>
<?

}


$query = " SELECT title, text FROM posts ORDER BY id DESC ";

$numresults = mysql_query($query) or die('Error, query failed');
$numrows = mysql_num_rows($numresults);
$maxpage = ceil($numrows/$postperpage);
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxpage; $page++)
{
    if ($page == $pagenum){
    $nav .=" $page "; 
    }
    else {
    $nav .=" <a href=\"$self?page=$page\">$page</a> ";
    }
}
if ($pagenum > 1)
{
   $page  = $pagenum - 1;
   $prev  = " <a href=\"$self?page=$page\">[Newer]</a> ";

   $first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
   $prev  = '&nbsp;'; // we're on page one, don't print previous link
   $first = '&nbsp;'; // nor the first page link
}

if ($pagenum < $maxpage)
{
   $page = $pagenum + 1;
   $next = " <a href=\"$self?page=$page\">[Older]</a> ";

   $last = " <a href=\"$self?page=$maxpage\">[Last Page]</a> ";
}
else
{
   $next = '&nbsp;'; // we're on the last page, don't print next link
   $last = '&nbsp;'; // nor the last page link
}

if($numrows == 0)
{
echo "<h4>There is no news found...</h4>";
}
}

// print the navigation link
echo $prev . $next;

require_once('sources/disconnect.php');
?>
Sneakyheathen is offline
Reply With Quote
View Public Profile Visit Sneakyheathen's homepage!
 
When You Register, These Ads Go Away!
     
Old 05-06-2008, 04:54 PM Re: Paging error
rogem002's Avatar
Webmaster Talker

Posts: 547
Name: Mike
Location: United Kingdom
Are you using output buffer's?
If you are put the following at the end of your code:
PHP Code:
ob_flush(); 
__________________
Website Services
PHP Code:
if(Added_Talkupation($post) == TRUE){iHug($you);} 
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 05-06-2008, 05:01 PM Re: Paging error
Sneakyheathen's Avatar
Ultra Talker

Posts: 259
Name: Corey Freeman
Quote:
Originally Posted by rogem002 View Post
Are you using output buffer's?
If you are put the following at the end of your code:
PHP Code:
ob_flush(); 
I don't know what an output buffer is. Honestly, I barely understand this code. I used a tutorial and got the basic concepts.
Sneakyheathen is offline
Reply With Quote
View Public Profile Visit Sneakyheathen's homepage!
 
Old 05-06-2008, 05:30 PM Re: Paging error
tripy's Avatar
Fetchez la vache!

Posts: 1,849
Name: Thierry
Location: In the void
Just looking at the code, I see one strange thing; the query:
Code:
$query = " SELECT id, title, text FROM posts " . " LIMIT $offset, $postperpage " . " ORDER BY id DESC ";
The LIMIT x, y is a shortcut, but I believe it should by LIMIT {how many}, {from position}.
Which would convert to
Code:
$query = " SELECT id, title, text FROM posts " . " LIMIT $postperpage, $offset " . " ORDER BY id DESC ";
Or, with the full SQL syntax:
Code:
$query = " SELECT id, title, text FROM posts " . " LIMIT $postperpage OFFSET $offset " . " ORDER BY id DESC ";
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 05-06-2008, 05:51 PM Re: Paging error
Sneakyheathen's Avatar
Ultra Talker

Posts: 259
Name: Corey Freeman
I'm afraid that didn't really fix anything...
Sneakyheathen is offline
Reply With Quote
View Public Profile Visit Sneakyheathen's homepage!
 
Old 05-07-2008, 02:56 AM Re: Paging error
Experienced Talker

Posts: 33
Can you send me your database structure too so that I can check the script
__________________
Explode Business Credit E-Book
bizcredit is offline
Reply With Quote
View Public Profile
 
Old 05-07-2008, 03:43 PM Re: Paging error
Sneakyheathen's Avatar
Ultra Talker

Posts: 259
Name: Corey Freeman
Here is the structure of the table. It's saved as a .txt file.
Attached Files
File Type: txt test.txt (1.4 KB, 0 views)
Sneakyheathen is offline
Reply With Quote
View Public Profile Visit Sneakyheathen's homepage!
 
Reply     « Reply to Paging error
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14065 seconds with 14 queries