www.webdeveloper.com
Results 1 to 4 of 4

Thread: Page Bar

  1. #1
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233

    Page Bar

    Ok so I'm trying to display images stored in a database (along with other info which ill add and format when it works) after creating a page bar (IE if there's 25 pics and i want 8 per page then there will be 4 pages) however I cannot figure out how to start the specified offset of my query result. IE when I press page 2, its still starting with image 1.

    PHP Code:
    $imgqry "SELECT * FROM items ORDER BY iditems ASC";
    $images mysql_query($imgqry);

    /***********
    * Page Bar *
    ***********/
    $pagesize 12;
    $rowsize 4;
    $return 1;
    $numimages mysql_num_rows($images);
    $numpages ceil $numimages $pagesize );
    if ( isset ( 
    $_GET['page'] ) ) {
        
    $page $_GET['page'];
    } else {
        
    $page 1;
    }
    if ( 
    $page ) {
        
    $prev $page-1;
        echo 
    "<a href='index.php?page=".$prev."'>Previoius Page</a> ";
    }
    for ( 
    $o 1$o <= $numpages$o++ ) {
        echo 
    "<a href='index.php?page=".$o."'>".$o."</a> ";
    }
    if ( 
    $page $numpages ) {
        
    $next $page+1;
        echo 
    "<a href='index.php?page=".$next."'>Next Page</a>";
    }
    /**  END PAGE BAR **/

    echo "<br /><br />";
    while ( 
    $row mysql_fetch_array$images ) ) {
        
    $return++;
        echo 
    "<img src='images/".$row['filename']."' style='float:top'>".$row['itemdesc'];
        if ( 
    $return == $rowsize ) {
            echo 
    "<br />";
            
    $return 1;
        }


  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    In MySQL use limit on the end of your query to set the offset

    How Limit works:
    limit 0,8 = first 8 results (first record is always zero)
    limit 5,5 = records 6,7,8,9,10

    "limit starting,duration"

    For example, page 1 is items 0 through 7, page 2 is items 8 through 15 and so on, you'd maintain the offset in a variable and pass it to the query, i.e.

    PHP Code:
    mysql_query("SELECT * FROM items ORDER BY iditems ASC limit 0,$offset"); // Offset could be 0, then later 8, and so on... 
    Up to you in your code to track the offset, make sure the query does not return records out of bounds, etc. This whole process is called PAGINATION and here is a great web page on using PHP/MySQL to paginate almost anything.

    -jim
    Jim, Sr. Web Developer
    You know who you real friends are when you ask them to move your furniture or paint.

  3. #3
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    Thank you very much for that link, that is exactly what I have been trying to find!

    However, I do have a question about the use of ...href='{$_SERVER['PHP_SELF']}?pa... can I use the self variable in the link, when my script creating the output (in this case pictures.php) is being shown thru an include via index.php (aka using PHP_SELF will it link to .com/pictures.php or .com/index.php) ?

  4. #4
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    For most servers, either works fine. But here is your technical response:

    The differences are defined here but in POST situations a safer bet is to use $_SERVER['SCRIPT_NAME'] since $_SERVER['PHP_SELF'] is not necessarily reported the same way by all web servers and should be treated as user input (which means it's prone to cross site scripting). The full listing of $_SERVER array members is here if you wish to learn more about this, and why I said what I said. For CGI based PHP servers and CLI mode, things change, so read that page carefully (FYI).

    -jim
    Jim, Sr. Web Developer
    You know who you real friends are when you ask them to move your furniture or paint.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles