mockenoff Posted September 15, 2006 Share Posted September 15, 2006 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 tableprint "<table width=\"575px\">\n";//start row count loopwhile ($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 tableecho "</table>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/ Share on other sites More sharing options...
Wintergreen Posted September 15, 2006 Share Posted September 15, 2006 Are you getting errors or is it just not displaying anything? Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/#findComment-92091 Share on other sites More sharing options...
mockenoff Posted September 15, 2006 Author Share Posted September 15, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/#findComment-92113 Share on other sites More sharing options...
btherl Posted September 15, 2006 Share Posted September 15, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/#findComment-92140 Share on other sites More sharing options...
mockenoff Posted September 15, 2006 Author Share Posted September 15, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/#findComment-92145 Share on other sites More sharing options...
pocobueno1388 Posted September 15, 2006 Share Posted September 15, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/#findComment-92149 Share on other sites More sharing options...
mockenoff Posted September 15, 2006 Author Share Posted September 15, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/#findComment-92159 Share on other sites More sharing options...
btherl Posted September 15, 2006 Share Posted September 15, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/#findComment-92160 Share on other sites More sharing options...
mockenoff Posted September 15, 2006 Author Share Posted September 15, 2006 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 tableecho "<table width=\"575px\">\n";//start row count loopwhile ($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 varecho "</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. Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/#findComment-92164 Share on other sites More sharing options...
btherl Posted September 15, 2006 Share Posted September 15, 2006 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? :) Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/#findComment-92176 Share on other sites More sharing options...
mockenoff Posted September 15, 2006 Author Share Posted September 15, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/20805-im-pretty-sure-this-code-should-work/#findComment-92546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.