|
|
 |
October 5th, 2007, 09:21 AM
|
#1
|
|
Most Reputable Member
SuperMember
Join Date: Nov 2005
Age: 28
Posts: 1,546
Thanks: 1
Thanked 11 Times in 11 Posts
Rep Altering Power: 0
|
[SOLVED] new page, with page numbers
i have limited the amount of database info to be displayed on my blog, and i am trying to dynamically create a second page, third page etc... as the information is inputted into the database, so if i had
page 1 limit 5
if there are 6, then create next page and it goes on
http://www.mywebsite.com/mypage.php?id=2
http://www.mywebsite.com/mypage.php?id=3
etc...
also a numbering system, p1,p2,p3,p4 linking to newly created 'pages'
how can i do that ?
thanks
Code:
<?php error_reporting(E_ALL);?> <?php include 'config.php';?> <?php $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db( $dbname, $conn );?> <?php $sql = "SELECT * FROM form order by date desc limit 5 "; $res = mysql_query($sql, $conn) or die(mysql_error()); while ($row = mysql_fetch_object($res)) { print $row->title ; echo nl2br("\n"); print $row->date ; print '<br></br>'; print $row->blog_mes; print '<br></br>'; } ?>
|
|
|
October 5th, 2007, 10:16 AM
|
#2
|
|
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 22
Posts: 592
Thanks: 0
Thanked 0 Times in 0 Posts
|
Re: new page, with page numbers
I'm working on a solution. Give me a bit of time.... [HINT:] I'm using the mysql_num_rows function....
|
|
|
October 5th, 2007, 10:17 AM
|
#3
|
|
Highly Reputable Member
SuperMember
Join Date: Sep 2007
Location: Australia
Age: 25
Posts: 954
Thanks: 0
Thanked 0 Times in 0 Posts
|
Re: new page, with page numbers
You just need to set a starting point and limit in your query.
You can do this by doing a count of the number of results in the database.
Here's a simple example I just whipped up, commented for your viewing pleasure
Code:
<?php include 'config.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db( $dbname, $conn );
$perPage = 5; // number to display on per page
// Start by counting the number of posts
$res = mysql_query ("SELECT count(*) AS num FROM form") or die (mysql_error() . 'dying hurts');
$num = mysql_fetch_assoc($res);
$num = $num['num'];
mysql_free_result($res);
// now work out how many pages there are, check if page has been sent via GET
$numPages = ceil($num / $perPage);
// Check to make sure pagenum will be 1 or greater, less than numpages and an actual number
$pageNum = (!empty($_GET['id']) &&
is_numeric($_GET['id']) &&
$_GET['id'] > 1)
? ($_GET['id'] < $numPages ? int ($_GET['id']) : $numPages) : 1; // Set to 1 if not set or something dodgy
// Okay, now we run the query and set the limits LIMIT xx, yy (x= start from , y= limit)
$sql = "SELECT * FROM form order by date desc LIMIT " . ($pagenum - 1) * $perPage . ", $perPage";
$res = mysql_query($sql, $conn) or die(mysql_error());
while ($row = mysql_fetch_object($res)) {
print $row->title ;
echo nl2br("\n");
print $row->date ;
print '<br></br>';
print $row->blog_mes;
print '<br></br>';
}
// Some paginananananananation
echo '<div>';
for ($i = 1; $i <= $numPages; $i++)
{
if ($pageNum != $i)
echo '<a href="thispage.php?id=', $i, '">', $i, '</a> ';
else
echo '<b>', $i, '</b>';
}
echo '</div>';
?>
|
|
|
October 5th, 2007, 10:25 AM
|
#4
|
|
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 22
Posts: 592
Thanks: 0
Thanked 0 Times in 0 Posts
|
Re: new page, with page numbers
 ok, looks like Rakuli beat me to it...
|
|
|
October 5th, 2007, 10:29 AM
|
#5
|
|
Highly Reputable Member
SuperMember
Join Date: Sep 2007
Location: Australia
Age: 25
Posts: 954
Thanks: 0
Thanked 0 Times in 0 Posts
|
Re: new page, with page numbers
Quote:
Originally Posted by c010depunkk
 ok, looks like Rakuli beat me to it...
|
Sorry C010depunk  Always rearing for a pagination script.
|
|
|
October 5th, 2007, 10:32 AM
|
#6
|
|
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 22
Posts: 592
Thanks: 0
Thanked 0 Times in 0 Posts
|
Re: new page, with page numbers
I didn't know about the LIMIT 3,7 command. (I'm assuming that selects results from #3 to #7, correct me if not...) I was going to use mysql_data_seek, but LIMIT is much more efficient, especially with larger databases.
|
|
|
October 5th, 2007, 10:34 AM
|
#7
|
|
Most Reputable Member
SuperMember
Join Date: Nov 2005
Age: 28
Posts: 1,546
Thanks: 1
Thanked 11 Times in 11 Posts
Rep Altering Power: 0
|
Re: new page, with page numbers
thank you for your help, but i am recieving this
Quote:
|
Undefined variable: pagenum in c:\program files\easyphp1-8\www\blog\main.php on line 31
|
Quote:
|
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-5, 5' at line 1
|
|
|
|
October 5th, 2007, 10:38 AM
|
#8
|
|
Highly Reputable Member
SuperMember
Join Date: Sep 2007
Location: Australia
Age: 25
Posts: 954
Thanks: 0
Thanked 0 Times in 0 Posts
|
Re: new page, with page numbers
Nearly but not quite.
LIMIT 3, 7 would select starting from result 3 and give you 7 results.
If you wanted #3 - #7 you would use LIMIT 3, 4
You want to try get as much processing and limiting done with SQL as possible as by comparison, MYSQL is much faster than PHP.
|
|
|
October 5th, 2007, 10:41 AM
|
#9
|
|
Highly Reputable Member
SuperMember
Join Date: Sep 2007
Location: Australia
Age: 25
Posts: 954
Thanks: 0
Thanked 0 Times in 0 Posts
|
Re: new page, with page numbers
Okay, change this line
Code:
$sql = "SELECT * FROM form order by date desc LIMIT " . ($pagenum - 1) * $perPage . ", $perPage";
to:
Code:
$sql = "SELECT * FROM form order by date desc LIMIT " . ($pageNum - 1) * $perPage . ", $perPage";
I forgot to capitalise the 'N'
|
|
|
October 5th, 2007, 10:45 AM
|
#10
|
|
Most Reputable Member
SuperMember
Join Date: Nov 2005
Age: 28
Posts: 1,546
Thanks: 1
Thanked 11 Times in 11 Posts
Rep Altering Power: 0
|
Re: new page, with page numbers
wow, thats fantasically kind of you thank you - works great
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
|
[SOLVED] My page numbers are falling off of my website!!
|
62vye |
HTML, XHTML and CSS |
21 |
November 11th, 2008 11:33 AM |
|
Green bar of colour at bottom of page gets bigger the more page is extended
|
pixelgirl |
HTML, XHTML and CSS |
1 |
March 31st, 2008 09:27 PM |
|
internal navigation, Linking from one page to a specific div in another page.
|
Oak |
HTML, XHTML and CSS |
5 |
February 8th, 2008 06:54 PM |
|
Linking an outside page back to previous framed page
|
MJustison |
Your Design and Layout |
1 |
October 18th, 2007 01:49 PM |
|
A gap appears beween the Page Title and the Body Content of the page.
|
sovereign6 |
HTML, XHTML and CSS |
6 |
December 18th, 2006 03:14 PM |
|