headmine Posted April 24, 2009 Share Posted April 24, 2009 I am trying to build my own gallery that reads the files from a folder. What I Need it to do is Choose only jpgs from the folder. Display 4 jpgs in a row the insert a line break to display the next for images Here is the code I have so far and it only displays one image over and over again and will not stop looping. <?php $count = 0; $counter = 0; $dir = @ opendir("images"); while (($file = readdir($dir)) !== false) { if(strpos($file, ".jpg")) { $name[${$counter}] = $file; $counter++; } } while($count <= $counter) { echo "<a href='img.php?file=". $name[${$count}]" /><img src='images/" . $name[${$count}] . "' width='100' style='border:1px solid #e96302;' /></a>"; $count++; if($count == 4) { echo '<br />'; $count = 1; } } closedir($dir); ?> im pretty sure the problem lies within the array. $name[${$counter}] = $file; Or has something to do with the $counter variable inside the name array. Please help! Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/ Share on other sites More sharing options...
Maq Posted April 24, 2009 Share Posted April 24, 2009 It's because you're always setting $count back to 1, so if $counter is Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-817977 Share on other sites More sharing options...
headmine Posted April 24, 2009 Author Share Posted April 24, 2009 Ok I included that in some changes I made to the script and that fixed the looping problem all images are displaying but now I am getting an error. Here is the code <?php $count = 0; $counter = 0; $break = 0; $dir = @ opendir("images"); while (($file = readdir($dir)) !== false) { if(stristr($file, ".jpg")) { $name[${"counter"}] = $file; $counter++; } } while($count <= $counter) { echo "<a href=\"img.php?file=$name[$count]\" /><img src=\"images/$name[$count]\" width=\"100\" style=\"border:1px solid #e96302;\" /></a>"; $count++; $break++; if($break == 4) { echo '<br />'; $break = 0; } } closedir($dir); ?> here is the error PHP Notice: Undefined offset: 15 in PATH\TO\FILE\view.php on line 26 PHP Notice: Undefined offset: 15 in PATH\TO\FILE\view.php on line 26 it prints out twice. Also I noticed that at the end something is coming up blank here is what the script is printing out <a href="img.php?file=Picture 108.jpg" /><img src="images/Picture 108.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 119.jpg" /><img src="images/Picture 119.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 120.jpg" /><img src="images/Picture 120.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 136.jpg" /><img src="images/Picture 136.jpg" width="100" style="border:1px solid #e96302;" /></a><br /><a href="img.php?file=Picture 138.jpg" /><img src="images/Picture 138.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 139.jpg" /><img src="images/Picture 139.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 140.jpg" /><img src="images/Picture 140.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 142.jpg" /><img src="images/Picture 142.jpg" width="100" style="border:1px solid #e96302;" /></a><br /><a href="img.php?file=Picture 143.jpg" /><img src="images/Picture 143.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 144.jpg" /><img src="images/Picture 144.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 145.jpg" /><img src="images/Picture 145.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 146.jpg" /><img src="images/Picture 146.jpg" width="100" style="border:1px solid #e96302;" /></a><br /><a href="img.php?file=Picture 147.jpg" /><img src="images/Picture 147.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 148.jpg" /><img src="images/Picture 148.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 149.jpg" /><img src="images/Picture 149.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=" /><img src="images/" width="100" style="border:1px solid #e96302;" /></a><br /> Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818475 Share on other sites More sharing options...
Maq Posted April 24, 2009 Share Posted April 24, 2009 Try changing this line: $name[$counter] = $file; And take away the '@' symbol, that's meant to suppress errors and right now we want to see them. Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818481 Share on other sites More sharing options...
headmine Posted April 24, 2009 Author Share Posted April 24, 2009 Thanks for the quick reply but that didn't do anything. It is still printing out the same error. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818483 Share on other sites More sharing options...
laffin Posted April 24, 2009 Share Posted April 24, 2009 Why ya using ${$counter} if counter = 0 than it becomes $0 so fix that why did u escape the first set of quotes, but not the rest in the display loop? <?php $count = 0; $counter = 0; $break = 0; $dir = @ opendir("images"); while (($file = readdir($dir)) !== false) { if(stristr($file, ".jpg")) { $name[$counter] = $file; $counter++; } } while($count <= $counter) { echo "<a href=\"img.php?file={$name[$count]}\" ><img src=\"images\/P\" width=\"100\" style=\"border:1px solid #e96302;\" alt=\"\"></a>"; $count++; $break++; if($break == 4) { echo '<br>'; $break = 0; } } closedir($dir); ?> This code can be optimized a lot. Other from that didnt see anything else Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818506 Share on other sites More sharing options...
.josh Posted April 24, 2009 Share Posted April 24, 2009 <?php $files = glob('images/*.jpg'); foreach ($files as $file) { echo ($count % 4 == 0)? "<br/>" : ""; $count++; echo "<a href=\"img.php?file=$file\" /><img src=\"images/$file\" width=\"100\" style=\"border:1px solid #e96302;\" /></a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818512 Share on other sites More sharing options...
headmine Posted April 24, 2009 Author Share Posted April 24, 2009 I found the problem Then problem was with this while($count <= $counter) { I switched it to while($count < $counter) { All errors are gone now. =) Thanks everyone for the help. Here is the final code for anyone reading along. <?php $count = 0; $counter = 0; $break = 0; $dir = opendir("images"); while (($file = readdir($dir)) !== false) { if(stristr($file, ".jpg")) { $name[$counter] = $file; $counter++; } } while($count < $counter) { echo "<a href=\"img.php?file=$name[$count]\" /><img src=\"images/$name[$count]\" width=\"100\" style=\"border:1px solid #e96302;\" /></a>"; $count++; $break++; if($break == 4) { echo '<br />'; $break = 0; } } closedir($dir); ?> Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818514 Share on other sites More sharing options...
Maq Posted April 24, 2009 Share Posted April 24, 2009 I don't know why switching that condition would resolve those errors.... So everything works properly now? Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818523 Share on other sites More sharing options...
headmine Posted April 24, 2009 Author Share Posted April 24, 2009 Wow! Thanks crayon violet that didn't work at first but I made a few adjustments and it works perfect! Here is what I did $files = glob('images/*.jpg'); $count = 0; foreach ($files as $file) { echo ($count % 4 == 0)? "<br/>" : ""; $count++; echo "<a href=\"img.php?file=$file\" /><img src=\"$file\" width=\"100\" style=\"border:1px solid #e96302;\" /></a>"; } Basically just made $count = 0; and changed the path to the file because the way you had it printed out this way "/image/images/image.jpg" THANKS ALOT! Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818529 Share on other sites More sharing options...
headmine Posted April 24, 2009 Author Share Posted April 24, 2009 I don't know why switching that condition would resolve those errors.... So everything works properly now? Yes working great now! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818531 Share on other sites More sharing options...
laffin Posted April 24, 2009 Share Posted April 24, 2009 because the base starts at zero not 1 so if yer count is 25 yer elements are 0 to 24 Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818532 Share on other sites More sharing options...
.josh Posted April 24, 2009 Share Posted April 24, 2009 ah yeah, the undefined var notice. It's not strictly necessary to pre-assign a var like that with php. It just "let's you know" that that's what you're doing. I always forget about that because I have it set to not report notices. and yeah, sorry, forgot about removing the image/ in the echo since it would already be there from the glob. and on that note, you're probably going to want to change the href to use basename($file) instead of just file. Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818547 Share on other sites More sharing options...
headmine Posted April 24, 2009 Author Share Posted April 24, 2009 Here's a quick question.. Where can I read about how to condense my scripts? I got this entire thing working. Basically Page 1 displays all the images in the folder <?php $files = glob('images/*.jpg'); $count = 0; $counter = 0; foreach ($files as $file) { echo ($count % 4 == 0)? "<br/>" : ""; echo "<a href=\"img.php?file=$file&count=$counter\" /><img src=\"$file\" width=\"100\" style=\"border:1px solid #e96302;\" /></a>"; $count++; $counter++; } ?> Once you click on the image it will take you to a page to view the images. <div align="center"> <table width="425" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <?php if(empty($_GET['file'])) { header('Location:view.php'); } else { $ifile = $_GET['file']; $files = glob('images/*.jpg'); $count = $_GET['count']; $next = $count + 1; $prev = $count - 1; $taly = 0; foreach ($files as $file) { $taly++; } ?> <?php if($_GET['count'] == 0) { } else {?> <td width="22"><a href="<?php echo("img.php?file=$files[$prev]&count=$prev"); ?>"><img src='../../../img/prev.png' width='22' height='24' border="0" /></a></td> <?php }?> <td width="381"><div align="center"><a href="view.php"><img src='<?php echo $ifile; ?>' border="0" /></a></div></td> <td width="22"><?php if($_GET['count'] != $taly -1) { ?> <a href="<?php echo("img.php?file=$files[$next]&count=$next"); ?>"><img src='../../../img/next.png' width='22' height='24' border="0" /></a> <?php } } ?></td> </tr> </table> </div> Thanks to Crayon Violet the first code is nice and tight. The second code is sloppy i think. So are there any good articles on how to make scripts smaller? Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818568 Share on other sites More sharing options...
.josh Posted April 24, 2009 Share Posted April 24, 2009 There are no good articles on that. That's the art of coding. It comes with experience. The more you code, the more you look at it and say hey, there's no reason these two things can't be combined, or maybe you come across in the manual some function or something that happens to already do what those 5 lines of code you wrote out the hard way does. It's pattern recognition. There's nothing to memorize about it. It's a skill that comes with experience. Quote Link to comment https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818577 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.