morrism35 Posted November 1, 2015 Share Posted November 1, 2015 want to create a random image from these 5 images that I have in the template array every time I refresh the home page. here is my code. my output has been : $dir = "Images"; $DirEntries = scandir($dir, 1); $template=preg_grep('/^download\d+\.(gif|png|jpg)$/', $DirEntries); foreach($template as $temp){ shuffle($temp); echo <IMG SRC="'. $temp.'"alt="alt">'; } Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/ Share on other sites More sharing options...
Jacques1 Posted November 1, 2015 Share Posted November 1, 2015 The code doesn't really make sense. You seem to have trouble understanding loops, which I already noticed in your previous thread. If you want a single picture, then don't use a loop. The point of a foreach loop is to iterate over all elements. This also means you cannot shuffle $temp, because that's a particular element (in this case a path string). Remove everything after the preg_grep() call and then select a random picture with array_rand(). Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525136 Share on other sites More sharing options...
morrism35 Posted November 1, 2015 Author Share Posted November 1, 2015 I understand about the foreach and I do know how to use the for loop, but for some reason it just didn't click in these situations My book instructions told be to use shuffle() in the instructions for the assignment. Array_rand isn't mentioned in my text book but I have seen it in searching for this issue. i will take a look at the array rand but not sure if my instructor will accept that. Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525138 Share on other sites More sharing options...
Jacques1 Posted November 1, 2015 Share Posted November 1, 2015 If you're artificially restricted to shuffle(), you'd shuffle the array and then fetch the first element. Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525140 Share on other sites More sharing options...
morrism35 Posted November 1, 2015 Author Share Posted November 1, 2015 what do we mean by fetch the first element. i've only been up to chapter 5 in this book. Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525141 Share on other sites More sharing options...
Jacques1 Posted November 1, 2015 Share Posted November 1, 2015 shuffle($template); echo $template[0]; You should use a better name than $template, though. Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525142 Share on other sites More sharing options...
morrism35 Posted November 2, 2015 Author Share Posted November 2, 2015 Thank you I 'am only receiving one image but it is not displaying that image only a little image caption, like it knows it's supposed to display an image but can't. $dir = "Images"; $DirEntries = scandir($dir, 1); $template=preg_grep('/^download\d+\.(gif|png|jpg)$/', $DirEntries); shuffle($template); echo '<img src=\$template[0]>'; Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525201 Share on other sites More sharing options...
Barand Posted November 2, 2015 Share Posted November 2, 2015 The string you are echoing is in single quotes therefore the variable is not expanded. Use double quotes echo "<img src='\$template[0]' >"; Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525202 Share on other sites More sharing options...
morrism35 Posted November 2, 2015 Author Share Posted November 2, 2015 echo "<img src='\$template[0]' >"; Threw an error: Parse error: syntax error, unexpected '\' (T_NS_SEPARATOR), expecting ',' or ';' in /homepages/6/d498253868/htdocs/morris/web182/ChineseZodiac/IncludesFolder/inc_footer.php on line 37 Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525203 Share on other sites More sharing options...
Barand Posted November 2, 2015 Share Posted November 2, 2015 Make the directory separator / instead of \ Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525204 Share on other sites More sharing options...
morrism35 Posted November 2, 2015 Author Share Posted November 2, 2015 Parse error: syntax error, unexpected '">"' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in/homepages/6/d498253868/htdocs/morris/web182/ChineseZodiac/IncludesFolder/inc_footer.php on line 37 thanks but still getting an error, been on this small part for 2 days. echo "<img src="/$template[0]">"; Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525205 Share on other sites More sharing options...
Jacques1 Posted November 2, 2015 Share Posted November 2, 2015 You cannot put raw double quotes into a double-quoted string. How is PHP supposed to figure out which ones are literal quotes and which ones are string delimiters? If you insist on using double quotes for the HTML attributes (instead of single quotes as suggested by Barand), you'll have to escape them: echo "<img src=\"/$template[0]\" alt=\"random image\">"; If this was serious code, you would of course have to HTML-escape the path before inserting it into the HTML attribute: echo '<img src="/'.htmlspecialchars($template[0], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8').'" alt="random image">'; Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525207 Share on other sites More sharing options...
morrism35 Posted November 2, 2015 Author Share Posted November 2, 2015 echo "<img src=\"/$template[0]\" alt=\"random image\">"; I tried this code but it is just outputting the random image in text with a caption. Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525209 Share on other sites More sharing options...
Jacques1 Posted November 2, 2015 Share Posted November 2, 2015 Are you absolutely sure that the path should have a leading slash? According to your original code, the images are actually in a subfolder called “Images”. In that case, the path needs to look like this: Images/$template[0] Otherwise, all images are searched directly in the document root. Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525210 Share on other sites More sharing options...
morrism35 Posted November 2, 2015 Author Share Posted November 2, 2015 yes that is correct Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525211 Share on other sites More sharing options...
morrism35 Posted November 2, 2015 Author Share Posted November 2, 2015 Hey it works. I need to go back and read or youtube sections on using single, double quotes, htmlentities, and escape characters. The syntax of php seems complicated and their seems to be a lot more code than say java. Maybe i'm wrong. Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525212 Share on other sites More sharing options...
morrism35 Posted November 2, 2015 Author Share Posted November 2, 2015 Thank you so much. Quote Link to comment https://forums.phpfreaks.com/topic/298989-dispaying-one-random-image-whenever-page-refreshed/#findComment-1525213 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.