Menu
Hi,
i have a sql statement that has over 100 entries. Im not the best at PHP but understand it quite well. How do i spreed these over a number of pages i.e have only 15 per page or so so there is page numbers at the bottom of how many pages there are.
If any1 has any tuts on it or can shed some light on it, that would be great.
Thanks Adam
[code=php]
<?PHP
$id = 100;
/* $id is the maximum number of entries
This would be the number you would to grabe the correct row.
*/
if(isset($_GET['ent']) && is_numeric($_GET['ent']) && $_GET['ent'] <= $id) {$start=$_GET['ent'];} else {$start=$id;}
for($x=$start; $x>0; $x--)
{
/* necessary database interaction here. Display entries here as well.*/
echo($x."<br />n");
// at the 15'th entry, displays next and/or back links.
if($x==$start-14 && $x>1)
{
if($start<=$id-15){echo("<a href="page.php?ent=".($x+($start-$x+15))."">Back</a> | ");}
echo("<a href="page.php?ent=".($x-1)."">Next</a>");
break;
}
if($x==1 && $start < $id){echo("<a href="page.php?ent=".($x+($start-$x+15))."">Back</a>");}
}
?>
[/code]
[/SIZE] [code=php]
<?php
# Connect to your database here; mysql_connect('host', 'user', 'pass');
# Assigns $page the current page number, or 1 if the user is on the main page
$page = (isset($_GET['page']) && is_numeric($_GET['page'])) ? $_GET['page'] : 1;
# We just need to select something, anything, from the DB to get the number of rows of data
$query_count = mysql_query("SELECT field
FROM db
");
# This is the number of results per page
$entry_limit = 5;
# The total number of rows in the DB
$total_rows = mysql_num_rows($query_count);
# This calcuates the number to use in the SQL "where" clause for the current page's results (this will just be 5 if the user is on the main page)
$limit_value = $page * $entry_limit - ($entry_limit);
# This is where you query the DB for data; you can remove the if..else statement that checks if there is any data in the DB
$query = mysql_query("SELECT field
, field2
ORDER BY some_auto_inc_field
ASC LIMIT " . $limit_value . ", " . $entry_limit);
if(mysql_num_rows($query) == 0) {
echo '<p>There is no available data.</p>' . "n";
}
else {
# Display DB results here, using a while loop or something
# If the user isn't on page one, decrement the page number
if($page > 1) {
$page--;
}
# This is the total number of pages you can display; it's the total rows of data in your DB divded by the number of results per page
$num_of_pages = $total_rows / $entry_limit;
# Loop through the number of pages so we can display links to the other pages
for($x=1; $x<=ceil($num_of_pages); $x++) {
# If the user is on the current page, then don't display a link, just display the page number
if($x == $_GET['page'] || (empty($_GET['page']) && $x == 1)) {
echo 'Page ' . $x . "n";
}
# Echo out the page number as a link to that page
else {
echo '<a href="view_gb.php?page=' . $x . '">Page ' . $x . '</a><br />' . "n";
}
}
} # If you remove the if...else statement above that checks for any data in the DB, you must also remove this brace
?>
[/code]
[code=php]<?php
$pop = isset($_GET["pop"]) ? $_GET["pop"] : "";
$pg = isset($_GET["pg"]) ? $_GET["pg"] : "1";
if ($pop == '1') {
ob_start();
include('/<SNIP>/neverquest/includes/class.gzip_encode.php');
include('/<SNIP>/neverquest/includes/conf_global.inc');
$first = (7 * $pg);
echo("
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang='English'>
<head>
<title>" . $info['name'] . " | News Viewer</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
<meta name="Author" content="Jonathan Hammler A.K.A Jonnie A.K.A Pedro">
<meta name="Language" content="English (UK)">
<style type='text/css'>
body {
background-color: #FFFFFF;
margin: 5px;
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
font-size: 12px;
}
a:link {
text-decoration: underline;
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
color: #000000;
}
a:visited {
color: #000000;
text-decoration: underline;
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
}
a:hover {
color: #EF3E07;
text-decoration: none;
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
}
acronym {
cursor: help;
}
h1 {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
font-size: 14px;
}
</style>
</head>
<body>
");
$result = mysql_query("SELECT * FROM latest_news
ORDER BY id
DESC LIMIT " . $first . ",7");
if (mysql_num_rows($result) > 0) {
while ($news = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("<span style='font-size: 10px;'><b>%s</b><br><br style='line-height: 3px;'>%s</span><br><br style='line-height: 9px;'>n", $news["title"], $news["content"]);
}
} else {
echo("<div style='font-size: 10px; text-align: center'><b>Error!</b> No news items could be found.</div>");
}
$pages = array("<a href='/news.php?pop=1&pg=0' target='_self'>0</a>", "<a href='/news.php?pop=1&pg=1' target='_self'>1</a>", "<a href='/news.php?pop=1&pg=2' target='_self'>2</a>", "<a href='/news.php?pop=1&pg=3' target='_self'>3</a>");
$pages[$pg] = $pg;
$echo_pages = "";
foreach ($pages as $a) {
$echo_pages .= "| " . $a . " ";
}
$echo_pages .= "|";
echo("<br><div style='text-align: center;'>" . $echo_pages . "
<script language='JavaScript' type='text/javascript'>
if (window.opener) document.write('<br><a href="#" onclick="self.close();">Close</a>');
</script></div>
</body></html>
");
new gzip_encode();
} else {
$result = mysql_query("SELECT * FROM latest_news
ORDER BY id
DESC LIMIT 7");
while ($news = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("<span style='font-size: 10px;'><b>%s</b><br><br style='line-height: 3px;'>%s</span><br><br style='line-height: 9px;'>", $news["title"], $news["content"]);
}
}
?>[/code]
[code=php]include('news.php');[/code]
It just grabs/outputs the latest 7 items from latest_news
[code=php]echo("<div style='text-align: right; font-size: 11px;'><a href="/news.php?pop=1&pg=1" onclick="popMe(this.href,300,120);return false;" target="newWin">Older News</a></div>")[/code]
Which opens a popup window with 7 news items on each page up to 4 pages (0-4). P0 has the latest news grabbed by the include, P1 has the next 7 etc. The default page is P1. What else, ahh, yes if no news is grabbed a message is printed saying no news is available.0.1.9 — BETA 5.10