Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
Old 08-16-2007, 10:03 AM How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
hey,

im trying to make a guestbook, and at the moment im just doing

[php]
while($row=mysql_fetch_array($result)) {
Code for gusstbook post...
$row['blah']l
}

to get the info and show every post.
but how do i do the thing, which shows say 10 posts per page and then creates a

<< < 1 2 3> >> like link system for them?

thanks,
Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 08-16-2007, 10:12 AM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
Yak Yak Yak Yak Yak

Posts: 593
Location: Rochester, MN
Trades: 0
It's called pagination. Below is a link to a very useful article with instructions. Good luck.

http://php.about.com/od/phpwithmysql...pagination.htm
__________________

Please login or register to view this content. Registration is FREE
neorunner is offline
Reply With Quote
View Public Profile
 
Old 08-16-2007, 10:24 AM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
okay... that would have been good if their servers dont try to parse .txt docs.. because when i click downlaod this doc.. i get this

http://php.about.com/gi/dynamic/offsite.htm?zi=1/XJ&sdn=php&zu=http%3A%2F%2Fidentity.st%2FAbout%2Fp aginate.php.txt

which shows connect error... grr stupid thing..

do you have any more, i think i got most of that i just want to get the whole script ..

ill do a bit of googling
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-16-2007, 10:37 AM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
The basics lie behind using an SQL quesry to limit the number of results coming through

To do this you need to pass a variable, usually using the GET method, along your pages to specify the start position and the number of results to show per page.

You can then use LIMIT <start_result>, <number_of_results> to get the correct results.

Then all you need to do is provide links on your site that point to the same page but with different GET variables, usually something like index.php?start=10, which will start getting the results at record 10.

Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Old 08-16-2007, 10:43 AM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I made a generic method long time ago, and I post it here.
It only create a navigation, like
Code:
 1  2  3  4 5 > >>
It don't output the datas from the db, but I have included a typical call below the function declaration.
At least, it should be enough to get you started.
PHP Code:
<?php
/**
 * Generate a navigation, given a number of elements, and how many elements we want per pages.
 * 
 * @param       int          $page              The current page in the pagination
 * @param        int          $tot              The total number of elements
 * @param        int          $nbPages      How many pages pages are there
 * @return      string    The html    block representing the pagination 
 */
function genNav($page,$tot,$nbPages){
    
$URI=$_SERVER['PHP_SELF'];
    
$delta=3;
    
$nav="";
    
$prev=$page-1;
    
$next=$page+1;
 
    if(
$page>0){
        
$nav.=<<<HTML
<a href="{$URI}/?page=$prev">&lt;&lt;</a>&nbsp;
HTML;
        }
 
    for(
$cpt=$page-$delta;$cpt<$nbPages && $cpt<=($page+$delta);$cpt++){
        if(
$cpt>=0){
            
$p=$cpt+1;
            if(
$cpt==$page){
                
$nav.=<<<HTML
<b>$p</b>&nbsp;
HTML;
            }
            else{
                
$nav.=<<<HTML
<a href="{$URI}/?page=$cpt">$p</a>&nbsp;
HTML;
            }
        }
    }
    if(
$page<$nbPages-1){
        
$nav.=<<<HTML
<a href="{$URI}/?page=$next">&gt;&gt;</a>&nbsp;
HTML;
    }
    return 
$nav;
}
 
$queryNum="select count(*) from ....";
$rnum=mysql_query($queryNum);
 
//current page
$page=(isset($_GET['page']))?$_GET['page']:0;
//Total number of elements to paginate
$cntElems=mysql_num_rows($rnum);
//How many elements per pages
$maxElems=5;
//Offset for db query
$offset=$page*$maxElems;
//How many pages to display all those elements
$nbPages=ceil($cntElems/$maxElems);
$htmlNav=genNav($page$cntElems$nbPages);
 
$query="select * from ... limit $maxElems offset $offset";
$r=mysql_query($query);
?>
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 08-16-2007 at 10:44 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-16-2007, 10:59 AM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
O.0 ookay... sorry Thierry i cant follow yours.. looks loads more advaced than the one im using what was on the about site. ill post it here

but im having some problems with it..


PHP Code:
<?php 
include ('config.php');
//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 

$pagenum 1

//Here we count the number of results 
//Edit $data to be your query 
$data mysql_query("SELECT * FROM guestbook") or die ("ERROR getting guestbook info:" mysql_error());
$rows mysql_num_rows($data); 
//This is the number of results displayed per page 
$page_rows 2
//This tells us the page number of our last page 
$last ceil($rows/$page_rows); 
//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum 1

