Jump to content

Recommended Posts

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 : download5.pngdownload4.jpgdownload3.jpgdownload2.jpgdownload1.jpg

 

 

 

$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">';
}

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().

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.

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]>'; 

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

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]">";

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">';

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.