Jump to content


Photo

I'm pretty sure this code should work.


  • Please log in to reply
10 replies to this topic

#1 mockenoff

mockenoff
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 15 September 2006 - 12:07 AM

I'm trying to write a pretty quick and (inherently) dirty image gallery script for my site. I've lost a lot of the PHP zing that I probably never had, but I'm pretty sure this script should work, but it doesn't. Does someone mind telling me why?
Also, the $dir directory is full of images, $th with thumbnails of them, and info.txt has descriptions of each picture per line of the text file and I'm not really interested in optimization right now, just getting it to work. Thanks guys.

//set directories
$dir = "./images/gallery/$set";
$th = "./images/gallery/thumbnails/$set";

//description call
$info = @fopen("./images/gallery/$set/info.txt", "r");
if ($info) {
   while (!feof($info)) {
       $descs = fgets($info);
       $desc[] = $descs;
   }
   fclose($info);
}

//reads files
$fd = @opendir($dir);  
    while (($part = @readdir($fd)) == true) {  
    clearstatcache();  
        if ($part != "." && $part != "..") {  
            $gal[] = $part;  
        } 
    }

//set while variables
$icount = "0";
$counts = "0";
$spot = "0";
$result = count($gal);
if ($result > 3) {
    $row = ($result / 3);
    $sec = ($result % $row);
    $thi = ($result / 3) - ($result % 3);
    if ($sec != "0") {
        $row = $thi++;
    }
    $col = ($result / $thi);
}
else {
    $row = "1";
    $col = $result;
}

//start table
print "<table width=\"575px\">\n";

//start row count loop
while ($spot < $row) {
    
    echo "<tr width=\"575px\">\n";
    
    //start column count loop
    while ($icounts < $col) {
        
        echo "<td width=\"33%\"><center><a href=\"$dir/{$gal[$icount]}\"><img src=\"$th/{$gal[$icount]}\" border=\"0\"></a><br>{$desc[$icount]}</center></td>\n";
        $counts = $counts++;
        $icount = $icount++;
        
    //end column count loop
    }
    
    echo "</tr>\n";
    $counts = "0";
    $spot = $spot++;
    
//end row count loop
}

//end table
echo "</table>";


#2 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 15 September 2006 - 12:11 AM

Are you getting errors or is it just not displaying anything? 

#3 mockenoff

mockenoff
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 15 September 2006 - 12:54 AM

I get a blank page. Essentially, anything before the while loops like the html header and body tags will show up, but anything in the loops won't.

#4 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 15 September 2006 - 03:54 AM

This looks a bit odd

$spot = $spot++;

It should be one of these three:

$spot++;
$spot = $spot + 1;
$spot += 1;

You might be getting infinite loops from that.  The semantics of $spot = $spot++ is "Increment $spot, then set $spot to be equal to the value before the increment."  Which boils down to "$spot = $spot".

You might want to check the return value of opendir() as well.. but I doubt that's the problem, since the readdir() ought to fail if $fp isn't valid.

#5 mockenoff

mockenoff
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 15 September 2006 - 04:07 AM

Thanks, dude, that got some output, except now it's an infite column of broken img src's. Just a page of
<td width="33%"><center><a href="./images/gallery/mobile/"><img src="./images/gallery/thumbnails/mobile/" border="0"></a><br></center></td>
over and over again and nothing else.
And I'm pretty sure the opendir works, too, because I made another script that just puts out a page of link pics without comments and not in a table using that.

#6 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 15 September 2006 - 04:21 AM

You probably need to re-think the logic on:

while ($spot < $row) {

Obviously $spot will always be less than $row...resulting in an infinite loop. I didn't take the time to see what $spot and $row were made up of, but at a glance that seems like the problem. I may be totally off on this, I'm not sure. That was just a shot in the dark.

EDIT: I just saw this one too...the same situation could be going on here:

while ($icounts < $col) {


#7 mockenoff

mockenoff
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 15 September 2006 - 04:46 AM

Thanks. Yeah, I just sort of noticed that. Pardon my ignorance, anyways, since I'm probably missing the very obvious answer to what I'm about to ask, but what should I do about it?

#8 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 15 September 2006 - 04:48 AM

mockenoff, can you post your current code?  Did you change the other places with incorrect incrementing, like '$counts = $counts++' and '$icount = $icount++' ?  They need to be changed too.  The simplest way to do it is just '$counts++' and '$icount++'

To make debugging easier, try printing out the values of $icount and $col inside the while loop.  Do the same with the outer while loop.  THen maybe you can stare at it and it'll suddenly all makes sense :)

#9 mockenoff

mockenoff
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 15 September 2006 - 05:06 AM

Yeah, here we go.
//set directories
$dir = "./images/gallery/$set";
$th = "./images/gallery/thumbnails/$set";

//description call
$info = @fopen("./images/gallery/$set/info.txt", "r");
if ($info) {
   while (!feof($info)) {
       $descs = fgets($info);
       $desc[] = $descs;
   }
   fclose($info);
}

print_r ($desc);

$test = 0;
print "<br>{$desc[$test]}";

//reads files
$fd = @opendir($dir);  
    while (($part = @readdir($fd)) == true) {  
    clearstatcache();  
        if ($part != "." && $part != "..") {  
            $gal[] = $part;  
        } 
    }

//set loop variables
$icount = 0;
$counts = 0;
$spot = 0;
$result = count($gal);
if ($result > 3) {
    $row = ($result / 3);
    $sec = ($result % $row);
    $thi = ($result / 3) - ($result % 3);
    if ($sec != 0) {
        $row = $thi + 1;
    }
    $col = ($result / $thi);
}
else {
    $row = 1;
    $col = $result;
}

//start table
echo "<table width=\"575px\">\n";

//start row count loop
while ($spot < $row) {
    
    echo "<tr width=\"575px\">\n";
    
    //start column count loop
    while ($icounts < $col) {
        
        echo "<td width=\"33%\"><center><a href=\"$dir/{$gal[$icount]}\"><img src=\"$th/{$gal[$icount]}\" border=\"0\"></a><br>{$desc[$icount]}</center></td>\n";
        echo "$counts and $icount and $result and $row and $col and $sec and $thi and ";
        $counts++;
        $icount++;
        
    //end column count loop
    }
    
    echo "</tr>\n";
    echo $spot;
    $counts = 0;
    $spot++;
    
//end row count loop
}

//end img var
echo "</table>\n";
And that debugging advice? Holy crap, good thinking. Some how $icount and $count come out as 5 digits numbers, but the other variables come out as I want them. I don't know how, though, since I only set them as 0 mere lines above.

#10 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 15 September 2006 - 06:15 AM

5 digits?  That's a lot of digits :)  Try printing out those values further up as well.  Print them right after you set them, and after any statement that might possibly alter them.  If that doesn't work, then print the values out after statements which CAN'T possibly alter them :)  Sooner or later you'll find where it's going wrong.

Oh.. I just noticed:

while ($icounts < $col) {

$icount++;

Notice anything? :)

#11 mockenoff

mockenoff
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 15 September 2006 - 05:33 PM

Ha, wow, I feel like a fool. That little mix of $icount and $counts was the problem. Well, I mean the only problem after the other ones were worked out. Thanks a lot, guys, I really appreciate it. I think that's all of it, but more problems will likely arise.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users