Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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

Posts: 346
Name: Corey Freeman
Trades: 0
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');
?>
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
Sneakyheathen is offline
Reply With Quote
View Public Profile Visit Sneakyheathen's homepage!
 
 
Register now for full access!
Old 05-06-2008, 04:54 PM Re: Paging error
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Are you using output buffer's?
If you are put the following at the end of your code:
PHP Code:
ob_flush(); 
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
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: 346
Name: Corey Freeman
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
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
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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 ";
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-06-2008, 05:51 PM Re: Paging error
Sneakyheathen's Avatar
Ultra Talker

Posts: 346
Name: Corey Freeman
Trades: 0
I'm afraid that didn't really fix anything...
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
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
Trades: 0
Can you send me your database structure too so that I can check the script
__________________

Please login or register to view this content. Registration is FREE
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: 346
Name: Corey Freeman
Trades: 0
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)
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
Sneakyheathen is offline
Reply With Quote
View Public Profile Visit Sneakyheathen's homepage!
 
Reply     « Reply to Paging error
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.77092 seconds with 13 queries