lastlifelost Posted September 30, 2009 Share Posted September 30, 2009 I'm curious if there's a way to write a loop to take the following code and automatically insert different .jpgs in. I'd like to replace jpg1 with the a filename taken from a folder on my server. If that's not possible, I could always make a list of the filenames and have those inserted into the php loop, if this can be done at all. This would still be easier than cop/pasting the whole thing together. <div class="pagecontent gallery"> <div class="imgcontainer"> <a href="java script:void(0);" onclick="showWindow('jpg1')"> <img alt="mrbungle" src="images/products/thumbs/jpg1.jpg" height="120" width="160" /> </a> </div> <!-- END .imgcontainer --> <!-- BEGIN #jpg1 --> <div class="popupdiv" id="jpg1"> <div id="subdiv"> <div class="close"> <a href="java script:void(0);" onclick="CloseWindow('jpg1')">Close Gallery</a> </div> <a href="http://woodturningvt.com/images/products/jpg1.jpg" target="blank"> <!-- full size view --> <img src="http://woodturningvt.com/images/products/jpg1.jpg" alt="mrbungle" title="click for full size view"/> </a> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam porttitor rutrum massa ac fringilla. Proin fringilla tincidunt dui, et condimentum nulla viverra vitae. Proin ipsum mauris, suscipit ac adipiscing ut, porttitor eu velit. Vivamus blandit orci vel justo sagittis viverra. In hac habitasse platea dictumst. Ut id eros sodales arcu facilisis tristique a cursus metus. Praesent iaculis egestas turpis sed hendrerit. Etiam eros eros, iaculis a ultrices in, sagittis in erat. Curabitur tristique purus eu nisi euismod ut rutrum mauris tincidunt. Quisque sit amet sem nunc. Sed sed eleifend lacus. </p> </div> <!-- END .subdiv --> </div> <!-- END .popupdiv --> <!-- END #jpg1 --> </div> Link to comment Share on other sites More sharing options...
jlhaslip Posted September 30, 2009 Share Posted September 30, 2009 read the list of jpg files into an array and loop through the array Link to comment Share on other sites More sharing options...
lastlifelost Posted September 30, 2009 Author Share Posted September 30, 2009 That's exactly what I'm trying to do, but I'm very new to this. I'm working my way through the w3schools php tutorial, but I haven't figured out how to insert the code right yet. This is where I'm at now: <?php $x=array("jpg1","jpg2","jpg3");foreach ($x as $value){write "<div class='imgcontainer'>"write " <a href='java script:void(0);' onclick='showWindow('" . $value . "')'>"write " <img alt='" . $value . "' src='images/products/thumbs/" . $value . ".jpg' height='120' width='160' />write " </a>"write " </div> <!-- END .imgcontainer -->"write " <!-- BEGIN #" . $value . "-->"write " <div class='popupdiv' id='" . $value . ">"write " <div id="subdiv">write " <div class="close">write " <a href='java script:void(0);' onclick='CloseWindow('" . $value . "')'>Close Gallery</a>"write " </div>"write " <a href='http://woodturningvt.com/images/products/" . $value . ".JPG' target='blank'> <!-- full size view -->"write " <img src='http://woodturningvt.com/images/products/" . $value . ".JPG' alt='$value' title='click for full size view'/>"write " </a>"write " <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam porttitor rutrum massa ac fringilla. Proin fringilla tincidunt dui, et condimentum nulla viverra vitae. Proin ipsum mauris, suscipit ac adipiscing ut, porttitor eu velit. Vivamus blandit orci vel justo sagittis viverra. In hac habitasse platea dictumst. Ut id eros sodales arcu facilisis tristique a cursus metus. Praesent iaculis egestas turpis sed hendrerit. Etiam eros eros, iaculis a ultrices in, sagittis in erat. Curabitur tristique purus eu nisi euismod ut rutrum mauris tincidunt. Quisque sit amet sem nunc. Sed sed eleifend lacus. </p>"write " </div> <!-- END .subdiv -->"write " </div> <!-- END .popupdiv -->"write " <!-- END #" . $value . " -->"}?> but I keep getting an error:Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/content/l/a/s/lastlifelost/html/jpgphp2.php on line 13I feel like I'm getting closer, but not quite there yet. Also, how do I read a list into an array? is it just: $cars=array("Saab","Volvo","BMW","Toyota"); or is there a way to get it to read the files in a folder without inputting all of the strings? Link to comment Share on other sites More sharing options...
lastlifelost Posted September 30, 2009 Author Share Posted September 30, 2009 OK - I got it. Here's my solution, feel free to look it over. I'm still looking for a way to automatically pull the file names from a folder rather than manually entering them into a list, though. <?php $x=array("jpg1","jpg2","jpg3");foreach ($x as $value){echo "<div class='imgcontainer'>";echo " <a href='java script:void(0);' onclick='showWindow('" . $value . "')'>";echo " <img alt='" . $value . "' src='images/products/thumbs/" . $value . ".jpg' height='120' width='160' />";echo " </a>";echo " </div> <!-- END .imgcontainer -->";echo " <!-- BEGIN #" . $value . "-->";echo " <div class='popupdiv' id='" . $value . ">";echo " <div id='subdiv'>";echo " <div class='close'>";echo " <a href='java script:void(0);' onclick='CloseWindow('" . $value . "')'>Close Gallery</a>";echo " </div>";echo " <a href='http://woodturningvt.com/images/products/" . $value . ".JPG' target='blank'> <!-- full size view -->";echo " <img src='http://woodturningvt.com/images/products/" . $value . ".JPG' alt='$value' title='click for full size view'/>";echo " </a>";echo " <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam porttitor rutrum massa ac fringilla. Proin fringilla tincidunt dui, et condimentum nulla viverra vitae. Proin ipsum mauris, suscipit ac adipiscing ut, porttitor eu velit. Vivamus blandit orci vel justo sagittis viverra. In hac habitasse platea dictumst. Ut id eros sodales arcu facilisis tristique a cursus metus. Praesent iaculis egestas turpis sed hendrerit. Etiam eros eros, iaculis a ultrices in, sagittis in erat. Curabitur tristique purus eu nisi euismod ut rutrum mauris tincidunt. Quisque sit amet sem nunc. Sed sed eleifend lacus. </p>";echo " </div> <!-- END .subdiv -->";echo " </div> <!-- END .popupdiv -->";echo " <!-- END #" . $value . " -->";}?> Link to comment Share on other sites More sharing options...
ls206 Posted September 30, 2009 Share Posted September 30, 2009 Hi,I wrote a little image gallery that simply gets the pictures from an image directory.Here's some of the code that might be what you're after.The php reads the folder contents into an array which can then be used to display a slideshow etc... <script type="javascript">var arr = new Array();var ctr = 0;function store(i) { arr[ctr] = i; ctr+=1;}function outputArr() {for (var x in arr) { document.write("" + arr[x] + "<br />"); }}function sortArr() { arr = arr.sort();}</script></head><body><?php $open = opendir("........IMAGE_FOLDER_DIRECTORY.........."); while ($read = readdir($open)) { if ($read!= "." && $read!= "..") { //echo $read . '<br>'; example of what the code does in php echo "<script language='javascript'>store('$read');</script>"; //stores all the file names into the array } } closedir($open); echo "<script language='javascript'>sortArr();</script>"; //Sorts the array incase there is a specific order e.g. "1.jpg", "2.jpg"... echo "<script language='javascript'>outputArr();</script>"; //displays all the files in the array, for testing purposes?></body></html> edited - minor errors Link to comment Share on other sites More sharing options...
justsomeguy Posted September 30, 2009 Share Posted September 30, 2009 That's using PHP to store everything in a Javascript array. The scandir function will return an array of files and directories:http://www.php.net/manual/en/function.scandir.phpWhen you loop through that array you can use the is_dir function to make sure you only work with files and not directories: <?php$files = scandir('c:\path\to\files');foreach ($files as $file){ if (is_dir($file)) { continue; } echo 'file: ' . $file . '<br>';}?> Link to comment Share on other sites More sharing options...
lastlifelost Posted September 30, 2009 Author Share Posted September 30, 2009 Sorry, still very new at this. How would I implement that for this code: $images=array("jpg1","jpg2","jpg3");foreach ($images as $value){echo <<<gallery <div class="imgcontainer"> <a href="java script:void(0);" onclick="showWindow('$value')"> <img alt="$value" src="images/products/thumbs/$value.jpg" height="120" width="160" /> </a> </div> <!-- END .imgcontainer --> <!-- BEGIN #$value --> <div class="popupdiv" id="$value"> <div id="subdiv"> <div class="close"> <a href="java script:void(0);" onclick="CloseWindow('$value')">Close Gallery</a> </div> <a href="http://woodturningvt.com/images/products/$value.JPG" target="blank"> <!-- full size view --> <img src="http://woodturningvt.com/images/products/$value.JPG" alt="$value" title="click for full size view"/> </a> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam porttitor rutrum massa ac fringilla. Proin fringilla tincidunt dui, et condimentum nulla viverra vitae. Proin ipsum mauris, suscipit ac adipiscing ut, porttitor eu velit. Vivamus blandit orci vel justo sagittis viverra. In hac habitasse platea dictumst. Ut id eros sodales arcu facilisis tristique a cursus metus. Praesent iaculis egestas turpis sed hendrerit. Etiam eros eros, iaculis a ultrices in, sagittis in erat. Curabitur tristique purus eu nisi euismod ut rutrum mauris tincidunt. Quisque sit amet sem nunc. Sed sed eleifend lacus. </p> </div> <!-- END .subdiv --> </div> <!-- END .popupdiv --> <!-- END #$value --> gallery;?> Would it be this: $images=array(scandir('c:\path\to\files'));foreach ($images as $value){ if (is_dir($value)) { continue; }echo <<<gallery <div class="imgcontainer"> <a href="java script:void(0);" onclick="showWindow('$value')"> <img alt="$value" src="images/products/thumbs/$value.jpg" height="120" width="160" /> </a> Link to comment Share on other sites More sharing options...
justsomeguy Posted September 30, 2009 Share Posted September 30, 2009 Basically yeah, but the first line should be this:$images=scandir('c:\path\to\files'); Link to comment Share on other sites More sharing options...
lastlifelost Posted September 30, 2009 Author Share Posted September 30, 2009 Here's the (final?) permutation of this code and a link to the test page. It works great except that it's indexing a directory as a file. Am I missing something?http://woodturningvt.com/gallery2.php <?php $images=scandir('images/products'); foreach ($images as $value) { if (is_dir($value)) { continue; } echo <<<gallery <div class="imgcontainer"> <a href="java script:void(0);" onclick="showWindow('$value')"> <img alt="$value" src="http://woodturningvt.com/images/products/thumbs/$value" height="120" width="160" /> </a> </div> <!-- END .imgcontainer --> <!-- BEGIN #$value --> <div class="popupdiv" id="$value"> <div id="subdiv"> <div class="close"> <a href="java script:void(0);" onclick="CloseWindow('$value')">Close Gallery</a> </div> <a href="http://woodturningvt.com/images/products/$value" target="blank"> <!-- full size view --> <img src="http://woodturningvt.com/images/products/$value" alt="$value" title="click for full size view"/> </a> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam porttitor rutrum massa ac fringilla. Proin fringilla tincidunt dui, et condimentum nulla viverra vitae. Proin ipsum mauris, suscipit ac adipiscing ut, porttitor eu velit. Vivamus blandit orci vel justo sagittis viverra. In hac habitasse platea dictumst. Ut id eros sodales arcu facilisis tristique a cursus metus. Praesent iaculis egestas turpis sed hendrerit. Etiam eros eros, iaculis a ultrices in, sagittis in erat. Curabitur tristique purus eu nisi euismod ut rutrum mauris tincidunt. Quisque sit amet sem nunc. Sed sed eleifend lacus. </p> </div> <!-- END .subdiv --> </div> <!-- END .popupdiv --> <!-- END #$value -->gallery; } ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted September 30, 2009 Share Posted September 30, 2009 Yeah, is_dir needs the path. Better to define the path on top and then just use that with is_dir. $dir = 'images/products'; $images=scandir($dir); foreach ($images as $value) { if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) { continue; } Link to comment Share on other sites More sharing options...
lastlifelost Posted September 30, 2009 Author Share Posted September 30, 2009 Perfect! Works like a charm now. Link to comment Share on other sites More sharing options...
jlhaslip Posted October 1, 2009 Share Posted October 1, 2009 If you only want to grab the Image files of a specific type (ie: .jpg), check out the glob function.http://php.net/glob Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.