need help with Pagination

Discussion in 'PHP' started by izlik, Nov 19, 2007.

  1. #1
    i have this code bellow that is a tag system for my image hosting page. if you got to http://filefrog.net/new.php?s=300 and press the tag "games" (has the ammount of images needed to show what i mean) it will show you all the images tagged with "games", but if you then go to the bottom of the page and press "next" it wont go to the next page, it stays on page one and i dont know why, and i cant solved the problem.

    i hope someone could help me solve this problem.

    <?
    
    <?php
    
    $con = mysql_connect("localhost","asd","das") OR die('Could not connect: ' . mysql_error());
    mysql_select_db("asd", $con);
    
    //This checks to see if there is a page number. If not, it will set it to page 1
    if (!(isset($pagenum)))
    {
    $pagenum = 1;
    }
    
    $result = mysql_query("
        SELECT *
        FROM `images` 
        WHERE `tags`
        LIKE '%" . mysql_real_escape_string($_GET['tag']) . "%'
        ORDER BY views
        $max
    ")
    OR die(mysql_error());
    $rows = mysql_num_rows($result); 
    
    //This is the number of results displayed per page
    $page_rows = 30; 
    
    $last = ceil($rows/$page_rows); 
    
    if ($pagenum < 1)
    {
    $pagenum = 1;
    }
    elseif ($pagenum > $last)
    {
    $pagenum = $last;
    } 
    
    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
    
    while($row = mysql_fetch_array($result))
    {
    	
        echo '<div style="float:left;width:25%"><a href="http://filefrog.net/show.php/' .$row['id'].'_' .$row['name'].'"><img src="http://www.filefrog.net/out.php/t' .$row['id'].'_' .$row['name'].'"></a></div>'; 
        echo "<br>\n";
     
    }	
    
    ?>
    <div style="clear:both"></div>
    <?	
    
        echo " --Page $pagenum of $last-- <p>";
       
        if ($pagenum == 1)
    {
    }
    else
    {
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&amp;tag={$_GET['tag']}'> <<-First</a> ";
    echo " ";
    $previous = $pagenum-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&amp;tag={$_GET['tag']}'> <-Previous</a> ";
    }
    
    //just a spacer
    echo " ---- ";
    
    if ($pagenum == $last)
    {
    }
    else {
    $next = $pagenum+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&amp;tag={$_GET['tag']}'>Next -></a> ";
    echo " ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&amp;tag={$_GET['tag']}'>Last ->></a> ";
    }
    ?>
    PHP:

     
    izlik, Nov 19, 2007 IP
  2. redlorry919

    redlorry919 Peon

    Messages:
    384
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Your $max var is after your query call and hence it wont LIMIT the results. You'd need to place an additional LIMIT query below this line to get the page results:

    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
     
    redlorry919, Nov 20, 2007 IP
  3. *louie*

    *louie* Peon

    Messages:
    48
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    When you are in trouble try echo the sql to see what's wrong:
    
    echo $result;
    
    PHP:
     
    *louie*, Nov 20, 2007 IP
  4. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #4
    thanks! :) i managed to get it to work so if you go to the page in my first post and press the tag and the next button in the bottom it loads page 2 now, BUT it shows the same images on both pages, why is this ? :(
     
    izlik, Nov 21, 2007 IP
  5. redlorry919

    redlorry919 Peon

    Messages:
    384
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Looks fine to me, I'm assuming you've resolved it?
     
    redlorry919, Nov 21, 2007 IP
  6. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #6
    when i switch page i se the same images, dont you do that ?
     
    izlik, Nov 21, 2007 IP
  7. redlorry919

    redlorry919 Peon

    Messages:
    384
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    redlorry919, Nov 21, 2007 IP
  8. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #8
    ah, your checking the wrong page! :)

    if you got to http://filefrog.net/new.php?s=300 and press the tag "games" (has the ammount of images needed to show what i mean) it will show you all the images tagged with "games", but if you go to the bottom and press "next" it switches page but shows the same images on both pages.
     
    izlik, Nov 21, 2007 IP
  9. redlorry919

    redlorry919 Peon

    Messages:
    384
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I'm there now. It must still be a LIMIT issue or like. Can you post the updated code.

    Also, have you echoed the mysql query to screen, you may find it useful to do this on every page then you can see if the LIMITs are going in correctly.
     
    redlorry919, Nov 21, 2007 IP
  10. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #10
    this is how it looks right now, everyting is now fixed except the problem when i changed pages it shows the same on the next page! :/

    <?
    include "includes/inc.php";
    
    require_once("header.php");
    
    $template->set_filenames(array(
    	'body' => 'tags.html')
    	);
    	
    ?>
    
    <?php
    
    $con = mysql_connect("localhost","asd","das) OR die('Could not connect: ' . mysql_error());
    mysql_select_db("asd", $con);
    
    //This checks to see if there is a page number. If not, it will set it to page 1
    if (!(isset($pagenum)))
    {
    $pagenum = 1;
    }
    
    //Edit $result to be your query 
    $result = mysql_query("
        SELECT *
        FROM `images` 
        WHERE `tags`
        LIKE '%" . mysql_real_escape_string($_GET['tag']) . "%'
        ORDER BY views
        $max
    ")
    OR die(mysql_error());
    $rows = mysql_num_rows($result); 
    
    //This is the number of results displayed per page
    $page_rows = 30; 
    
    //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;
    } 
    
    while($row = mysql_fetch_array($result))
    {
    	
    echo '<div style="float:left;width:25%"><a href="http://filefrog.net/show.php/' .$row['id'].'_' .$row['name'].'"><img src="http://www.filefrog.net/out.php/t' .$row['id'].'_' .$row['name'].'"></a></div>'; 
        echo "<br>\n";
     
    }	
    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
    ?>
    <div style="clear:both"></div>
    
    <?	
    // This shows the user what page they are on, and the total number of pages
        echo " --Page $pagenum of $last-- <p>";
       
        if ($pagenum == 1)
    {
    }
    else
    {
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&amp;tag={$_GET['tag']}'> <<-First</a> ";
    echo " ";
    $previous = $pagenum-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&amp;tag={$_GET['tag']}'> <-Previous</a> ";
    }
    
    //just a spacer
    echo " ---- ";
    
    if ($pagenum == $last)
    {
    }
    else {
    $next = $pagenum+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&amp;tag={$_GET['tag']}'>Next -></a> ";
    echo " ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&amp;tag={$_GET['tag']}'>Last ->></a> ";
    }
    ?>
    <?
    include "footer.php";
    ?>
    PHP:
     
    izlik, Nov 21, 2007 IP
  11. redlorry919

    redlorry919 Peon

    Messages:
    384
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You've got

    $result = mysql_query(" SELECT * FROM `images` WHERE `tags` LIKE '%" . mysql_real_escape_string($_GET['tag']) . "%' ORDER BY views $max")

    however where is $max being set before the qry?

    I'd look at that and also try echoing the qry out as mentioned before.
     
    redlorry919, Nov 21, 2007 IP
  12. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #12
    i made a new file just to test out echoing the query out and i think im getting on the way to it but atm it dont load the images, do you know why ? :(

    http://filefrog.net/tags3.php?=games

    
    <?
    include "includes/inc.php";
    
    require_once("header.php");
    
    $template->set_filenames(array(
    	'body' => 'tags.html')
    	);
    	
    ?>
    
    <?php
    
    $con = mysql_connect("localhost","asd","das") OR die('Could not connect: ' . mysql_error());
    mysql_select_db("asd", $con);
    
    //This checks to see if there is a page number. If not, it will set it to page 1
    if (!(isset($pagenum)))
    {
    $pagenum = 1;
    }
    
    //Edit $result to be your query 
    $result = mysql_query("
        SELECT *
        FROM `images` 
        WHERE `tags`
        LIKE '%" . mysql_real_escape_string($_GET['tag']) . "%'
        $max
    ")
    OR die(mysql_error());
    
    //This is where you display your query results
    while($info = mysql_fetch_array( $result ))
    {
    Print $info['Name'];
    echo "<br>";
    }
    echo "<p>";
    
    $rows = mysql_num_rows($result); 
    
    //This is the number of results displayed per page
    $page_rows = 30; 
    
    //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;
    } 
    
    while($row = mysql_fetch_array($result))
    {
    
    }	
    
    ?>
    <div style="clear:both"></div>
    
    <?	
    // This shows the user what page they are on, and the total number of pages
        echo " --Page $pagenum of $last-- <p>";
       
        if ($pagenum == 1)
    {
    }
    else
    {
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&amp;tag={$_GET['tag']}'> <<-First</a> ";
    echo " ";
    $previous = $pagenum-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&amp;tag={$_GET['tag']}'> <-Previous</a> ";
    }
    
    //just a spacer
    echo " ---- ";
    
    if ($pagenum == $last)
    {
    }
    else {
    $next = $pagenum+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&amp;tag={$_GET['tag']}'>Next -></a> ";
    echo " ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&amp;tag={$_GET['tag']}'>Last ->></a> ";
    }
    
    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
    ?>
    <?
    include "footer.php";
    ?>
    PHP:
     
    izlik, Nov 21, 2007 IP