ParadoxKing Posted December 17, 2007 Share Posted December 17, 2007 Okay, I fixed up the database script mentioned in my earlier topic. The script I originally made was for a web comic complete with an archive. Now however, I'm trying to write a similar script for my friend, he refuses to use the database version. So I'm using directory functions and the like. Before this I had a made a more simplistic script: <?php $maximages = 5; //$maximages is set as an integer. if ($imagenum<=0||$imagenum>$maximages) {$imagenum=$maximages;} else {$imagenum=$imagenum;} echo sprintf("<img src=\"Comic%d.png\" name=\"comic\" /><br />",$imagenum); echo sprintf("<a href=\"index2.php?imagenum=%d\"><img border=\"0\" src=\"back.png\" alt=\"Previous Comic\" align=\"left\" /></a>",(($imagenum-1>0)?(--$imagenum)$imagenum))); echo sprintf("<a href=\"index2.php?imagenum=%d\"><img border=\"0\" src=\"next.png\" align=\"right\" alt=\"Next Comic\" /> </a>",(($imagenum+1<=$maximages)?(++$imagenum)$imagenum))); ?> The Comics were listed in the same directory as the index page and the $maximages var was defined as an integer. I soon grew very annoyed with this script because each time I uploaded a new comic I would also have to go into the code and change $maximages to whatever the new number of comics was. I started playing around with directories and failed miserably so I went out to the internet and got this (I editted the paths and changed the $count var to $maximages): <?php $dir = "comics/"; $maximages = 0; if(is_dir($dir)) { if($handle = opendir($dir)) { while(($file = readdir($handle)) !==false) { $maximages++; } closedir($handle); } } echo $maximages; ?> I moved the comics into their own subdirectory (comics) to simplify things. Then I echoed the $maximages var to see if the script worked. It didn't. The number of files it read totaled up to a big resounding 0. When I changed the value of $dir to ".", however, it returned the correct number of files. I rewrote the comic path several ways and still got nothing. Can anyone tell me why? Also, when I used $dir = ".", the returned number included either hidden files or the files in the comics subdirectory. Is there away around that? Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 17, 2007 Share Posted December 17, 2007 There is a function that might help you considerably: glob(). It will "glob" all the file matching your pattern from whatever directory you specify. $comics = glob("comics/*.jpg"); jpg being whatever the comics' file type is. http://www.php.net/manual/en/function.glob.php Quote Link to comment Share on other sites More sharing options...
ParadoxKing Posted December 17, 2007 Author Share Posted December 17, 2007 Maybe I didn't use it right (I replaced $comics with $dir and substituted the existing $dir string with it), but I still gives me a big 0. http://146.145.25.157/animeclub/nodbsample.php See. The only possible reasons I can think of for this is that the path is written incorrectly and it can't find the directory, or it can see the directory, but can't read it. Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 17, 2007 Share Posted December 17, 2007 I would assume that is because you are setting $maximages = 0 then echoing $maximages, which is 0. glob() returns an array, try just this code: $comics = glob("comics/*.jpg"); print_r($comics); That should show you a list of all the comics. You can iterate through the array to use the files. Quote Link to comment Share on other sites More sharing options...
ParadoxKing Posted December 17, 2007 Author Share Posted December 17, 2007 No, that wasn't the problem. I feel really stupid now... I uploaded the page but I didn't upload the folder holding the comics. It returned 7, which means its still reading hidden files. Would your script help weed those out? Mind you all I want is the number of comics set as the $maximages var. I don't want to actually print them. The stuff with echo was just me testing the script. Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 18, 2007 Share Posted December 18, 2007 I simplified the script just using different methods of doing what you are trying to do and it may work <?php $dir = "comics/"; if(is_dir($dir)) { $maximages = 0; $files = glob($dir . '*'); foreach($files as $file) { if(($file != '.') && ($file != '..')) { $maximages = $maximages+1; } } } echo $maximages; ?> Quote Link to comment Share on other sites More sharing options...
ParadoxKing Posted December 18, 2007 Author Share Posted December 18, 2007 Perfect! Thanks a lot, I knew there was a reason I like help forums. Quote Link to comment Share on other sites More sharing options...
ParadoxKing Posted December 18, 2007 Author Share Posted December 18, 2007 Or not. The echo part worked perfectly. its bugging out on actually displaying the comics. Can you put that other script back up please, I didn't try that part. I can scroll backwards but not forwards: http://146.145.25.157/animeclub/nodbsample.php Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 19, 2007 Share Posted December 19, 2007 Ok now try this one. I saw how you were getting the imagenum through get so i went ahead and made sure it was a number to guard against SQL injections, then i made the script as it will only show the next image if there is another image and will only show the back image of you are not at the first image. You can go in later and make a new image like your old one that show the user that there is no more in that direction by adding an else statement. The method you were using to determine the link for each image was over complicated so i made it easier. I also check to see if there are any comics, if there arent any you will get a message saying 'There are no comics'. <?php if(isset($_GET['imagenum'])) { if(is_numeric($_GET['imagenum'])) { $comics_num = $_GET['imagenum']; $comics_dir = "comics/"; $comics_max = '0'; if(is_dir($comics_dir)) { $comics_files = glob($comics_dir . '*'); foreach($comics_files as $comics_file) { if(($comics_file != '.') && ($comics_file != '..')) { $comics_max = $comics_max+1; } } } if($comics_max > 0) { if($comics_num >= $comics_max) { $comics_num = $comics_max; } if($comics_num > 0) { print '<a href="index2.php?imagenum=' . $comics_num-1 . '">'; print '<img border="0" alt="Previous Comic" title="Previous Comic" align="left" src="back.png" />' print '</a>'; } print '<img src="Comic' . $comics_num . '.png" name="comic" /><br />'; if($comics_num < $comics_max) { print '<a href="index2.php?imagenum=' . $comics_num+1 . '">'; print '<img border="0" alt="Next Comic" title="Next Comic" align="right" src="next.png" />'; print '</a>'; } } else { print 'There are no comics'; } } else { print 'Bad input'; } } ?> Quote Link to comment 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.