SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: page numbering problem
-
Mar 30, 2007, 11:43 #1
page numbering problem
hey all,
i have serached for page numbering but from the tutorials my situration is different basicly i have 12 results stored in my hacks table in the database and i would like to limit 10 results per page so you should have 2 pages and when more data gets added to the database so more pages will be shown. anyways that does not appaer to happen
here is my code
PHP Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Hacks</title>
</head>
<body>
<table width="50%" border="0" cellspacing="0" cellpadding="3" align="center" >
<tr>
<th align="center">Hack Name</th>
<th align="center">Description</th>
<th align="center">Version</th>
<th align="center">View</th>
</tr>
<?php
include("config.php");
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
//first we need to decide how much to display per page
$limit = 10;
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}
//start and end records
$start = ($page - 1) * $limit;
$search=mysql_query("SELECT hacksid, hackname, description, version FROM hacks WHERE `approved`='yes' AND `typeid`={$_GET['cat']}");
if(mysql_num_rows($search) > 0)
{
while($result=mysql_fetch_assoc($search))
{
echo "<tr valign='top'>\n";
$hacksid = $result['hacksid'];
$hackname = $result['hackname'];
$description = $result['description'];
$version = $result['version'];
echo "<td align=center>$hackname</td>\n";
echo "<td align=center>$description</td>\n";
echo "<td align=center>$version</td>\n";
echo "<td align=center><a href='hacks.php?hacksid=$hacksid'>View</a></td>\n";
echo "</tr>\n";
}
}
else
{
echo "<td align=center>there were no records found!</td>\n";
}
?>
</table>
</body>
</html>
Code:typeid name link 1 Add-On's cat.php?cat=1 2 Admin Tools cat.php?cat=2 3 BBCode cat.php?cat=3 4 Communication cat.php?cat=4 5 Cosmetic/Styles cat.php?cat=5 6 Syndication cat.php?cat=6 7 Security cat.php?cat=7 8 Profile cat.php?cat=8
-
Mar 30, 2007, 12:01 #2
- Join Date
- Sep 2005
- Posts
- 323
- Mentioned
- 5 Post(s)
- Tagged
- 0 Thread(s)
Google Pagination i think is what you want. Some nice tutorials on there- i can't remember which one i used.
-
Mar 30, 2007, 14:57 #3
- Join Date
- Mar 2007
- Posts
- 48
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Use the LIMIT and ORDER BY functions of mySQL.
LIMIT works like so: LIMIT $start, $show
$start = first row to show
$show = number of rows to show
So if its LIMIT 5, 12
It will shows records 5 to 17.
PHP Code:if(isset($_GET['page']))
$page = $_GET['page'];
else
$page = 1;
$start = ($page - 1) * 5;
$result = mysql_query("SELECT * FROM `articles` ORDER BY `posteddate` DESC LIMIT $start, 5 ");
EDIT:
Oh, and heres the page numbering system I just wrote, should not be hard to adapt to whatever you want:
PHP Code:
$total = mysql_num_rows(mysql_query("SELECT * FROM `articles`"));
$max = ceil($total / 5);
#prev page
if ($page > 1 && $total >= ($start - 1))
echo ' <a href="index.php?page=',($page-1),'"><prev</a> ';
elseif ($max > 1)
echo '<prev ';
#page numbers
if ($max > 1)
for($y = 1; $y <= $max; $y++)
{
if($page == $y) echo ' ',$page,' ';
else echo ' <a href="index.php?page=',$y,'">',$y,'</a> ';
}
#next page
if ($total > (5 + $start))
echo ' <a href="index.php?page=',($page+1),'">next></a> ';
elseif ($max > 1)
echo ' next>';
#no pages
if ($total == 0)
echo ' There are no articles to display. ';
-
Mar 30, 2007, 18:38 #4PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Hacks</title>
</head>
<body>
<table width="50%" border="0" cellspacing="0" cellpadding="3" align="center">
<tr>
<th align="center">Hack Name</th>
<th align="center">Description</th>
<th align="center">Version</th>
<th align="center">View</th>
</tr>
<?php
include("config.php");
mysql_connect($server,$db_user,$db_pass) or die("Could not connect to mysql because ".mysql_error());
mysql_select_db($database) or die("Could not select database because ".mysql_error());
$limit = 10;
$self=$_SERVER['PHP_SELF'];
$cat=isset($_GET['cat']) ? $_GET['cat'] : false;
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$start = ($page - 1) * $limit;
$query="SELECT hacksid, hackname, description, version FROM hacks WHERE `approved`='yes' AND `typeid`='" . mysql_real_escape_string($cat) . "' ";
$pagingQuery="LIMIT $start, $limit";
$result = mysql_query( $query . $pagingQuery) or die(mysql_error());
if(mysql_num_rows($result) > 0){
while($row=mysql_fetch_assoc($result)){
$hacksid = $row['hacksid'];
$hackname = $row['hackname'];
$description = $row['description'];
$version = $row['version'];
?>
<tr valign="top">
<td align="center"><?php echo $hackname ?></td>
<td align="center"><?php echo $description ?></td>
<td align="center"><?php echo $version ?></td>
<td align="center"><a href="hacks.php?hacksid=<?php echo $hacksid ?>">View</a></td>
</tr>
<?php
}
}else{
?>
<tr valign="top">
<td align="center">there were no records found!</td>
</tr>
<?php
}
?>
</table>
<table border="0" cellspacing="0" cellpadding="3" align="center">
<tr valign="top">
<?php
$result = mysql_query($query) or die(mysql_error());
$numrows = mysql_num_rows($result);
$maxPage = ceil($numrows / $limit);
?>
<td align="center"><?php echo $page ." of ". $maxPage ?></td>
<?php
if($page > 1 && (!($page > $maxPage))){
$p = $page - 1;
?>
<td align="center"><a title="previous" href="<?php echo $self ?>?page=<?php echo $p ?>&cat=<?php echo $cat ?>">< Previous Page</a></td>
<td align="center"><a title="top" href="<?php echo $self ?>">top</a></td>
<?php
}
if($page < $maxPage){
$p = $page + 1;
?>
<td align="center"><a title="next" href="<?php echo $self ?>?page=<?php echo $p ?>&cat=<?php echo $cat ?>">Next Page ></a></td>
<?php
}else{
$p = $maxPage;
}
?>
</tr>
</table>
</body>
</html>my mobile portal
ghiris.ro
-
Mar 31, 2007, 09:21 #5
thank you Ernie
much appricated
Bookmarks