$pagenum 1

elseif (
$pagenum $last

$pagenum $last

//This sets the range to display in our query 
$max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows
//This is your query again, the same one... the only difference is we add $max into it
$data_p mysql_query("SELECT * FROM guestbook  $max") or die ("ERROR getting guestbook info:" mysql_error());
//This is where you display your query results
while($info mysql_fetch_array$data_p )) 

echo 
'gestbook formatting <br />';
echo 
'USER:' .$row['user'];
echo 
'<br /> Date:'$row['date_added'];
echo 
'<br /> Email:'$row['email'];
echo 
'<br /> MSG:'$row['gbook_msg'];
echo 
'<br /> NAME:'$row['fullname'];
echo 
'<br /> IP:'$row['ip'];
echo  
'<br />end of this<br /><br />';

echo 
"<p>";
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1
{

else 
{
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo 
" ";
$previous $pagenum-1;
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";

//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last
{

else {
$next $pagenum+1;
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo 
" ";
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";

?>
its been copy and pasted and modified where it should..
its live here http://calm.dansgalaxy.co.uk/pagination.php

i have 7 entries in the DB and it set to show 2 per page..

but its all messed up and isnt working, if you look at the live version you should see what i mean..

whats wrong and how do i fix it?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE

Last edited by dansgalaxy; 08-16-2007 at 11:01 AM..
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-16-2007, 11:21 AM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Dan, I'm still at work.
Gimme 2 hours to get back home (I know...) and I'll look at it later tonight.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-16-2007, 12:37 PM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Ok nw

i wonder if you could get a job of just being on forums?... hmmm o.0

__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-16-2007, 02:55 PM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
Quote:
Originally Posted by dansgalaxy View Post
Ok nw

i wonder if you could get a job of just being on forums?... hmmm o.0

Likely not. However I do have a friend that hires people to maintain his forum site. So it is out there but not a common job.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 08-16-2007, 02:57 PM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Ok, I see soe problems with your file, and they are inattention problems.
Dan, you know better than that...

PHP Code:
if (!(isset($pagenum))) 

$pagenum 1

This implies REGISTER_GLOBALS on, which is not the default on PHP.
Use $_GET['pagenum'].

PHP Code:
$data_p mysql_query("SELECT * FROM guestbook  $max") or die ("ERROR getting guestbook info:" mysql_error());
//This is where you display your query results
while($info mysql_fetch_array$data_p )) 

echo 
'gestbook formatting <br />';
echo 
'USER:' .$row['user']; 
You should address $info['user'], not $row. I smell a copy/paste here.


PHP Code:
if ($pagenum == 1
{

else 
Why this block ?
It serves nothing...
PHP Code:
$previous $pagenum -1;
$next $pagenum +1;
if(
$pagenum 1){
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

if(
$pagenum<$last){
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";

I don't know what's in config.php, but I suspect the db connection.
If it's the fetching of $pagenum from $_GET, my bad, sorry for the 1st remark.

I've altered a version of your file that use an array as a source, and not a query. This file works ok for me. I include it so you can inspect it.
PHP Code:
<?php
//include ('config.php');

//This checks to see if there is a page number. If not, it will set it to page 1 
$pagenum=(isset($_GET['pagenum']))?$_GET['pagenum']:0;

//Here we count the number of results 
//Edit $data to be your query 
////$data = mysql_query("SELECT * FROM guestbook") or die("ERROR getting guestbook info:" . mysql_error());
////$rows = mysql_num_rows($data);

//This is the number of results displayed per page 
$page_rows 2;
//This tells us the page number of our last page 
////$last = ceil($rows / $page_rows);


//This sets the range to display in our query 
$max 'limit ' . ($pagenum) * $page_rows ',' $page_rows;

//This is your query again, the same one... the only difference is we add $max into it
////$data_p = mysql_query("SELECT * FROM guestbook  $max") or die("ERROR getting guestbook info:" . mysql_error());

//This is where you display your query results
////while ($info = mysql_fetch_array($data_p)) {

for($i=0;$i<8;$i++){
    
$aryDummy[]=array("user"=>"user_$i","email"=>"email_$i","gbook_msg"=>"msg_$i","fullname"=>"fullname_$i","ip"=>"192.168.0.$i");
}
$rows=sizeof($aryDummy);
$last ceil($rows $page_rows);
$cpt=0;

//this makes sure the page number isn't below 0, or more than our maximum pages 
$pagenum=($pagenum<0)?0:$pagenum;
$pagenum=($pagenum>$last)?$last:$pagenum;
foreach(
$aryDummy as $info){
    if(
$cpt<($pagenum $page_rows)){
        
//nothing
    
}
    elseif(
$cpt>=(($pagenum $page_rows)+$page_rows) ){
        
//nothing
    
}
    else{
        echo<<<HTML

Guestbook formatting:<br/>
USER: 
{$info['user']}<br/>
Email: 
{$info['email']}<br/>
MSG: 
{$info['gbook_msg']}<br/>
NAME:
{$info['fullname']}<br/>
IP: 
{$info['ip']}<br/>
end of this<hr/>

HTML;
    }
    
$cpt++;
}

echo 
"<p>";

// This shows the user what page they are on, and the total number of pages
$pg=$pagenum+1;
echo 
" --Page $pg of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
$previous $pagenum -1;
$next $pagenum +1;
if(
$pagenum 0){
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=0'> <<-First</a> ";
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

if(
$pagenum<$last-1){
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
echo
"</p>"
?>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-16-2007, 03:16 PM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Ok sorry im loads confussed...

yes config has the connection and DB select amoung other things..

yes the script was copy and paste (like i said off about which the first response posted)

and i the get thing is not me its whom ever wrote the orginal script.

i think who ever wrote it did know tha they could just do
if($x !== 5)
{stuff to do if $x isnt 5}

which is why they were doing

if($x == 5)
{
}
else { stuff to do if $x isnt 5}

ok, i tried to go thru the scrip and correct the bits you said but im stil getting exactly the same problem

and i got the script you posted at the end put its not getting the content..

and im probally looked very very stupid at the minute..

so how would i either fix the first script to work, or adapt the second to work for this?

Thanks
Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE

Last edited by dansgalaxy; 08-16-2007 at 03:25 PM..
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-16-2007, 04:03 PM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
and im probally looked very very stupid at the minute..
Nah, it happens to us all.

Can you post your modified script please ?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-16-2007, 04:06 PM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
here:
PHP Code:
<?php 
include ('config.php');
//This checks to see if there is a page number. If not, it will set it to page 1 
if (!isset($_GET['pagenum']))

$pagenum 1

//Here we count the number of results 
//Edit $data to be your query 
$data mysql_query("SELECT * FROM guestbook") or die ("ERROR getting guestbook info:" mysql_error());
$rows mysql_num_rows($data); 
//This is the number of results displayed per page 
$page_rows 2
//This tells us the page number of our last page 
$last ceil($rows/$page_rows); 
//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum 1

$pagenum 1

elseif (
$pagenum $last

$pagenum $last

//This sets the range to display in our query 
$max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows
//This is your query again, the same one... the only difference is we add $max into it
$data_p mysql_query("SELECT * FROM guestbook  $max") or die ("ERROR getting guestbook info:" mysql_error());
//This is where you display your query results
while($info mysql_fetch_array$data_p )) 

echo 
'gestbook formatting <br />';
echo 
'USER:' .$info['user'];
echo 
'<br /> Date:'$info['date_added'];
echo 
'<br /> Email:'$info['email'];
echo 
'<br /> MSG:'$info['gbook_msg'];
echo 
'<br /> NAME:'$info['fullname'];
echo 
'<br /> IP:'$info['ip'];
echo  
'<br />end of this<br /><br />';

echo 
"<p>";
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum !== 1
{
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo 
" ";
$previous $pagenum-1;
echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";

//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum !== $last
{
$next $pagenum+1;
$next $pagenum +1;
if(
$pagenum 1){
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
if(
$pagenum<$last){
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo 
" <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}  
}
?>
i probally missed loads

mark my work for me miss i didnt copy honest XD
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-16-2007, 04:09 PM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
<?php
include 'library/config.php';
include 'library/opendb.php';
// how many rows to show per page
$rowsPerPage = 20;

// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$query = " SELECT val FROM randoms " .
" LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

// print the random numbers
while($row = mysql_fetch_array($result))
{
echo $row['val'] . '<br>';
}
// ... more code here
?>


LOOK AT THIS
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Old 08-16-2007, 05:33 PM Re: How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Thanks, Thierry
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to How to show X results per page then do mor epages? - TP FOR WORKING ANSWER
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.11279 seconds with 12 queries