Jump to content

Small bit of script not working


screaminbean

Recommended Posts

Alrighty, I'm new at this, so I suppose I ask that you don't just dismiss me as a moron, but I have a small bit of script that isn't working for me. It's supposed to randomly display an image from a pool of 10 on my website. Here's the code:

 

<?php

$images = 10;

$path = "afp://path/path/path/path/";

$random = rand([1], $images);

echo = "<img src=$path"."$random".".png"." border=\'0\'>";

?>

 

I don't know what I'm doing wrong. If someone could help, that would be dandy.

Link to comment
Share on other sites

I'm going to try not to aggravate too many people with my lack of experience here. I have the script written directly into my html. When I check it on both Safari and Firefox it doesn't show anything. I'm very very new at this, and I don't yet know proper formatting or anything, so it may be something along those lines. The project is saved as a php document, though.

Link to comment
Share on other sites

The brackets around the "1" is definitely wrong. But, there are other problems.

 

1. The path is not enclosed in quotes. It shouldn't be a problem here since the names don't have any spaces, but it is invalid code nonetheless.

 

2. The border parameter has escaped single quotes. But, since these are created in a double quoted string they wouldn't be escaped and the backslashes would be in the code. That may be causing the problem.

 

You should first create a static image tag to one of the images and ensure the full path works. Then you can make it dynamic. This would be a more appropriate code for the above

$images = 10;
$path = "afp://path/path/path/path/";
$random = rand(1, $images);
echo = "<img src=\"{$path}{$random}.png\" border=\"0\" />";

 

However, are the only .png images in that folder the ones that you want to display randomly? If so, you can clean up that logic and not even have to name your images 0, 1, 2, etc.

$path = "afp://path/path/path/path/";
$images = glob("$path*.png"); //Get array of all PNG images
$randImage = $images[array_rand($images)]; //Get random image
echo = "<img src=\"{$randImage}\" border=\"0\" />";

 

I have the script written directly into my html

 

What does that mean exactly. You need to be writing a PHP file and be requesting the file through a web-server (which you could be running on your PC). You can't just open the file in your web browser. IE, FF, etc. don't have any clue on how to execute PHP code. That takes place on the web server.

Link to comment
Share on other sites

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.