Current location: Hot Scripts Forums » Programming Languages » PHP » Pagination


Pagination

Reply
  #1 (permalink)  
Old 05-24-09, 06:18 PM
SniperTowers SniperTowers is offline
Wannabe Coder
 
Join Date: Dec 2008
Posts: 143
Thanks: 5
Thanked 0 Times in 0 Posts
Pagination

I have this script:

PHP Code:

<?php

session_start
();
require (
"connect.php");
require(
"functions.php"); 
include(
"template/header.php");

    
$username $_SESSION['username'];
    
$uid $_SESSION['id'];

    
$cat $_GET['layouts'];

    
$uid $_SESSION['id'];
    if(
$cat == "none"){
    
$result mysql_query("SELECT * from layouts ORDER BY `id` ASC");   
    }else{
    
$result mysql_query("SELECT * from layouts WHERE categoryID='$cat'");
    }

    while(
$list mysql_fetch_array$result )){

$no_results TRUE;   // No results found yet
$howmany    4;     // Return 10 results per query
 
// Set default starting point of query to 0, or, if set, to $_GET['rs']
$row_start  = (isset($_GET['rs'])) ? $_GET['rs'] : 0;
 
 
// Do our SQL query, with something like LIMIT 0, 10
$sql    "SELECT SQL_CALC_FOUND_ROWS title, previewlink, image FROM layouts LIMIT "$row_start .", "$howmany ."";
$result mysql_query($sql);
 
 
// Get the number of rows that would have been returned WITHOUT a limit clause, to be used later for paging.
$count_sql        "SELECT FOUND_ROWS() AS total";
$count_sql_result mysql_query($count_sql);
$count_row       mysql_fetch_array($count_sql_result);
$count_result       $count_row['total'];
 
// Start looping through our result set
while($row mysql_fetch_array($result)) {
    
$no_results FALSE;
 
    
// Save results of query to $line_output
    
$line_output .= "


       <style>.button {
        margin-top: 0px;
    display:block;
    width:250px;
    height:50px;
    text-indent:-9999px;
    margin-left: 300px;
    }
    .button a {
    display:block;
    width:100%;
    height:100%;
    background:url(images/save.png) no-repeat top left;
    outline:none;
    }
    .button a:hover {
    background-position:0 -50px;
    }
    .button2 {
    margin-top: 0px;
    display:block;
    width:250px;
    height:50px;
    text-indent:-9999px;
    margin-left: 300px;
    }
    .button2 a {
    display:block;
    width:100%;
    height:100%;
    background:url(images/preview.png) no-repeat top left;
    outline:none;
    }
    .button2 a:hover {
    background-position:0 -50px;
    }
    #cats {
    width: 100px;
    height: 100px;
    margin: 5px;
    float: left;
    margin-left: 50px;
    }
    #layouts {
    background: url('images/backdrop.png');
    width: 609px;
    height: 190px;
    margin-left: 50px;
    }
    #next {
    margin-left: 50px;
    }
    </style>"
;
        echo 
"<div id=\"layouts\"><br/><br/><div id=\"cats\"><img align=\"left\" src=\"images/";
        echo 
$row['image'];
        echo 
"\" width=\"100px\" height=\"100px\" border=\"1px\">";
        echo 
$row['title'];
        echo 
"</div><p class=\"button2\">";
        echo 
"<a href=\"";
    echo 
"twitterpreview.php?image=images/";
        echo 
$row['image'];
        echo 
"\" target=_blank\">";
        echo 
"Preview Twitter Background";
        echo 
"</a> ";
        echo 
"</p>";
        echo 
"<p class=\"button\">";
        echo 
"<a href=\"";
        echo 
"downloadimage.php?image=images/";
        echo 
$row['image'];
        echo 
"\">";
        echo 
"Save Image";
        echo 
"</a> ";
        echo 
"</p></div>";
}}
// Don't bother building paging if we don't have records
if ($no_results) {
    
$line_output "No records found...";
    
$page_output "";
}
else {
    
// Build <prev> and <next> links and save to $page_output
    
$rs_prev $row_start $howmany// where would prev page start, given current start less no. of records
    
$rs_next $row_start $howmany// where would next page start, given current start plus no. of records
 
    // If for some reason the next <prev> starting point is negative, do not display <prev>
    // This happens when our current starting point is already 0
    // This may happen if some smartass manually changes the rs= bit in the url
    
$page_output_prev     = ($rs_prev 0) ? "" "<div id=\"next\"><a href='?layouts=".$cat."?rs=".$rs_previous."'>Previous</a></div>";
 
    
// Will the next page jump start point exceed the number of records returned?
    // If so, don't display <next>'
    
$page_output_next     = ($rs_next >= $count_result) ? "" "<div id=\"next\"><a href='?layouts=".$cat."?rs=".$rs_next."'>Next</a></div>";
 
    
// Just something to put between <prev> & <next>, IF they are both active
    
if (($page_output_prev == "") || ($page_output_next == "")) {$page_output_breaker "";}
    else { 
$page_output_breaker " || ";}
 
    
// Build final paging output
    
$page_output $page_output_prev $page_output_breaker $page_output_next;
}
 
