[SOLVED] new page, with page numbers

This is a discussion on "[SOLVED] new page, with page numbers" within the PHP Forum section. This forum, and the thread "[SOLVED] new page, with page numbers are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Closed Thread
 
LinkBack Thread Tools
  #1  
Old Oct 5th, 2007, 13:21
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,524
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
[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

PHP: Select all

<?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>';
}
?>
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
Old Oct 5th, 2007, 14:16
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 592
Blog Entries: 2
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....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Oct 5th, 2007, 14:17
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
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

PHP: Select all

<?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>';

?>
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Oct 5th, 2007, 14:25
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 592
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: new page, with page numbers

ok, looks like Rakuli beat me to it...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Oct 5th, 2007, 14:29
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: new page, with page numbers

Quote:
Originally Posted by c010depunkk View Post
ok, looks like Rakuli beat me to it...
Sorry C010depunk Always rearing for a pagination script.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Oct 5th, 2007, 14:32
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 592
Blog Entries: 2
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
Old Oct 5th, 2007, 14:34
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,524
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: new page, with page numbers

thank you for your help, but i am recieving this

Quote:
Notice
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
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
Old Oct 5th, 2007, 14:38
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
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.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #9  
Old Oct 5th, 2007, 14:41
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: new page, with page numbers

Okay, change this line

PHP: Select all

$sql "SELECT * FROM form order by date desc LIMIT " . ($pagenum 1) * $perPage ", $perPage"
to:

PHP: Select all

$sql "SELECT * FROM form order by date desc LIMIT " . ($pageNum 1) * $perPage ", $perPage"
I forgot to capitalise the 'N'
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #10  
Old Oct 5th, 2007, 14:45
saltedm8's Avatar
SuperMember

SuperMember
Join Date: Nov 2005
Location: here
Age: 27
Posts: 1,524
Blog Entries: 2
Thanks: 1
Thanked 11 Times in 11 Posts
Re: new page, with page numbers

wow, thats fantasically kind of you thank you - works great
Last Blog Entry: Strict and Transitional Doctype's (Sep 12th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Closed Thread

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] My page numbers are falling off of my website!! 62vye Web Page Design 21 Nov 11th, 2008 16:33
Green bar of colour at bottom of page gets bigger the more page is extended pixelgirl Web Page Design 1 Apr 1st, 2008 01:27
internal navigation, Linking from one page to a specific div in another page. Oak Web Page Design 5 Feb 8th, 2008 23:54
Linking an outside page back to previous framed page MJustison Starting Out 1 Oct 18th, 2007 17:49
A gap appears beween the Page Title and the Body Content of the page. sovereign6 Web Page Design 6 Dec 18th, 2006 20:14


All times are GMT. The time now is 13:20.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved