Scratching My Head Over This

I added some code to my photo gallery web page to provide for scrolling to the previous image or the next image in the category. I use an array of all the photo keys in the category to deal with the first and last image.

When I have some debugging print_r and echo statements added everything works as I expect. When I remove the debugging statements it does not work. (?!) The code seems to act like I have only have a single photograph in the gallery. But I don’t know what is going on when I don’t have the debugging lines of code, because … I don’t have the debugging lines of code. There are no errors being recorded in the log.

Here is the code I added:


$allphotosquery = "SELECT photo_id FROM gallery_photos WHERE photo_category=:cid ORDER by pair_order";

try {
    $stmt = $dbh->prepare($allphotosquery);
    $stmt->bindValue(':cid', $cid, PDO::PARAM_INT);
    $stmt->execute();
    while( $rows = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
       $photos[] = $rows['photo_id'];
     }
  } catch (PDOException $e) {
        echo 'Error getting photograph information: ' . $e->getMessage();
  }
/*
  print_r($photos);
*/
  $prev_str = "<a href=\\"viewgallery.php?cid=$cid&pid=%pid\\">PREV</a>";
  $next_str = "<a href=\\"viewgallery.php?cid=$cid&pid=%pid\\">NEXT</a>";

  $key = array_search( $pid, $photos );
/*
  echo "Key is: " . $key;
*/
  if ( $key === FALSE ) {
      echo "Key is false";
      $prev = "&nbsp;&nbsp;&nbsp;&nbsp;";
      $next = "&nbsp;&nbsp;&nbsp;&nbsp;";
  } else {
/*     echo "Key is: " . $key;  */
        if ( $key == 0 ) $prev = "&nbsp;&nbsp;&nbsp;&nbsp;";
        else $prev = str_replace( '%pid', $photos[$key - 1], $prev_str );
        if ( $photos[$key] == end( $photos ) ) $next = "&nbsp;&nbsp;&nbsp;&nbsp;";
        else $next = str_replace( '%pid', $photos[$key + 1], $next_str );
  }

  $gap = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  $contenttop .= "<br /><span class=\\"prevnext\\"><br />$prev$gap$next</span>";


Executed like this I get the HTML:


<span class="prevnext"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="viewgallery.php?cid=3&pid=4">NEXT</a></span>

But if I remove the comments around the print_r($photos) I get this for the print_r:


 Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 2
    [4] => 1
)

and this for the HTML:


<span class="prevnext"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="viewgallery.php?cid=3&pid=4">NEXT</a></span>

I also get the correct HTML when I remove the comments around either of the echo $key statements.

But I can’t use the print_r and echo on my public site.

I even tried touching the variables by added this code:


  $key = array_search( $pid, $photos );
  $keycopy = $key;                                     <<<<< these lines
  $photoscopy = $photos;                             <<<<< these lines

Any and suggestions appreciated.

Updated.

On closer look I see that I am getting the HTML for the href’s. It is just not visible on the web page. Even stranger.

Could you post a link so we can check it out?

I figured this out with fresh eyes this morning.

It was my CSS.

The href links were under my photograph. The print_r and echos moved things around.

I changed the positioning and all is well.

Thanks