Boards.ie uses cookies. By continuing to browse this site you are agreeing to our use of cookies. Click here to find out more x
Post Reply  
 
Thread Tools Search this Thread
23-04-2012, 21:33   #1
funkyflea
Registered User
 
funkyflea's Avatar
 
Join Date: Nov 2004
Location: South East
Posts: 2,561
Display a different image depending on variable

Hi,

I need to do something like display a small icon on a webpage that differs based on the value of a variable..

So I was thinking naming my images in a folder 1.jpg, 2.jpg, 3.jpg etc..

then use something like:

Code:
<?php echo $_GET["var"]; ?>.jpg
Has anyone done this before?

Any tips?
funkyflea is offline  
Advertisement
23-04-2012, 23:48   #2
arleitiss
Registered User
 
Join Date: Jun 2009
Posts: 2,079
Quote:
Originally Posted by funkyflea View Post
Hi,

I need to do something like display a small icon on a webpage that differs based on the value of a variable..

So I was thinking naming my images in a folder 1.jpg, 2.jpg, 3.jpg etc..

then use something like:

Code:
<?php echo $_GET["var"]; ?>.jpg
Has anyone done this before?

Any tips?
What I've done before is:

Code:
<img src="images/background<?php
$random = rand(0,10); 
echo($random);
?>.jpg"/>
That generated random number from 0-10 and then I had multiple images named background1.jpg, background2.jpg and so on, and it randomly displayed. Worked very well, though later I changed to same technique but using JavaScript as I could allow user to regenerate number and change background with click of a button.

Sorry haven't done PHP for a month, might have some typos.

And yes you could have like:

Code:
<img src="background<?php
$image = $_GET['var'];
echo($image);
?>.jpg"/>
Just make sure user always has a GET method set, otherwise image wont appear whatsoever, so might as well do:

Code:
<img src="background<?php
if(isset($_GET['var'])){
$image = $_GET['var'];
echo($image);
}
else{
echo("1");
}
?>.jpg" />
I think that should work, if GET is set, then it will specify value of it, if it's not then it will output 1.jpg (or whatever you want default to be).
That's what I did before. Hopefully that helps.

Last edited by arleitiss; 23-04-2012 at 23:54.
arleitiss is offline  
Thanks from:
23-04-2012, 23:55   #3
arleitiss
Registered User
 
Join Date: Jun 2009
Posts: 2,079
Of course you could cut out that $image part. and just use $_GET['var'];
but just in my personal preference and experience, I rather make a hard variable into easy.
arleitiss is offline  
Thanks from:
24-04-2012, 04:13   #4
DonkeyStyle \o/
Registered User
 
Join Date: Oct 2004
Posts: 6,514
I wouldn't go straight-up echo'ing the contents of $_GET like that, since it could be anything.
http://en.wikipedia.org/wiki/Cross-site_scripting

Always take measures to ensure you're getting only what you want and nothing else.
http://www.w3schools.com/php/func_filter_var.asp
http://www.php.net/manual/en/functio...-match-all.php
DonkeyStyle \o/ is offline  
(2) thanks from:
26-04-2012, 22:20   #5
funkyflea
Registered User
 
funkyflea's Avatar
 
Join Date: Nov 2004
Location: South East
Posts: 2,561
What I'm trying to do is this:

Shopping cart (woocommerce in wordpress)

When user adds item to cart > a little icon appears.
When user adds another item to the cart, the icon changes to graphic 2, and 3 and so on up to around 10.

I'm pretty new to PHP so I'm going to try some of those things above and see.
funkyflea is offline  
Advertisement
28-04-2012, 18:02   #6
Art_Wolf
Moderator
 
Art_Wolf's Avatar
 
Join Date: Nov 2003
Location: Boston, MA
Posts: 996
Send a message via ICQ to Art_Wolf Send a message via AIM to Art_Wolf Send a message via MSN to Art_Wolf Send a message via Yahoo to Art_Wolf Send a message via Skype™ to Art_Wolf
For something like that you could use PHP's GD library. Instead of having an individual image for each item have the base layer and have PHP overlay the count on top.

