Jump to content

Simple question from php novice: Passing parameters from an HTML document


pufferz

Recommended Posts

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!

}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P

 

<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

Link to comment
Share on other sites

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  :P

 

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

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.