anderson_catchme Posted August 27, 2014 Share Posted August 27, 2014 This is my code for displaying images from a folder: function generate_img ($imagepointer, $imageornot){ if($imageornot == 1){ $var = getcwd(); $dirlocation = $var."\\imageuploads\\".$imagepointer; $files = array_diff(scandir($dirlocation), array('..', '.')); foreach ($files as $image){ $data[] = array('label' => '<img src="/imageuploads/'.$imagepointer.'/'.$image.'" alt="thumbnail"> <br/>'); }; return $data; } else { return FALSE; } } I'm using it with print_r: <p><?php print_r(generate_img($imagepointer, $imageornot)); ?></p> Working ok, but obviously not good for a live site. Is implode() a good idea in this situation? And how would I use it?Appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/290672-using-implode-in-array-to-string-conversions/ Share on other sites More sharing options...
cpd Posted August 27, 2014 Share Posted August 27, 2014 I think you're asking if there's a cleaner solution as opposed to can you use implode. Personally, I'd store just the URLs and loop with a foreach statement to print them. $dirs = array_diff(scandir($dirlocation), array('..', '.')); array_walk($dirs, function(&$v, $k, $d) { $v = $d . $v; }, $dirlocation); // HTML section <?php foreach($dirs as $url) : ?> // IMG tag with SRC = $url <?php endforeach; ?> Quote Link to comment https://forums.phpfreaks.com/topic/290672-using-implode-in-array-to-string-conversions/#findComment-1489034 Share on other sites More sharing options...
Solution cyberRobot Posted August 27, 2014 Solution Share Posted August 27, 2014 (edited) @anderson_catchme - Do you need the associative array for the function output? If not, you could change this: $data[] = array('label' => '<img src="/imageuploads/'.$imagepointer.'/'.$image.'" alt="thumbnail"> <br/>'); To this: $data[] = '<img src="/imageuploads/'.$imagepointer.'/'.$image.'" alt="thumbnail"> <br/>'; And then use implode() here: <p><?php echo implode(generate_img($imagepointer, $imageornot)); ?></p> Edit: sorry I forgot the to add "echo" before the implode() function. I guess I'm not quite awake yet. Edited August 27, 2014 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/290672-using-implode-in-array-to-string-conversions/#findComment-1489038 Share on other sites More sharing options...
cpd Posted August 27, 2014 Share Posted August 27, 2014 By putting the HTML in your array you're limiting yourself. HTML is abouoti presentation, not business logic, therefore you shouldn't really have it in the php as you've done. Quote Link to comment https://forums.phpfreaks.com/topic/290672-using-implode-in-array-to-string-conversions/#findComment-1489042 Share on other sites More sharing options...
anderson_catchme Posted August 27, 2014 Author Share Posted August 27, 2014 Thanks for everyone's responses. Quote Link to comment https://forums.phpfreaks.com/topic/290672-using-implode-in-array-to-string-conversions/#findComment-1489099 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.