Re: howw do I programm a PREV & Next mechanism in PHP? Help Please!!
| From: | Steve Werby | Date: | Thu, 04 Oct 2001 21:04:44 +0000 |
| Subject: | Re: howw do I programm a PREV & Next mechanism in PHP? Help Please!! | ||
| References: | 1 | Groups: | php.db php.general |
| Request: | Send a blank email to php-db+get-13012@lists.php.net to get a copy of this message | ||
"Sander Peters" <S.Peters@Visionnet.nl> wrote:
> I users wants to browse records from my (mysql) database, and the user
> can decide how many records he/she wants to see per page. There should
> be a Previous and a Next button on each page to brows through the
> records.
Here's a snippet of code from a site I built. It shoud be enough code for
you to accomplish what you want without really having to understand it. I
suggest taking the time to understand it though. If you're curious what it
looks like in action here's one of the pages generated from the full script
that the code below was pulled from.
http://www.tysonchandler.com/news_3.html
(that's an underscore, not a
space).
$sql = "SELECT * ";
$sql .= "FROM articles ";
$sql .= "WHERE active = 1 ";
$sql .= "ORDER BY article_date DESC, article_source ASC ";
$result = mysql_query( $sql, $GLOBALS['handle'] ) or die( 'No records.' );
$num_records = mysql_num_rows( $result );
$limit_rows = 8;
$pages = ceil( $num_records / $limit_rows );
if ( empty( $page ) )
{
$page = 1;
$limit_start = 0;
}
else
{
$limit_start = ( $page - 1 ) * $limit_rows;
}
$sql .= "LIMIT $limit_start, $limit_rows ";
$result = mysql_query( $sql, $GLOBALS['handle'] ) or die( 'No records.' );
$page_next = $page + 1;
$page_prev = $page - 1;
if ( $page_prev >= 1 )
{
$output .= ' <a href="/news_' . $page_prev . '.html';
$output .= '">Prev Page</a> |';
}
else
{
$output .= " Prev Page |";
}
--
Steve Werby
President, Befriend Internet Services LLC
http://www.befriend.com/