SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Kiwi Fr00t jylyn's Avatar
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Function not returning expected value

    PHP Code:
    function checkPhotoExists($i) {
        global 
    $photo;
        if(@
    mysql_data_seek($photo$i)) {
            
    $columns mysql_fetch_array($photo);
            
    $coverPhoto $columns['FileName'];
            
    $displayPhoto "$root/images/gallery/$coverPhoto";
            
    $i++;
            if(!
    file_exists($displayPhoto)) checkPhotoExists($i);
            else return(
    $coverPhoto);
        } else {
            echo(
    "<script type='text/javascript'>alert('returning none');</script>");
            return(
    'none');
        }

                
    $coverPhoto checkPhotoExists(0); 
    Having trouble with this function and would be grateful for some help...

    $photo is the result of a mysql_query.
    It seems to be looping through the function all right, and I am getting the javascript alert, but it's not assigning 'none' as the value for $coverPhoto.

    I'm not too familiar with user-defined functions but I have tried doing this:
    PHP Code:
    function checkPhotoExists($i) {
        return(
    'none');

                
    $coverPhoto checkPhotoExists(0); 
    ...and it assigns the value, so I'm stumped as to what's happening!

  2. #2
    SitePoint Zealot _theworks's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im not entirely sure whats causing this, but heres a couple of tips,

    with control structures i'd advise to always use curly brases, having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.

    PHP Code:
    if(!file_exists($displayPhoto)) {
        
    checkPhotoExists($i);
    } else { 
        return(
    $coverPhoto);

    If the function is just supposed to detirmine wheather or not the image exists just return boolean instead of none or the filename..

  3. #3
    Kiwi Fr00t jylyn's Avatar
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, yeah I do need the value of $coverPhoto if is exists.

    Have discovered a few things that might shed some light:
    I forgot to make $root global, but I fixed that and it doesn't make any difference.
    If I take out the @ from @mysql_data_seek I get this error:

    Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 3 is invalid for MySQL result index 14 (or the query data is unbuffered)...

    But as far as I can understand that just means it reached the end of the result and didn't find an existing file?

  4. #4
    Kiwi Fr00t jylyn's Avatar
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Recursive problem

    Right, I've managed to scrape it down to the bare basics and still can't get it to work, so I think I must be doing something wrong in the recursive department?
    PHP Code:
    function myFunction($i) {
      if(
    $i 3) {
        
    $i++;
        
    myFunction($i);
      } else {
        return(
    'nothing at all');
      }
    }

    $functionValue myFunction(0);
    echo(
    "Function value is $functionValue"); 
    Hopefully now it's glaringly obvious to someone who knows more about this than I do!

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the mysql_fetch_*() functions advance the internal result pointer of the result set, so you dont need to be using mysql_data_seek(), unless you dont want to start recursing through the result set from the beggining of it. ill assume you dont want to start from the beggining, and thats why your using it.

    anyway, your problem was that you were just calling the function, but you were never using the return value of that function, making your recursion a bit pointless.
    PHP Code:
    function checkPhotoExists($i) {
        global 
    $photo$root;
        @
    mysql_data_seek($photo$i);
        
    $columns mysql_fetch_array($photo);
        if (!
    $columns) {
            return 
    'none';
        }
        
    $coverPhoto $columns['FileName'];
        
    $displayPhoto "$root/images/gallery/$coverPhoto";
        
    $i++;
        if (!
    file_exists($displayPhoto)) {
            
    // recurse and use the ret val
            
    return checkPhotoExists($i);
        } else {
            return (
    $coverPhoto);
        }
    }


    $var checkPhotoExists(0); 

    are you sure you really need to use a recursive function for this? this seems like such a simple task that can be done without the overhead of recursion. like
    PHP Code:
    $result false;
    while (
    $columns mysql_fetch_assoc($result)) {
       
    $coverPhoto $columns['FileName'];
       
    $displayPhoto "$root/images/gallery/$coverPhoto";
       if (
    file_exists($displayPhoto)) {
           
    $result $displayPhoto;
           break;
       }
    }

    if (
    $result) {
        echo 
    $result;
    } else {
        
    // not found


  6. #6
    Kiwi Fr00t jylyn's Avatar
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    anyway, your problem was that you were just calling the function, but you were never using the return value of that function, making your recursion a bit pointless.
    Thanks for that... I completely missed it because I kept thinking it was a problem with the mysql_data_seek. I guess I won't forget how to do recursive functions again for a while!
    Quote Originally Posted by clamcrusher
    are you sure you really need to use a recursive function for this? this seems like such a simple task that can be done without the overhead of recursion.
    Right again... it was one of those things that started out simple and got more and more complicated to the point where it ended up doing a completely different thing than originally intended. It's good to have someone else see it for what it really is!

    Thanks for all your help


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
  •