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
Help with listing page numbers (pagination)
Old 02-17-2010, 03:09 PM Help with listing page numbers (pagination)
Novice Talker

Posts: 8
Name: Alan
Trades: 0
I am having trouble listing the page numbers at the bottom of my page. The site is ijustwanttotellyou.com. Currently, every page is listed at the bottom surrounded by 'Previous' and 'Next'. What I'm trying to do is, like most sites with multiple pages, have 'Previous 2 3 4 5 .... 400 Next' (depending on what page is being viewed).

This is my current code for the page numbers and I can't figure out what to change:

PHP Code:
if ($back >=0) {

$links .= '<a href="?page='.$back.$catdynamic.'"><p class="pagenumber">Previous Page</p></a> ';



$i 0;

$x 1;


for (
$i=0;$i $total_rows;$i=$i+$limit){


if(
$i != $eu){

$links .= '<a href="?page='.$i.$catdynamic.'"><span class="white">'.$x.'</a></span> ';

}

else { 

$links .= '<b><span class="grey">'.$x.'</b></span> ';

}


$x $x+1;


}


if (
$whathis $total_rows) {

$links .= ' <a href="?page='.$next.$catdynamic.'"><p class="anynamehere">Next Page</p></a>';



Last edited by chrishirst; 02-17-2010 at 03:47 PM..
ijustwanttotell is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-17-2010, 06:41 PM Re: Help with listing page numbers (pagination)
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
this was asked a while back...

I replied with: http://www.crankberryblog.com/2009/s...ing-pagination

basically its not having the .... but it slides around with the page so if you're page 10 it'll start from 5 6 7... etc... just like Google.. see if that works for you
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 02-18-2010, 11:18 AM Re: Help with listing page numbers (pagination)
Novice Talker

Posts: 8
Name: Alan
Trades: 0
Thanks for the link, I tried to make that work, but I couldn't figure out the variables and how to adjust them for my database and script...I'm just having the hardest time with this simple task. So frustrating.
ijustwanttotell is offline
Reply With Quote
View Public Profile
 
Old 02-18-2010, 01:15 PM Re: Help with listing page numbers (pagination)
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
you're grabbing content from a db right? post your code so far including the fetching part
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 02-18-2010, 02:52 PM Re: Help with listing page numbers (pagination)
Novice Talker

Posts: 8
Name: Alan
Trades: 0
PHP Code:
//Pagination Stuff*****************
if (!empty($_GET['page'])){
$start $_GET['page'];
}
else {
$start 0;
}
if (!(
$start 0)) {
$start 0;
}
 
$eu = ($start 0);
$limit 10;
$whathis $eu $limit;
$back $eu $limit;
$next $eu $limit;
 
if(!
$cat){
$comp_select1 mysql_query("SELECT * FROM `comps` WHERE `accepted`='Yes'");
$total_rows mysql_num_rows($comp_select1);
 
}
 
if(
$cat){
$comp_select1 mysql_query("SELECT * FROM `comps` WHERE `accepted`='Yes' and cat='$cat'");
$total_rows mysql_num_rows($comp_select1);
 
$cats mysql_query("select * from cats where id='$cat'");
$fcats mysql_fetch_array($cats);
$catname $fcats['cat'];
 
echo 
"<span class='catname'>Category :: $catname</span>";
 
}
$voting_array mysql_fetch_array(mysql_query("SELECT * FROM `votes` WHERE `ip`='$ip'"));
$vote_explode explode(','$voting_array['comp_id']);
 
 
foreach(
$vote_explode as $vote_ex){
 
$vote_ex explode(":"$vote_ex);
 
$compnum $vote_ex[0];
$compvote $vote_ex[1];
$compvotearray[$compnum] = $compvote;

----------Then for displaying the page numbers--------------

PHP Code:
 
if ($back >=0) {
$links .= '<a href="?page='.$back.$catdynamic.'"><p class="anynamehere">Previous Page</p></a> ';
}
 
$i 0;
$x 1;
 
for (
$i=0;$i $total_rows;$i=$i+$limit){
 
if(
$i != $eu){
$links .= '<a href="?page='.$i.$catdynamic.'"><span class="white">'.$x.'</a></span> ';
}
else { 
$links .= '<b><span class="grey">'.$x.'</b></span> ';
}
 
$x $x+1;
 
}
 
if (
$whathis $total_rows) {
$links .= ' <a href="?page='.$next.$catdynamic.'"><p class="anynamehere">Next Page</p></a>';

 
echo 
'<br /><br />';
 
 
echo 
''.$links.''



Any help is already greatly appreciated, thank you!

Last edited by chrishirst; 02-18-2010 at 02:59 PM..
ijustwanttotell is offline
Reply With Quote
View Public Profile
 
Old 02-18-2010, 05:52 PM Re: Help with listing page numbers (pagination)
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
try this

PHP Code:
<?php


//DB Stuff*****************

if (!$cat$comp_select1 mysql_query("SELECT * FROM comps WHERE accepted='YES'");
else  
$comp_select1 mysql_query("SELECT * FROM comps WHERE accepted='YES' AND cat='$cat'");

//Total Rows
$total_rows mysql_num_rows($comp_select1);
 

$cats mysql_query("SELECT * FROM cats where id='$cat'");
$fcats mysql_fetch_array($cats);
$catname $fcats['cat'];
 
echo 
"<span class='catname'>Category :: $catname</span>";
 
}
$voting_array mysql_fetch_array(mysql_query("SELECT * FROM votes WHERE ip='$ip'"));
$vote_explode explode(','$voting_array['comp_id']);
 
 
foreach(
$vote_explode as $vote_ex){
 
$vote_ex explode(":"$vote_ex);
 
$compnum $vote_ex[0];
$compvote $vote_ex[1];
$compvotearray[$compnum] = $compvote;
}  


//Page total is the total number of items that you have for the user to go
//through. You can define this yourself if you have a lot of static content
//of feed your database row count into here.

$page_total $total_rows;

//Page Show is the number of items you want to show per page. You should add
//this number in yourself. Depending on how you display your results. The default
//is set to show 20 items per page.

$page_show 10;

//Page Pages basically how many page options you want to show to the user per
//time. If you have 20 pages you can choose to show 5 pages to select per time.
//REMINDER: the number you select, if its even it will go one up. So if you choose
//8 it will actually show 9 options, this is because the dynamic scroller will
//always be in the center and even numbers don't really have a center.

$page_pages 5;
$page_align 'left';
$page_extention $catdynamic;

//End – Adding Variables






//The script below grabs the current page they're on
if (isset($_GET['p']) && is_numeric($_GET['p'])) $current_pageNum $_GET['p'];
else 
$current_pageNum 1;

//Forming the Extention
if (isset($page_extention) && $page_extention!=""$page_extention '&' $page_extention;





//Pagination Function
function display_pages($current_pageNum$page_total$page_show 20$page_pages$current_page$page_align$page_extention NULL) {
        
//Don't Show pagination if there isn't more than 1 page
        
if ($page_total>$page_show) {
                
//If Page number exceeds then just show page 1
                
if ($current_pageNum>$page_total || $current_pageNum<1$current_pageNum 1;
               
                
//Getting Page Alignment
                
switch ($page_align) {
                        case 
'left':
                                
$div_align 'left';
                                break;
                        case 
'center':
                                
$div_align 'center';
                                break;
                        case 
'right':
                                
$div_align 'right';
                                break;
                        default:
                                
$div_align 'left';
                                break;
                }
               
                
//Start Div
                
echo '<div align="'.$div_align.'">Pages: ';
               
                
//Setting Var
                
$page_prev $current_pageNum-1;
                
$page_next $current_pageNum+1;
               
                
//Displaying Prev Page
                
if ($current_pageNum!=1) echo '<a href="'.$current_page.'?p='.$page_prev.$page_extention.'">&laquo; </a>';
               
                
//Making The Pages
                
$page_count ceil($page_total/$page_show);
               
                
//For Page Pages
                
$page_pages;
                
$page_padding floor($page_pages/2);
               
                
//If There Are More Pages
                
if ($page_count>$page_pages) {
                        
//Measuring Page Start
                        
if ($current_pageNum>=($page_padding+1)) {
                                
//If They're Near The End
                                
if ($current_pageNum>($page_count-$page_padding)) {
                                        
//Where To Start Page
                                        
$page_start $page_count-($page_padding*2);
                                        
$page_end $page_count;
                                } else {
                                        
$page_start $current_pageNum-$page_padding;
                                        
$page_end $current_pageNum+$page_padding;
                                }
                        } else {
                                
$page_start 1;
                                
$page_end = ($page_padding*2)+1;
                        }
                } else {
                        
//The Vars
                        
$page_start 1;
                        
$page_end $page_count;
                }
               
                for(
$t=$page_start$t<=$page_end$t++) {
                        
//Displaying Pages
                        
if ($current_pageNum==$t) echo '<strong>';
                        echo 
'<a href="'.$current_page.'?p='.$t.$page_extention.'">'.$t.' </a>';
                        if (
$current_pageNum==$t) echo '</strong>';
                }
               
                
//Displaying Next Page
                
if ($current_pageNum!=$page_count) echo '<a href="'.$current_page.'?p='.$page_next.$page_extention.'">&raquo; </a>';
               
                
//Displaying Last Page
                
echo '<a href="'.$current_page.'?p='.$page_count.$page_extention.'">Last </a>
'
;
               
                
//Page Number Results
                
$page_display_low = (($current_pageNum-1)*$page_show)+1;
                
$page_display_high = ($page_display_low+$page_show)-1;
                if (
$page_display_high>$page_total$page_display_high $page_total;
                echo 
'<font style="font-size: 9px;">(Displaying: '.$page_display_low.'-'.$page_display_high.' of '.$page_total.')</font>';
               
                
//End Div
                
echo '</div>';

                
//Offset
                
$page_final = ($current_pageNum-1)*$page_show;
               
                return 
$page_final;
        } else {
                return 
0;       
        }
}







//Displaying the Pagination
$page_offset display_pages($current_pageNum$page_total$page_show$page_pages$current_page$page_align$page_extention);


?>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 02-19-2010, 10:41 AM Re: Help with listing page numbers (pagination)
Novice Talker

Posts: 8
Name: Alan
Trades: 0
Wow, thank you so much. It works almost perfectly!

One (hopefully) simple question. When I click page 2, or page 3, or page 4, etc, it doesn't take me to those pages. What it does, is takes me to (what should be page X, ?page=x) but its just a new page with submission 2, or 3, or 4 as the first submission. Not actually the 2nd 3rd or 4th page of 10 submissions. Does that make sense?
ijustwanttotell is offline
Reply With Quote
View Public Profile
 
Old 02-19-2010, 11:39 AM Re: Help with listing page numbers (pagination)
Novice Talker

Posts: 8
Name: Alan
Trades: 0
Based on how the old page links worked, I guess this is how it needs to work:

When clicking page 2, it needs to send the user to /?page=20 instead of /?page=2

What variables need to change to make this possible? I tried messing with it but was unsuccessful.
ijustwanttotell is offline
Reply With Quote
View Public Profile
 
Old 02-19-2010, 11:58 AM Re: Help with listing page numbers (pagination)
Mad182's Avatar
Skilled Talker

Posts: 54
Name: Madars
Location: Latvia
Trades: 0
Just add *10 to variable before mysql query...
__________________
There's no place like 127.0.0.1

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Mad182 is offline
Reply With Quote
View Public Profile Visit Mad182's homepage!
 
Old 02-19-2010, 12:25 PM Re: Help with listing page numbers (pagination)
Novice Talker

Posts: 8
Name: Alan
Trades: 0
I believe I tried that, and had no luck. Unless I put it on the wrong variable. Which specifically?
ijustwanttotell is offline
Reply With Quote
View Public Profile
 
Old 02-19-2010, 12:52 PM Re: Help with listing page numbers (pagination)
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
when you query the display part you need

"SELECT * FROM where_ever WHERE blah='blah' LIMIT $page_offset, $page_show"

these two variables are already defined
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 02-19-2010, 01:11 PM Re: Help with listing page numbers (pagination)
Novice Talker

Posts: 8
Name: Alan
Trades: 0
i tried adding *10 to each mysql query. No luck.

I know I'm sounding like an idiot here, but I can't figure it out.

I got the "next" page button to work (by jumping to ?page10 instead of ?page1) but I can't get the 1 2 3 4 5 6 buttons to do the same.

Last edited by ijustwanttotell; 02-19-2010 at 01:13 PM..
ijustwanttotell is offline
Reply With Quote
View Public Profile
 
Old 02-19-2010, 01:49 PM Re: Help with listing page numbers (pagination)
Novice Talker

Posts: 8
Name: Alan
Trades: 0
I added *10 to $page_extention and it turns 1 2 3 4 into links to page 10 20 30 and 40.

problem is, it takes you to the correct selection of submissions, but it changes the page numbers to 10 20 30 or 40....F**K

I just need page 2 to link to page 20, page 3 to link to page 30, etc while maintaining page 2 3 4 5 and so on at the bottom instead of jumping to 20 21 22 23

also, tried adding LIMIT $page_offset, $page_show to the query but it doesnt seem to do anything?

Last edited by ijustwanttotell; 02-19-2010 at 03:05 PM..
ijustwanttotell is offline
Reply With Quote
View Public Profile
 
Old 02-19-2010, 06:17 PM Re: Help with listing page numbers (pagination)
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
the LIMIT is to be added to the query that's used for displaying your content... which query is it?

you don't need to edit:

PHP Code:
//Total Rows
$total_rows mysql_num_rows($comp_select1);
 

$cats mysql_query("SELECT * FROM cats where id='$cat'");
$fcats mysql_fetch_array($cats);
$catname $fcats['cat'];
 
echo 
"<span class='catname'>Category :: $catname</span>"
the script you gave me didn't have the query for the produced content... where is that?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 02-19-2010, 06:30 PM Re: Help with listing page numbers (pagination)
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
here's how you should lay it out

$query = querying what you're looking at
$total_rows = total_rows of your query

plug these variables into the page script

$page_total = $total_rows

configure the other variables if you need but not necessary

include the page function

display the pages with
//Displaying the Pagination
$page_offset = display_pages($current_pageNum, $page_total, $page_show, $page_pages, $current_page, $page_align, $page_extention);



HERE'S THE KEY... when you use the function it returns a number to $page_offset... which is what content to load (like page 1 which is 1 - 10 or page 2 which is 11 - 20).

Now you query for the content. You can't do it before because you didn't have the offset

So now you query for the specific content you want to get:

$query = "SELECT * FROM table WHERE blah ORDER BY blah LIMIT $page_offset, $page_show";

now you display the content

while ($content = mysql_fetch_array($query)) {

}


here's the exact script on one of my other sites

http://www.shirlz.com/food-and-drinks-ct1.htm?p=7
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help with listing page numbers (pagination)
 

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 0.68034 seconds with 12 queries