tarnold4 Posted February 14, 2015 Share Posted February 14, 2015 Im very new to php, I wanted to see if anyone could help me with the following. I have to take a function, along with the count function, to randomly select one of the array values. Use the return keyword to return this value and end the function. The value returned should be one of the provided file names. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 14, 2015 Share Posted February 14, 2015 What is the code you have tried so far? Quote Link to comment Share on other sites More sharing options...
tarnold4 Posted February 17, 2015 Author Share Posted February 17, 2015 This is what i came up with, the whole thing is supposed to randomly show a different image everytime the webpage is loaded. When i test it, all it does is pretty much shows the title, but no image <html> <head> <title>JFQ Turnings</title> </head> <body bgcolor="#ffffff" text="#000000"> <img src="jfqturnings.gif" alt="JFQ Turnings, coming soon." width="864" height="100"> <br /><img src="<?php echo getRandomImage(); ?>" alt="" width="864" height="567"> <?php echo getRandomImage(); function getRandomImage() { $images = array("candlestick.jpg", "rollingpin.jpg", "table.jpg", "table2.jpg"); $random = rand(0, 3); return $images[$random]; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 17, 2015 Share Posted February 17, 2015 The code works fine for me. If you're just seeing a broken image, perhaps the file path isn't correct. Side note: you could use array_rand(). With rand(), you'll need to manually change the arguments every time the number of items in $images changes. Quote Link to comment Share on other sites More sharing options...
tarnold4 Posted February 17, 2015 Author Share Posted February 17, 2015 I thought that to but, its fine. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 17, 2015 Share Posted February 17, 2015 I thought that to but, its fine. Are you seeing a broken image? If so, did you go into your browser window's source code to make sure the file name is coming through? If the name is fine, the problem is most likely due to the path being incorrect...or maybe the file names don't match the ones listed in the array. Quote Link to comment Share on other sites More sharing options...
JadWS Posted February 18, 2015 Share Posted February 18, 2015 Are you seeing a broken image? If so, did you go into your browser window's source code to make sure the file name is coming through? If the name is fine, the problem is most likely due to the path being incorrect...or maybe the file names don't match the ones listed in the array. wait a second ... if he didnt save the img in the same folder isnt showing a broken image something normal ? maybe upload it and put it url instad of "picname.jpg" part .. it may work. Here its css/html more then PHP Quote Link to comment Share on other sites More sharing options...
tarnold4 Posted February 18, 2015 Author Share Posted February 18, 2015 I just wanna thank everyone for their help, but i finally got it working. I was so wrapped up in the code and not realizing.... I forgot to save the images in the same folder... DUH Quote Link to comment Share on other sites More sharing options...
JadWS Posted February 18, 2015 Share Posted February 18, 2015 so after all i was right best answer ? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 18, 2015 Share Posted February 18, 2015 so after all i was right best answer ? As cyberRobot had already told him to check the file locations a couple of times before you chimed in then no biscuit this time Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 18, 2015 Share Posted February 18, 2015 As cyberRobot had already told him to check the file locations a couple of times before you chimed in then no biscuit this time Yeah for me! Quote Link to comment Share on other sites More sharing options...
JadWS Posted February 18, 2015 Share Posted February 18, 2015 As cyberRobot had already told him to check the file locations a couple of times before you chimed in then no biscuit this time Well a cookie? Quote Link to comment 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.