$image = $_GET['image']; return "<img src='assets/images/$image.jpg' />";
//GetPostData //23dec2005 by Dimitri Hilverda //A small snippet to get post data from the url and place it. //Made this version for images but it could be used for any post data. //make a link like this on site 1 http://www.domainname.com/page.html?image=imagename //image name in url must be without an extention //edit this for extention: $ext= ".jpg"; //edit this for default image when no post data is found: $defimg= "pand"; //no extention //endconfig $image = $_GET['image']; if (($image == '')) { $image = $defimg; } return "<img src='assets/images/$image$ext' />";
if(isset($_GET['image'])) { $image = $_GET['image']; return "<img src='assets/images/$image.jpg' />"; } else { return "<img src='assets/images/$image.jpg' />"; }
if(isset($_GET['image']) && is_file('assets/images/'.$_GET['image'].'jpg')) { return "<img src='assets/images/".$_GET['image']."jpg' />"; }else{ // no image requested or requested image file does not exist return "<img src='assets/images/default.jpg' />"; }
//GetPostImage //23dec2005 by Dimitri Hilverda //A small snippet to get post data from the url and place it. //When no post data is found it wil place a random image from the same folder //make a link like this on site 1 http://www.domainname.com/page.html?image=imagename //image name in url must be without an extention //edit this for extention: //Postdata only $ext= ".jpg"; //Define what folder to use $dir=opendir("assets/images/acc"); $directory=("assets/images/acc/"); $pattern=".(GIF|JPG|gif|jpg|jpeg|JPEG)$"; //for random part of the snippet //endconfig $image = $_GET['image']; if(isset($_GET['image']) && is_file($directory.$_GET['image'].$ext)) { $output= "<img src='$directory$image$ext' />"; }else{ $s=readdir($dir); $count=0; $image = array(); while($s) { if(ereg($pattern, $s)) { $image[$count]=$s; $count++; } $s=readdir($dir); } closedir($dir); //Spit it out srand(time()); $limit=count($image); $limit--; $randNum=mt_rand(0,$limit); $filename = "$image[$randNum]"; $output= "<img src='$directory$filename'>"; } return $output;
Hi,
to clarify things a little bit, you’re using get solution, which you can use either form or simple link to access and change the parameter, like the one you use right now. To check whether it was set or not, use isset function.
if(isset($_GET['image'])) { $image = $_GET['image']; return "<img src='assets/images/$image.jpg' />"; } else { return "<img src='assets/images/$image.jpg' />"; }
If you want to use post, you can set the action on the form to post, which the parameter name and value will not appear on the address bar, and you can access it using $_POST[’image’], the same way as you did with GET.
don’t you think that sometime PHP is not that consistent in defining their function name? For example isset and is_file. Why is_file using underscore, but yet isset is not.
Anyway, it’s 5 am in here, that’s why I come up with this weird questions.