Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Pagination Error

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pagination Error

    ok, I have been trying to get a pagination script running for a while now and can't seem to get it. Here is my code:
    PHP Code:
    function getLinkPage() {

    // Pagination Script
    if($_GET['page_num']) { // Is the page defined?

        
    $page_num $_GET['page_num']; // Set page defined
        
    } else {

        
    $page_num 1// Default page is 1
        
    }
    $max 20// Max results on a page
    $cur = (($page $max) - $max); // Works out what results to show


    $count_total mysql_query("SELECT * FROM links"); // Get data from database
    $count_total mysql_num_rows($count_total); // Count lines in database

    $total_pages ceil($count_total $max); // Total results divided by max to display

    if($page 1) { // Is the page # more than one?

        
    $prev = ($page 1); // If yes, take one away from current page
        
    $prevTxt '<a href="?page_num='.$prev.'">&lt;&lt;Previous</a>'


    $nav ' ';

    for(
    $i == 1$i <= $total_pages$i++) // For each page number

        
    if($page == $i) { // If page = current page
        
            
    $nav .= '<b>'.$i.'</b>'// Echo page in bold
        
    } else {
        
            
    $nav .= ' <a href="">'.$i.'</a> '// Echo link to the page
        
    }
        
    if(
    $page $total_pages) { // Is there another page?

        
    $next = ($page 1); // If so, add 1 to current page
        
    $nextTxt '<a href="">Next>></a>'// Echo next page link

    Here is the code where I run the sql query:
    PHP Code:
    $max 20// Max results on a page
    $cur = (($page_num $max) - $max); // Works out what results to show

    // Query to get links from database
    $sql "SELECT * FROM `links` ORDER BY `count` ASC LIMIT $cur,$max";
    $result mysql_query($sql) or die ('Error Getting Links! <br />' .mysql_error()); 
    The error I get is this: "Error Getting Links!
    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 '-20,20' at line 1". Obviously it has something to do with my $cur,$max statement to limit the results. I am thinking that maybe it is not setting the page number to 1 and therefore multiplying 20 by 0 and subtracting 20, hence -20. Could the problem be that I also use ?page=whatever to get pages, not just page numbers? Thanks in advance
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Anyone have an idea of what could be wrong?
    Thanks DD, you saved me countless times

  3. #3
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    try using single quotes ' instead of backticks `

    should help some.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  4. #4
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by boxxertrumps View Post
    try using single quotes ' instead of backticks `

    should help some.
    I don't see anywhere that I used backticks where I need to use single quotes. I only used backticks in the sql queries
    Thanks DD, you saved me countless times

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm almost positive that is NOT the problem.

    The problem is with your min/max.... you can't start at a negative entry.
    You're right. You start with 0, not 1. You could just multiply by $pagenum+1, or you could just figure out a better way to set it in the first place. The default should be 1, so I guess you'd have it set correctly if you have 2, 3, ..., etc. So... I think the solution is just making it default to 1 if it isn't set.

    $page_num = $_GET['page_num'] ? is_numeric($_GET['page_num']) : 1;

    (This is a compact if/else statement: $x = this IF condition ELSE this)

    You already have something like this, but might not be working. Just using if ($x) isn't a very good way to check.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I did what you said and now I get: "
    Warning: Division by zero in /home/bntqann/public_html/testing/cms/functions.php on line 61". Here is lines 58-61:
    PHP Code:
    $count_results mysql_query("SELECT * FROM `links` ORDER BY id DESC"); // Get data from database
    $count_total mysql_num_rows($count_results); // Count lines in database

    $total_pages ceil($count_total $max); // Total results divided by max to display 
    I should be getting results from my query because there is a link in the database. Any ideas?
    Thanks DD, you saved me countless times

  7. #7
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    sorry for misleading, i thought backticks were invalid in sql...
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Hm.... that would mean $max is 0, clearly. The question is why and how to fix it.


    OH! Obviously...

    $page_num = [something];
    ....
    ($page * $max)

    Erm.... $page or $page_num? You gotta pick... heh.
    That should fix all of it.

    And note that my line of code may not be needed any more, though it's a lot cleaner than the if/else you have and more accurate. If someone chose to input ?page_num=hello, that would give odd results.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok, I did what you said and it took the error out, but the pages are not being displayed. Here is my updated code:
    PHP Code:
    // Pagination for counter
    function getLinkPage() {

    $max 20;

    $page_num = ($page_num $max);



    $count_results mysql_query("SELECT * FROM `links` ORDER BY id DESC"); // Get data from database
    $count_total mysql_num_rows($count_results); // Count lines in database

    $total_pages ceil($count_total $max); // Total results divided by max to display

    if($page_num 1) { // Is the page # more than one?

        
    $prev = ($pag_nume 1); // If yes, take one away from current page
        
    $prevTxt '<a href="?page_num='.$prev.'">&lt;&lt;Previous</a>'


    $nav ' ';

    for(
    $i == 1$i <= $total_pages$i++) // For each page number

        
    if($page_num == $i) { // If page = current page
        
            
    $nav .= '<b>'.$i.'</b>'// Echo page in bold
        
    } else {
        
            
    $nav .= ' <a href="">'.$i.'</a> '// Echo link to the page
        
    }
        
    if(
    $page_num $total_pages) { // Is there another page?

        
    $next = ($page_num 1); // If so, add 1 to current page
        
    $nextTxt '<a href="">Next>></a>'// Echo next page link
    }


    I echo the pages like this:
    PHP Code:
    echo 'Pages: '.$prevTxt $nav $nextTxt.''
    Any idea why its not displaying the page? Thanks for the help
    Thanks DD, you saved me countless times

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    $nextTxt = '<a href="">Next>></a>'; // Echo next page link
    ...an actual HREF might be helpful
    $nav .= ' <a href="">'.$i.'</a> '; // Echo link to the page
    ...same


    From what I can tell... that is NOT how you 'echo the pages'... in fact, that is just echoing links... broken ones at that.
    You're just echoing links to the pages... not text of the pages themselves.
    Is this for a TOC type script? Why paginate a TOC?
    I'm a bit confused.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •