Forum Moderators: coopster

Message Too Old, No Replies

Show image dependent on variable

         

terrybarnes

10:31 pm on Jun 25, 2009 (gmt 0)

10+ Year Member



Apologies for this but I really have no idea how to program php but I'm presuming this could be the best way to go! I'm using Wordpress at the moment and what I'd like to do is add an image in a containing div - with that image dependent on what the variable passed to the page is:

i.e. /?var=image-1 would show image 1
, /?var=image-2 would show image 2

etc etc.

Is this possible with php? I'm not expecting anybody to write me a script but if someone could help point me in the right direction that would be fantastic.

chief stains

11:58 am on Jun 26, 2009 (gmt 0)

10+ Year Member



What exactley are you trying to do, because Wordpress may have a plugin that will do it for you. If not, you have two options really.

First, you can name all your files in the folder image-1.jpg, image-2.jpg and when the variable is passed in create a new variable called $imageFile = $_GET['var'] . ".jpg"; and then in your html have <img src="/images/<?php echo $imageFile ?>">. This will only really work if you have full control over the naming conventions for the files.

Secondly, if users are uploading files, you can store references to the images in a database. Store an id for the image and its location, for example id=1 location=someImageName.jpg and then pass the id of the image to the page i.e. /?imageId=1. Then do a select for the image location based on its id and print it out inside the <img> tags.

hth

terrybarnes

12:09 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



Thanks for this - very much appreciated... Basically it's a step by step selections so the user first selects a style of design and then they choose a colour scheme - from that colour scheme selection I'd then like to move them on to a form whereby their colour selection is previewed as a thumbnail.

I've hunted around the WP plugins and can't find one that would do the job, unfortunately.

Your idea though sounds perfect - as I do have control over the naming of the files and no users will be uploading files to the site.

I've only one question: Where do I add the new variable?

$imageFile = $_GET['var'] . ".jpg";

terrybarnes

12:19 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



Oh, hold on there... I've done it!

I just inserted this <?php echo $_GET["var"]; ?>.jpg

and that seems to be bringing in the correct image!

Thanks for your help to put me on the right track.

chief stains

2:57 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



Ok, no problem, glad it worked...