Jump to content


Photo

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


  • Please log in to reply
8 replies to this topic

#1 rebeat

rebeat
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 25 March 2006 - 05:05 PM

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

#2 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 25 March 2006 - 05:19 PM

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

Try this:
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';
}

Personal site: NewEnigma  |  Java and JSF Discussion

#3 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 26 March 2006 - 02:17 AM

[!--quoteo(post=358275:date=Mar 25 2006, 12:19 PM:name=annihilate)--][div class=\'quotetop\']QUOTE(annihilate @ Mar 25 2006, 12:19 PM) View Post[/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.


#4 rebeat

rebeat
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 March 2006 - 06:58 AM

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.
<?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.'" />';
}

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

#5 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 27 March 2006 - 11:54 AM

Try this:
<?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.'" />';
}
?>

Personal site: NewEnigma  |  Java and JSF Discussion

#6 rebeat

rebeat
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 27 March 2006 - 12:56 PM

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

#7 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 27 March 2006 - 01:45 PM

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.
<?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.'" />';
}
?>

Personal site: NewEnigma  |  Java and JSF Discussion

#8 rebeat

rebeat
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 28 March 2006 - 02:07 PM

Thankyou annihilate, it works great, just what i wanted, your a champion!!

cheers and best regards, wayne.

#9 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 28 March 2006 - 02:32 PM

No problem. Glad you have it working the way you want.
Personal site: NewEnigma  |  Java and JSF Discussion




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users