开发者

How to do greater (>) and lesser (<) scripts?

I am using this script below. I need the "next" to show only when there are more than 10 entries.

code:

$fetch = mysql_query("SELECT * FROM table LIMIT $startrow, 10")or
         die(mysql_error());
/*
this next options shows regarldess if there are more than 10 queries or less.  
How can i make it so that it shows only when there are more than 10 queries.
*/ 
echo开发者_运维问答 '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a>';
$prev = $startrow - 10;
//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0)
    echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'">Previous</a>';


You need to perform a separate query to count the number of rows in the table: SELECT COUNT(*) FROM table, and check if the returned number is greater than 10 $startrow + 10.


A word of advice. Don't rewrite something that's been done a thousand times. What you are trying to accomplish is called Pagination (Page by page displaying of data). It's commonly part of most PHP frameworks; such as CodeIgniter or CakePHP. There are also plugin classes out there which will help you greatly.

Google Search: http://www.google.com/search?q=php+pagination

As other have already mentioned you'll typically need two queries. One query for the subset of results (number per page & an offset point) and one query for the total number of results. Try to find an existing class to help you with your paginating.

Good luck, Jeff Walters

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