Jump to content

MySql Paging


zackcez

Recommended Posts

My code is coming out odd...here's my code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
mysql_connect("mysql", "6498_rshelp", "") or die(mysql_error());
mysql_select_db("6498_rshelp") or die(mysql_error());
// how many rows to show per page
$rowsPerPage = 5;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$query  = "SELECT * FROM news LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

// print the random numbers
while($row = mysql_fetch_array($result)){
$result2 = mysql_query("SELECT COUNT(newsid) AS total FROM comments WHERE newsid='" . $row['id'] . "'");
$values2 = mysql_fetch_assoc($result2);
$count = $values2['total'];
echo "<h2><a href=\"view.php?id=" . $row['id'] . "\">" . $row['name'] . "</a></h2>";
echo "<p class=\"post-by\">Posted by " . $row['auther'] . "</p>";
echo "<p>" . $row['story'] . "</p>";
echo "<p class=\"post-footer align-right\">";
echo "<a href=\"view.php?id=" . $row['id'] . "\" class=\"readmore\">Focus</a>";
echo "<a href=\"view.php?id=" . $row['id'] . "\" class=\"comments\">Comments (" . $values2['total'] . ")</a></p>";
echo "</div>";
}
// how many rows we have in database
$query   = "SELECT COUNT(id) AS numrows FROM news";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];

// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link

// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";

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

// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";

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

// print the page navigation link
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
?>
</body>
</html>

Output:

http://rshelp.net/

I want the paging stuff to be under news..

Link to comment
https://forums.phpfreaks.com/topic/100222-mysql-paging/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.