Jump to content

I'm pretty sure this code should work.


mockenoff

Recommended Posts

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.
[code]

//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>";
[/code]
Link to comment
Share on other sites

This looks a bit odd

[code]$spot = $spot++;[/code]

It should be one of these three:

[code]$spot++;
$spot = $spot + 1;
$spot += 1;[/code]

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.
Link to comment
Share on other sites

Thanks, dude, that got some output, except now it's an infite column of broken img src's. Just a page of [code]<td width="33%"><center><a href="./images/gallery/mobile/"><img src="./images/gallery/thumbnails/mobile/" border="0"></a><br></center></td>[/code] 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.
Link to comment
Share on other sites

You probably need to re-think the logic on:

[code]while ($spot < $row) {[/code]

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:

[code]while ($icounts < $col) {[/code]
Link to comment
Share on other sites

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 :)
Link to comment
Share on other sites

Yeah, here we go.
[code]
//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";[/code]
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.
Link to comment
Share on other sites

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:

[code]while ($icounts < $col) {[/code]

[code]$icount++;[/code]

Notice anything? :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.