SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is the value returned with no results

    Ok. This is a bit of a tricky one to explain. But here goes.

    I've created a simple album image viewer http://www.acumendesign.co.uk/rp_cor...test_album.php

    Each click of the arrow performs a php request( via ajax ) to retreive the new image.

    What I want to do is if the user clicking the right arrow clicks it again after the last image of the album it just loops to the first image. Or if clicking the left arrow it will loop round to select the last image of the album and flow backwards.

    I tried to use the code in the php below. I basically selects the next row with $image(which is a number) in the table and returns the result $row['filename'];

    If i have 5 images in my album but then the request that comes through is 'select the 6th image' which doesn't exist, what is the result that is returned and how can I translate that into my php query so that I can tell it to perform a new query?

    PHP Code:
    if(isset($_GET['getimage'])){

            
    $image $_GET['getimage'];

            include 
    "modules/db.php";

            
    $result mysql_query("SELECT * FROM images LIMIT $image, 1");
            
    }

    if(
    $result == " "){

        
    $newresult mysql_query("SELECT * FROM images LIMIT 0, 1") or die ("Query Error: " mysql_error () );

        while(
    $newrow mysql_fetch_array($newresult)){

        echo 
    $newrow['filename'];

         }



    while(
    $row mysql_fetch_array($result)){ 
        echo 
    $row['filename'];

    My Javascript is ok

    Thanks

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    $result is a resource -- a pointer to where other functions can access some data in memory. It's a resource whether there was a row or not. You can use mysql_num_rows($result) to see how many rows are in that resource. If there are zero, then issue your next query.

    Also, the only way this will work the way you want is if you specify a sort order for your results. You don't know what the "first" and "last" images are unless you do. Add an ORDER BY clause to your queries.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I could kick myself!!

    I completely forgot about the mysql_num_rows($result); so obvious now.lol

    Thanks for the memory jogger


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
  •