http://php.net/manual/en/image.examples-png.php
Art_Wolf is offline  
Thanks from:
28-04-2012, 21:30   #7
OneIdea
Registered User
 
Join Date: Jan 2012
Posts: 240
Quote:
Originally Posted by funkyflea View Post
What I'm trying to do is this:

Shopping cart (woocommerce in wordpress)

When user adds item to cart > a little icon appears.
When user adds another item to the cart, the icon changes to graphic 2, and 3 and so on up to around 10.

I'm pretty new to PHP so I'm going to try some of those things above and see.
Based on what your saying, there would all ready be a variable that takes a shopping cart count value, 1,2,3 etc... so you really only need to find that variable and use it to pull images from a set folder.

PHP Code:
echo '<img src="/images/cart_count/' $variable ' .jpg" border="0">'
OneIdea is offline  
Thanks from:
02-05-2012, 09:58   #8
funkyflea
Registered User
 
funkyflea's Avatar
 
Join Date: Nov 2004
Location: South East
Posts: 2,561
Quote:
Originally Posted by OneIdea View Post
Based on what your saying, there would all ready be a variable that takes a shopping cart count value, 1,2,3 etc... so you really only need to find that variable and use it to pull images from a set folder.

PHP Code:
echo '<img src="/images/cart_count/' $variable ' .jpg" border="0">'
Yep, this was the original idea, just having trouble finding where/which variable (if at all) the cart count is being stored at the moment!
funkyflea is offline  
02-05-2012, 10:41   #9
The Corinthian
Moderator
 
The Corinthian's Avatar
 
Join Date: Jun 2001
Location: The 2nd Circle of Hell
Posts: 14,546
How do you store what's in the cart? A session variable? Hidden form elements? URL encoded ID's?

Depending on which your code will differ hugely at this stage, but ultimately you're looking to do the same thing, which is to get a count of items and assign it to an integer variable (we'll call it $num here).

The second stage is using this integer variable to select or generate the image. If you're limiting yourself to ten such images, you could simply pre-prepare ten such images and use a naming convention for them (such as img_X.png, where X is the number). Then it becomes a case of:
PHP Code:
$imageName "img_".$num.".png"
And you can then echo it accordingly in an image element.

Dynamically generating such images is relatively easy using the GD library, as Art_Wolf suggested. A simple example would be that you call your image generating script as if it were an image, and this in turn would generate the image and output it with the correct content-type:
PHP Code:
$o_Img imagecreate(5015);
$bg imagecolorallocate($o_Img255255255);
$textcolor imagecolorallocate($o_Img25500);
imagestring($o_Img500$num$textcolor);
header("Content-type: image/png");
imagepng($o_Img); 
That's all there is to it, but as I said first how you keep track of the basket is important, because that is how you get your value for $num in the first place.

What are you using for the basket? A third party script? If so, you might want to Google how it does it, examine the code and/or print to the buffer what it is storing in the session.
The Corinthian is offline  
Thanks from:
Advertisement
03-05-2012, 10:55   #10
OneIdea
Registered User
 
Join Date: Jan 2012
Posts: 240
Quote:
Originally Posted by funkyflea View Post
Yep, this was the original idea, just having trouble finding where/which variable (if at all) the cart count is being stored at the moment!
Have you tried $quantities
PHP Code:
echo '<img src="/images/cart_count/' $quantities ' .jpg" border="0">'
OneIdea is offline  
03-05-2012, 11:37   #11
The Corinthian
Moderator
 
The Corinthian's Avatar
 
Join Date: Jun 2001
Location: The 2nd Circle of Hell
Posts: 14,546
Quote:
Originally Posted by funkyflea View Post
Shopping cart (woocommerce in wordpress)
After a quick Google, the number of items in a shopping cart appears to be returned by the following object method:
PHP Code:
$woocommerce->cart->cart_contents_count 
The Corinthian is offline  
Post Reply

Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline

Insert Image
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Please sign up or log in to join the discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search