chrish Posted October 15, 2008 Share Posted October 15, 2008 Hey, I was following a tutorial which demonstrates how to create an Ajax gallery. I finished it but the problem is is that their code is broken and gives me the error in the title of this thread. This has gotta be finished real soon, so i'd appreciate some help as soon as you can! You can see the error live here: http://www.andy-cooke.com/gallery.php?work=uni-brief-1-visual-communication The complete error is: Warning: Invalid argument supplied for foreach() in /home/andy/domains/andy-cooke.com/public_html/functions.php on line 23 Warning: Invalid argument supplied for foreach() in /home/andy/domains/andy-cooke.com/public_html/functions.php on line 35 Here is the page, functions.php: <?php /* Let's be honest here; I didn't write this all myself. I just edited it to suit my needs. The important thing is that I learned something from it, and I did. http://www.tutorialtoday.com/read_tutorial/5751/2/? */ function count_total_images($images) { $count = 0; foreach($images as $file) { $type = strtolower(strrchr($file, '.')); $name = substr($file, 0, -4); $ending = substr($name, -6); if(($type == '.jpg' || $type == '.gif' || $type == '.png') && $ending != '_thumb') { $count++; } } return $count; } function show_main($path, $images, $width, $height) { foreach($images as $value) { $name = substr($value, 0, -4); $ending = substr($name, -6); if($ending !== '_thumb') { echo '<a id="fulllink" target="_blank" href="'.$path.$value.'"><img id="full" src="'.$path.$value.'" width="'.$width.'" height="'.$height.'" /></a><br />'; break; } } } function show_thumbnails($total, $path, $images, $twidth, $theight) { echo '<a href="javascript:prev('.$total.')" style="font-size: 30px;"><</a>'; foreach($images as $value) { $fullpath = $path.$value; $type = strtolower(strrchr($fullpath, '.')); list($width, $height) = getimagesize($fullpath); $image_p = imagecreatetruecolor($twidth, $theight); $name = substr($value, 0, -4); $ending = substr($name, -6); // skip the thumbnails if($ending !== '_thumb') { $i++; // create a thumbnail if we need to if(!file_exists($path.$name.'_thumb'.$type)) { if($type == '.gif') { $image = imagecreatefromgif($fullpath); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $twidth, $theight, $width, $height); imagegif($image_p, $path.$name.'_thumb'.$type); }elseif($type == '.jpg') { $image = imagecreatefromjpeg($fullpath); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $twidth, $theight, $width, $height); imagejpeg($image_p, $path.$name.'_thumb'.$type); }else{ $image = imagecreatefrompng($fullpath); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $twidth, $theight, $width, $height); imagepng($image_p, $path.$name.'_thumb'.$type); } } if($i > 5) { echo '<a href="javascript:show_full(' . $path . $name . $type . ')"><img width="' . $twidth . '" height="' . $theight . '" id="thumb_' . $i . '" style="display:none;margin: 10px 2px 0px 2px;border:0px;" src="' . $path . $name . '_thumb' . $type . '" alt="" /></a>'; }else{ echo '<a href="javascript:show_full('.$path.$name.$type.')"><img width="'.$twidth.'" height="'.$theight.'" id="thumb_'.$i.'" style="margin: 10px 2px 0px 2px;border:0px;" src="'.$path.$name.'_thumb'.$type.'" alt="" /></a>'; } } } echo '<a href="javascript:next('.$total.')" style="font-size: 30px;">></a>'; } ?> Line 23: foreach($images as $value) { Line 35: foreach($images as $value) { I didn't write the code, so i'm not entirely sure where this error has come from. I think it's because $images or $value is an empty array, but i'm not sure how to fix this. Thanks for your help ??? Link to comment https://forums.phpfreaks.com/topic/128585-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
aeonsky Posted October 15, 2008 Share Posted October 15, 2008 Most likely the arrays are empty. Need to see 'gallery.php'. Link to comment https://forums.phpfreaks.com/topic/128585-invalid-argument-supplied-for-foreach/#findComment-666398 Share on other sites More sharing options...
chrish Posted October 15, 2008 Author Share Posted October 15, 2008 Actually it's galleryget.php, gallery.php controls the navigation/content (it's loaded from a database). Here's galleryget.php: <?php /* Let's be honest here; I didn't write this all myself. I just edited it to suit my needs. The important thing is that I learned something from it, and I did. http://www.tutorialtoday.com/read_tutorial/5751/2/? */ include("functions.php"); $path = "images/work/" . $_GET['work'] . "/"; $images = array(); if($handle = opendir($path)) { while(false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $images[] = $file; } } $total = count_total_images($images); } else { die('Could not access gallery.'); } closedir($handle); ?> <script src="js/ajax.js" language="javascript" type="text/javascript"></script> <?php show_main($path, $pics, 581, 403); echo '<div style="white-space: nowrap;">'; show_thumbnails($total, $path, $pics, 100, 75); echo '</div>'; ?> Link to comment https://forums.phpfreaks.com/topic/128585-invalid-argument-supplied-for-foreach/#findComment-666401 Share on other sites More sharing options...
aeonsky Posted October 15, 2008 Share Posted October 15, 2008 At the end of galleryget.php try doing 'echo images[0];' to see what comes up. Link to comment https://forums.phpfreaks.com/topic/128585-invalid-argument-supplied-for-foreach/#findComment-666406 Share on other sites More sharing options...
DarkWater Posted October 15, 2008 Share Posted October 15, 2008 At the end of galleryget.php try doing 'echo images[0];' to see what comes up. Actually, do; echo '<pre>' . print_r($images, TRUE) . '</pre>'; Link to comment https://forums.phpfreaks.com/topic/128585-invalid-argument-supplied-for-foreach/#findComment-666425 Share on other sites More sharing options...
chrish Posted October 15, 2008 Author Share Posted October 15, 2008 Okay, so when I was putting in that code I noticed something. Originally, the $images variable was called $pics, but I changed the name of it. I noticed that i'd missed two of them so they were still $pics which caused some problems. So it works now to a certain degree. A new problem has arrised! Although I think it's javascript related, not PHP. If you go to http://www.andy-cooke.com/gallery.php?work=uni-brief-1-visual-communication you'll notice that the images show up, but when you click on the thumbnails they don't do anything. I'm not sure why this is - any suggestions? Link to comment https://forums.phpfreaks.com/topic/128585-invalid-argument-supplied-for-foreach/#findComment-666516 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.