screaminbean Posted June 7, 2011 Share Posted June 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238697-small-bit-of-script-not-working/ Share on other sites More sharing options...
Drummin Posted June 7, 2011 Share Posted June 7, 2011 Try $random = rand(1, $images); Quote Link to comment https://forums.phpfreaks.com/topic/238697-small-bit-of-script-not-working/#findComment-1226605 Share on other sites More sharing options...
screaminbean Posted June 7, 2011 Author Share Posted June 7, 2011 That's what I had before, but it didn't work. I deleted the line and retyped it, and it had 1 set off in brackets like that. I didn't understand why, but I complied. Quote Link to comment https://forums.phpfreaks.com/topic/238697-small-bit-of-script-not-working/#findComment-1226609 Share on other sites More sharing options...
matthew9090 Posted June 7, 2011 Share Posted June 7, 2011 does it diplay and errors? and does it output anything? Quote Link to comment https://forums.phpfreaks.com/topic/238697-small-bit-of-script-not-working/#findComment-1226612 Share on other sites More sharing options...
screaminbean Posted June 7, 2011 Author Share Posted June 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238697-small-bit-of-script-not-working/#findComment-1226617 Share on other sites More sharing options...
Drummin Posted June 7, 2011 Share Posted June 7, 2011 Can you directly put an image name in this line and see the picture? Maybe it's a path problem. echo = "<img src=$path"."imagename".".png"." border=\'0\'>"; Quote Link to comment https://forums.phpfreaks.com/topic/238697-small-bit-of-script-not-working/#findComment-1226618 Share on other sites More sharing options...
fugix Posted June 7, 2011 Share Posted June 7, 2011 i spotted a few errors in your code. try <?php $images = 10; $path = "afp://path/path/path/path/"; $random = rand(1, $images); echo "<img src='{$path}{$random}.png' border='0'>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/238697-small-bit-of-script-not-working/#findComment-1226620 Share on other sites More sharing options...
Psycho Posted June 7, 2011 Share Posted June 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238697-small-bit-of-script-not-working/#findComment-1226621 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.