// Write the outputs
echo $line_output;
echo 
$page_output;
 


    
    include(
"template/footer.php");
?>
It basically takes the info from the database and paginates it. Each set of data is in a category however the url is like
Code:
http://www.mysite.com/layouts.php?layouts=14
when I would like it to go to the next page it goes to
Code:
http://www.mysite.com/layouts.php?layouts=14?rs=4
and then to
Code:
http://www.freetweetgraphics.com/layouts.php?layouts=14?rs=4?rs=4
etc. etc. all I get is 4 display results (which are the same each time) on each page.

How do i get it to update to the next 4 peices of data from each category?
Reply With Quote
  #2 (permalink)  
Old 05-24-09, 09:21 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Try this;
PHP Code:

// If for some reason the next <prev> starting point is negative, do not display <prev>
    // This happens when our current starting point is already 0
    // This may happen if some smartass manually changes the rs= bit in the url
    
$page_output_prev     = ($rs_prev 0) ? "" "<div id=\"next\"><a href='?layouts=".$cat."&rs=".$rs_previous."'>Previous</a></div>";
 
    
// Will the next page jump start point exceed the number of records returned?
    // If so, don't display <next>'
    
$page_output_next     = ($rs_next >= $count_result) ? "" "<div id=\"next\"><a href='?layouts=".$cat."&rs=".$rs_next."'>Next</a></div>"
Reply With Quote
  #3 (permalink)  
Old 05-25-09, 05:02 AM
SniperTowers SniperTowers is offline
Wannabe Coder
 
Join Date: Dec 2008
Posts: 143
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks however I have another problem now. Say if I added a layout to category 14, 12 and 9 all three layouts will show up in each category. Any ideas?
Reply With Quote
  #4 (permalink)  
Old 05-25-09, 09:57 AM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
PHP Code:

// Do our SQL query, with something like LIMIT 0, 10
$sql    "SELECT SQL_CALC_FOUND_ROWS title, previewlink, image FROM layouts WHERE categoryID='$cat' LIMIT "$row_start .", "$howmany ."";
$result mysql_query($sql); 
Should do it;
Reply With Quote
  #5 (permalink)  
Old 05-25-09, 10:08 AM
SniperTowers SniperTowers is offline
Wannabe Coder
 
Join Date: Dec 2008
Posts: 143
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks almost solved. When I go to a category click next then go to previous I get this:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/virapcom/public_html/layouts.php on line 40

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/virapcom/public_html/layouts.php on line 19
No records found...

Line 40 is while($row = mysql_fetch_array($result)) {

Link 19 is while($list = mysql_fetch_array( $result )){
Reply With Quote
  #6 (permalink)  
Old 05-25-09, 10:31 AM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
What is your URL after you click "previous"?

It should read: http://server/page.php?layouts=14&rs=4

if layouts isn't populated with a number, it will give you that error.
Reply With Quote
  #7 (permalink)  
Old 05-25-09, 10:39 AM
SniperTowers SniperTowers is offline
Wannabe Coder
 
Join Date: Dec 2008
Posts: 143
Thanks: 5
Thanked 0 Times in 0 Posts
Reply With Quote
  #8 (permalink)  
Old 05-25-09, 10:47 AM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Try this:
PHP Code:

 // If for some reason the next <prev> starting point is negative, do not display <prev>
    // This happens when our current starting point is already 0
    // This may happen if some smartass manually changes the rs= bit in the url
    
$page_output_prev     = ($rs_prev 0) ? "" "<div id=\"next\"><a href='?layouts=".$cat."&rs=".$rs_prev."'>Previous</a></div>";
 
    
// Will the next page jump start point exceed the number of records returned?
    // If so, don't display <next>'
    
$page_output_next     = ($rs_next >= $count_result) ? "" "<div id=\"next\"><a href='?layouts=".$cat."&rs=".$rs_next."'>Next</a></div>"
Edit: the $page_output_prev line had rs being passed as $rs_previous; The actual variable was $rs_prev ;
Reply With Quote
  #9 (permalink)  
Old 05-25-09, 11:31 AM
SniperTowers SniperTowers is offline
Wannabe Coder
 
Join Date: Dec 2008
Posts: 143
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a 'general' pagination function please macintosh PHP 1 03-21-09 06:36 PM
Pagination Help Nikas PHP 2 03-10-08 05:38 PM
pagination pumpernickles pilch PHP 0 05-17-06 11:24 AM
Pagination Problem lppa2004 PHP 1 03-24-06 07:54 PM
Pagination karlcore Hot Scripts Forum Questions, Suggestions and Feedback 0 09-17-05 08:03 AM


All times are GMT -5. The time now is 08:36 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.