SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    paginate an array

    I have a bunch of paintings in a collection that I want to paginate through. I have the first page set up so that it displays all of the paintings, in groups of 10, with back and next links. Works perfectly.

    The problem is, when somebody clicks a link and views a painting I want to have back and next links on that page too, so that the user can go to the next painting in the series, etc.

    My array contains all of the ID numbers or each painting in the series.

    PHP Code:
    array(3) {
      [
    0]=>
      
    string(5"87763"
      
    [1]=>
      
    string(5"87764"
      
    [2]=>
      
    string(5"87765"


    I'm not sure how to do this.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's tons of tutorials out there on how to do pagination. Most will probably use a database, so instead of using LIMIT in the sql to get the desired range of items, use something like array_slice()



    edit- sorry i read your question wrong.
    PHP Code:
    $pos abs((int) $_GET['pos']);
    if (
    $pos count($array) - 1) {
        
    // show next link
    }
    if (
    $pos 0) {
        
    // show prev link


  3. #3
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Here's what I have so far.

    PHP Code:
    $paintings_count = array();
    $query_paintings_count "SELECT id FROM paintings WHERE painting_year=2009 ORDER BY id ASC";
    $sql_paintings_count mysql_query($query_paintings_count);

    if(
    mysql_num_rows($sql_paintings_count) > 1) {
        while(
    $row_paintings_count mysql_fetch_assoc($sql_paintings_count)) {
            
    array_push($paintings_count,$row_paintings_count['id']);
        }

        
    $pos abs((int) $_GET['id']);
        
        if(
    $pos count($paintings_count) - 1) {
            
    // show next link
            
    echo '<a href="index.php?id='; echo $pos+1; echo'">next</a>';
        }
        if(
    $pos 0) {
            
    // show prev link
            
    echo '<a href="index.php?id='; echo $pos-1; echo'">previous</a>';
        }

    I'm running into some problems. Firstly, if I start on the first record of the gallery, I'm still seeing the previous link, which links me to the next previous record in the db - which has nothing to do with the current gallery.

    Second, since I'm starting on the first record, I should see the next link, not the previous link.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works fine.
    PHP Code:
        $array = array('foo''bar''baz');

        
    $pos abs((int) $_GET['pos']);
        
        echo 
    "showing the thing thats in array[$pos]";
         
        if(
    $pos count($array) - 1) { 
            
    // show next link 
            
    echo '<a href="?pos='; echo $pos+1; echo'">next</a>'
        } 
        if(
    $pos 0) { 
            
    // show prev link 
            
    echo '<a href="?pos='; echo $pos-1; echo'">previous</a>'
        } 
    Maybe you have confused pos with some id in your database. pos was meant to be the array index, not anything to do with your database.

    Since you now have made it clear that you actually are using a database, I recommend you look at some of the many existing tutorials.

  5. #5
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I spent well over an hour searching for this. The only thing I could find was how to do it the way I already have it working; by displaying large result lists.

    I only found one thing that resembled what you showed me.

    What I'm trying to do stems from the large results list: show a gallery full of thumbnails. Click on a thumb and see the larger picture. Now click next or previous to move through the larger pictures.

    EDIT: Yes, I was confusing $pos with an ID.
    I have $pos working now. How do I pass the ID number that's in the array?

  6. #6
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anybody?


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
  •