pufferz Posted April 15, 2009 Share Posted April 15, 2009 I have the following php script that was taken from http://ma.tt/scripts/randomimage/ . I want to use this extensively in an HTML page so I turned the script into a basic function so I could add a parameter to it, as to make it more flexible. My question is, now that I edited the parameter into the script, how can I specify this parameter within the HTML page? Heres the Script: <?php function previewImage($directory){ $folder = $directory; // Space seperated list of extensions, you probably won't have to change this. $exts = 'jpg jpeg png gif'; $files = array(); $i = -1; // Initialize some variables if ('' == $folder) $folder = './'; $handle = opendir($folder); $exts = explode(' ', $exts); while (false !== ($file = readdir($handle))) { foreach($exts as $ext) { // for each extension check the extension if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive $files[] = $file; // it’s good ++$i; } } } closedir($handle); // We’re not using it anymore mt_srand((double)microtime()*1000000); // seed for PHP < 4.2 $rand = mt_rand(0, $i); // $i was incremented as we went along header('Location: '.$folder.$files[$rand]); // Voila! } ?> Quote Link to comment Share on other sites More sharing options...
eRott Posted April 15, 2009 Share Posted April 15, 2009 Well, that depends on how / where you are getting this value to pass to the function from. Is it being input from a user from a form? Or is it generated from a database via PHP? A quick example: If you are passing a value from a form which a user has submitted: previewImage($_POST['--field--']); If you are generating this directory from a database, you could try something like: $query = "SELECT --row-- FROM --table-- WHERE --condition here--"; $result = mysql_query($query); $data = mysql_fetch_array($result); $dir = $data['--row--']; previewImage($dir) I hope that helps. Quote Link to comment Share on other sites More sharing options...
pufferz Posted April 15, 2009 Author Share Posted April 15, 2009 Basically, all I want to do is call the script and then display the image that it returns. So something like this...but not this since its not working <html> <head> </head> <body> <src="Random.php" img src="<?php previewImage('thumb')?>" alt="Preview of Images"/> </body> </html> Also, would anyone mind skimming over the code to make sure its working/corrent? I don't want to disregard a solution because the code is cippled Quote Link to comment Share on other sites More sharing options...
eRott Posted April 15, 2009 Share Posted April 15, 2009 Basically, all I want to do is call the script and then display the image that it returns. So something like this...but not this since its not working <html> <head> </head> <body> <src="Random.php" img src="<?php previewImage('thumb')?>" alt="Preview of Images"/> </body> </html> Also, would anyone mind skimming over the code to make sure its working/corrent? I don't want to disregard a solution because the code is cippled If I remember to do it, I will look over it for you tomorrow. But the first thing I notice is your HTML; its not valid. There is no <src> tag. <img src="<?php previewImage('thumb'); ?>" alt="Preview of Images" /> Take care. EDIT: After taking a quick peak at the script from the link you provided, your approach of turning into a function will not work. What that script does is creates an array of all the files in the directory you specify which have an extension of either .jpg, .jpeg, .png or .gif. It then randomly selects one of the options in the array and writes that image into the header of the file displaying that image (or something like that--I never cared to fully learn how that works). So using a function as the img src won't really work. 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.