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

Thread: How to use next() and prev() on an array

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default How to use next() and prev() on an array

    Does anyone know how to get next(), prev(), and current() to work properly? I have tried it on what seems like a simple application but no luck.

    I have a table of art titles with title_id as the index. When someone clicks on a thumbnail it takes them to a detail page for that painting using the title_id in the query string. On the detail page I have NEXT and PREVIOUS arrows so people can browse to new paintings without returning to the thumbnail page. I want the array to be defined as
    Code:
    SELECT * FROM title WHERE artist_id=1 ORDER BY title
    so that when they click NEXT it will take them to the next painting by that artist in alphabetical order by title. I haven't been able to get it to work so am currently using title_id + 1 and title_id -1, which is unsatisfactory. Seeing as the page is selected using the title_id, as in <a href="/php/art-detail.php?title_id=<?php echo $title_id +1;?>, how do I bring the array into it so next and prev work?

    I have considered separating the artists into separate tables but that seems like settling for failure. I'd rather do it properly. This can't be that hard. I must be making some error I just can't see. Please put me out of my misery. Thanks! e

  2. #2
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    Can you give us an sample of the code you're currently using? It's the easiest way to debug the code to find out what you need.

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    honestly, I have never used next(), prev(), or current() in actual (production) code.

    Since your links will only ever be for the next or previous page, the $id+1 or $id-1 is actually fairly efficient.

    If you need to keep your search result (for this, or other, reasons), you might consider using $_SESSION. You could also cache the results (save it in a txt file) so they would be readily available when anyone made the same query.

  4. #4
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    I tried various ways trying to get it to work. This is just the last one that didn't work in which I try using the image title in the query string...

    Code:
    $sql = "SELECT * FROM `title` WHERE `on` = 1 ORDER BY `filetag`";  
    $result2 = mysql_query($sql,$connection) or die("Couldn't execute $sql query. <br> mysql error: ".mysql_error()); 
    $art = mysql_fetch_assoc($result2);
    $current = current($art['filetag']);
    $next = next($art['filetag']);
    $prev = prev($art['filetag']);
    
    <a href="/php/art-detail3.php?title=<?php echo $prev;?>"><<</a> &nbsp; &nbsp; <?php echo $art['title'];?> &nbsp; &nbsp; <a href="/php/art-detail3.php?title=<?php echo $next;?>">>></a>

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by kuau View Post
    PHP Code:
    $current current($art['filetag']);
    $next next($art['filetag']);
    $prev prev($art['filetag']); 
    is $art['filetag'] an array?
    (I would imagine it is a string)

    Edit:

    Also, I'm not sure you have the right idea about these functions, anyway. They all deal with PHP's internal array pointer, which is a lot more similar to C's idea of traversal than what most PHP programmers do. observe:
    PHP Code:
    <?php
    $array 
    = array( 'one','two','three' );
    // since we just initialized the array, the pointer is at the beginning:
    print current$array );
    //  pointer doesn't move.  prints "one".
    print next$array );
    //  pointer moves forward.  prints "two".
    print prev$array );
    //  pointer moves back/    prints "one".

    print next$array[0] );
    //  this (similar to what you're doing) should actually throw an error:
    //    Warning: next() expects parameter 1 to be array ...
    //  and print nothing at all/
    Last edited by traq; 05-22-2012 at 01:22 AM.

  6. The Following User Says Thank You to traq For This Useful Post:

    kuau (05-25-2012)

  7. #6
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear traq: Using +1 and -1 doesn't really work very well because I have no control over what is being presented. It jumps from one artist to another and then to sketches and calendars and things I don't really want displayed, all out of order. I want to be able to specifically define the array and then advance or rewind the pointer. I used to be able to do this in older database software.

    I would have to reassign all the title_id's in sorted order with each artist in a separate table in order to accomplish what I want. I have considered this in exasperation, but I find it hard to believe that php cannot do this simple array advancing.

  8. #7
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    I'm not fully up on PHP syntax so please forgive any basic errors in this. This would be the best way I could see to achieve it, using next(), prev() and current().

    PHP Code:
    <?php
    mysql_connect
    ("localhost""mysql_user""mysql_password") or
        die(
    "Could not connect: " mysql_error());
    mysql_select_db("mydb");

    $sql "SELECT * FROM title WHERE artist_id=1 ORDER BY title";
    $res mysql_query($sql);

    function 
    mysql_fetch_all($res) {
       while(
    $row=mysql_fetch_array($resMYSQL_BOTH)) {
           
    $return[] = $row;
       }
       return 
    $return;
    }

    $art mysql_fetch_all($res);
    ?>
    PHP Code:
    <a href="/php/art-detail.php?title_id=<?php
    if(current($art) != reset($art)) {
      
    $prev prev($art);
    else
      
    $prev end($art);
    }
    echo 
    $prev['title'];
    ?>">Previous</a>

    <!-- code to display current($art) image here -->

    <a href="/php/art-detail.php?title_id=<?php
    if(current($art) != end($art)) {
      
    $nextnext($art);
    else
      
    $nextreset($art);
    }
    echo 
    $next['title'];
    ?>">Next</a>
    Last edited by ApacheTech; 05-22-2012 at 01:36 AM. Reason: Change of code design, edited the hyperlink PHP code.

  9. The Following User Says Thank You to ApacheTech For This Useful Post:

    kuau (05-25-2012)

  10. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    okay - so you're talking about doing this:
    Code:
    [result-set]
        [row-1]
            [title_id]  // you're here
            [whatever]
        [row-2]
            [title_id]  // you want to go here next
            [whatever]
    // correct?
    Edit:

    the first part of what Apache suggests is pretty much what I would suggest, if this assumption is correct.

    The exception is that you can't use reset() and end() in the second part, because -well- they reset and end the array pointer when used.

    Last edited by traq; 05-22-2012 at 01:41 AM.

  11. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Not necessarily. The title_id's are assigned as new paintings are entered into inventory. The ones I started with are in alphabetical order but new paintings just get the next available number, so the title_id's in order do not present the titles in order. But that is not even that important. If someone is looking at a particular artist's art, they don't want to suddenly see another artist's work mixed in. Why can't I define an array that does not include the whole table?

  12. #10
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    reset() rewinds array's internal pointer to the first element and returns the value of the first array element.

    end() advances array's internal pointer to the last element, and returns its value.
    From http://www.php.net/manual/en/function.end.php

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
  •