/    Sign up×
Community /Pin to ProfileBookmark

only 10 entries per page

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

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@MstrBobSep 10.2004 — I did something similar. Erm, here, here's the code straight out, see if you can adapt it.

[SIZE=1][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]

Basically, it uses a loop. It starts off with the maximum number, and then at the 15'th number, it breaks from the loop and creates a link to the page (in this case, page.php). It sends the variable ent through the URL, which the script uses to continue. Hope it helps some, you may be able to adapt/learn from it.
Copy linkTweet thisAlerts:
@k0r54authorSep 10.2004 — could you perhaps break it down a bit more and explain where the sql statements comes in and how i display the database info using this.

Thanks Adam
Copy linkTweet thisAlerts:
@EupseudesSep 10.2004 — When you retrieve data from an sql database, it returns from an array. You can loop through that array and stop when you hit the page number you want.
Copy linkTweet thisAlerts:
@Paul_JrSep 11.2004 — [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]

I read a tutorial at PHPFreaks, and this is the code I ended up with. It isn't very practical if you have many, many results, because then you'll have a whole bunch of links going to each one of those pages. The original tutorial had a contingency for this, but at the time I didn't understand that part, so I cut it out. I'll try to find the original article and post it here.


[b]***EDIT***[/b]

Here, I found the article: [url=http://www.phpfreaks.com/tutorials/43/0.php]Pagination[/url]. You should probably be able to find more tutorials and articles by searching Google for "pagination", or "php pagination", or something along those lines.
Copy linkTweet thisAlerts:
@JavaHead_JonnieSep 11.2004 — If it helps, here's my news script, straight-out-of-the-bag:[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&amp;pg=0' target='_self'>0</a>", "<a href='/news.php?pop=1&amp;pg=1' target='_self'>1</a>", "<a href='/news.php?pop=1&amp;pg=2' target='_self'>2</a>", "<a href='/news.php?pop=1&amp;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]


If included on a page as in[code=php]include('news.php');[/code]It just grabs/outputs the latest 7 items from latest_news, but under the include I put something like this:[code=php]echo("<div style='text-align: right; font-size: 11px;'><a href="/news.php?pop=1&amp;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.

I hope this helps.
×

Success!

Help @k0r54 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...