Jump to content

dynamic image display using php id tag... pls help


rebeat

Recommended Posts

Hi,

I am trying to display images within my page dynamically using the ? call function, the way i want to link and include an image for example is: index.php?image=photo1.jpg

the code on my index.php page is as follows:

<?php

if (!$image) {
echo "<img src='$image'>";
}

else {
echo "no image to display";
}
?>

this is not working, just keps showing me the else function... i have played with it and googled for HOURS, even trying the print and include commands... include writes the jpg as ascii..

any help would be greatly appreciated :)

cheers, wayne
Link to comment
Share on other sites

It will never show for you because you are saying if the variable image is not set, show the image.

Try this:
[code]if (isset($_GET['image']))
{
$image = $_GET['image'];
// Do some validation on the image string here, if passes then show image
// Obviously need to provide full path to image unless it is in same folder as script
// eg <img src="images/'.$image.'" />
echo '<img src="'.$image.'" />';
}
else
{
echo 'No image to display';
}[/code]
Link to comment
Share on other sites

[!--quoteo(post=358275:date=Mar 25 2006, 12:19 PM:name=annihilate)--][div class=\'quotetop\']QUOTE(annihilate @ Mar 25 2006, 12:19 PM) [snapback]358275[/snapback][/div][div class=\'quotemain\'][!--quotec--]
// Do some validation on the image string here, if passes then show image
// Obviously need to provide full path to image unless it is in same folder as script
[/quote]

You can store the path and image name in the database tho and output it like "images/myimage.jpg"

I recommend this method so you do not have to rely on keeping your images in the same folder or specifically noting it in the code. Helpful if you have images divided up into catagory named folders.

I needed to do this for a photography website and it worked great. Especially when returning search results which need to display images also.
Link to comment
Share on other sites

Thankyou annihilate for the code and freakus_maximus for the tip ;) works fantastic.

on a side note to this post... i would like to be able to call a dynamic page, eg: index.php?id=page1.php within my index --but-- also to be able to call an image eg: index.php?image=images/test.jpg (this would also give me the option to insert dynamic images on dynamic pages, ie: index.php?id=page2.php&image=photo2.jpg) --but if i dont specify a page 'id' i would like just the image.

Here is the code i have so far (obviously it doesnt work correctly as i have a default 'id' value of 'home.php' in order to fill the index.php home page by default.
[code]
<?php
if (!$id) {
$id = "home.php";
$include = $id;
}
else {
$include = $id;
}
if (is_file($include) == "1") {
        include $include;
}
else {
include "404.php";
}

if (isset($_GET['image']))
{
$image = $_GET['image'];
// Do some validation on the image string here, if passes then show image
// Obviously need to provide full path to image unless it is in same folder as script
// eg <img src="images/'.$image.'" />
echo '<img src="'.$image.'" />';
}

?>
[/code]
--------------
what i am trying to achieve is IF there is JUST an index.php?image= call, then the default ?id=home.php is ignored? but still be able to call both, ie: index.php?id=page2.php&image=photo2.jpg if i need to -- i could create a blank.php page and call all images like: ?id=blank.php&image-photo1.jpg but there has to be a way to do this within the script. i could also just remove the 'home.php' value in the script, but then to view mysite.com would require mysite.com/?id=home to show default home page content.

again, any help is greatly appreciated :)

cheers, wayne.
Link to comment
Share on other sites

Try this:
[code]<?php
if ( isset($_GET['id']) && $_GET['id'] <> '' )
{
$include = $_GET['id'];
}
else // No page specified, so set a default
{
$include = 'home.php';
}

if ( is_file($include) )
{
include $include;
}
else
{
include '404.php';
}

if ( isset($_GET['image']) && $_GET['image'] <> '' )
{
$image = $_GET['image'];
// Do some validation on the image string here, if passes then show image
// Obviously need to provide full path to image unless it is in same folder as script
// eg <img src="images/'.$image.'" />
echo '<img src="'.$image.'" />';
}
?>[/code]
Link to comment
Share on other sites

Thx annihilate for the help. I have uploaded the script, if i choose JUST the ?image= command, it still seems to show the default home.php. Im not sure if what i want can be done...

* if nothing is specified, the default home.php is displayed ONLY
* if JUST ?image=photo.jpg --only the image is displayed (home.php is NOT displayed)
* if ?id=anypage.php&image=photo.jpg --both are shown dynamically

here's a working example of the script above, calling JUST the image (see bottom of page):

[a href=\"http://dunebuggies.com.au/home/newsite/test1.php?image=action_640.jpg\" target=\"_blank\"]http://dunebuggies.com.au/home/newsite/tes...=action_640.jpg[/a]

the test1.php only has the php script above, nothing else, it is including the home.php by default. maybe it needs another else command or maybe elseif to check if the id does NOT exist BUT the image does, to display it, if false, then display the home.php or 404.

I have played around with it will no success.

If it is not possible to do, i will just create a blank page say, blank.php to overwrite the home.php default

any ideas?

thanks again, wayne
Link to comment
Share on other sites

Try this:
If you view the page passing no variables in the url, then home.php is included.
If you specify an image to view, them image is shown, nothing is inlcuded.
If you specify just an id, then that is included if it exists, else 404.php is included.
If you specify a page and image, then both are shown.
[code]<?php
if ( isset($_GET['id']) && $_GET['id'] <> '' ) // If id is set, then set include to that value
{
$include = $_GET['id'];
}
elseif ( (!isset($_GET['id']) || $_GET['id'] == '') && isset($_GET['image']) ) // If id not set, but image is, don't include anything
{
$include = FALSE;
}
else // id and image not set, so include a default page
{
$include = 'home.php';
}

if ( $include != FALSE)
{
    if ( is_file($include) ) // If finds file, then include it
    {
    include $include;
    }
    else // File doesn't exist, so show 404 page
    {
    include '404.php';
    }
}

// Show the image if set
if ( isset($_GET['image']) && $_GET['image'] <> '' )
{
$image = $_GET['image'];
// Do some validation on the image string here, if passes then show image
// Obviously need to provide full path to image unless it is in same folder as script
// eg <img src="images/'.$image.'" />
echo '<img src="'.$image.'" />';
}
?>[/code]
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.