Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Display a different image depending on variable

  • 23-04-2012 8:33pm
    #1
    Registered Users, Registered Users 2 Posts: 3,135 ✭✭✭


    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:
    <?php echo $_GET["var"]; ?>.jpg
    

    Has anyone done this before?

    Any tips?
    Tagged:


Comments

  • Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


    funkyflea wrote: »
    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:
    <?php echo $_GET["var"]; ?>.jpg
    
    Has anyone done this before?

    Any tips?

    What I've done before is:
    <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:
    <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:
    <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.


  • Registered Users, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


    Of course you could cut out that $image part. and just use $_GET;
    but just in my personal preference and experience, I rather make a hard variable into easy.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    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/function.preg-match-all.php


  • Registered Users, Registered Users 2 Posts: 3,135 ✭✭✭fifth


    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.


  • Registered Users, Registered Users 2 Posts: 1,071 ✭✭✭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


  • Advertisement
  • Closed Accounts Posts: 249 ✭✭OneIdea


    funkyflea wrote: »
    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]
    echo '<img src="/images/cart_count/' . $variable . ' .jpg" border="0">';
    [/PHP]


  • Registered Users, Registered Users 2 Posts: 3,135 ✭✭✭fifth


    OneIdea wrote: »
    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]
    echo '<img src="/images/cart_count/' . $variable . ' .jpg" border="0">';
    [/PHP]

    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!


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    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]$imageName = "img_".$num.".png";[/PHP]
    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]$o_Img = imagecreate(50, 15);
    $bg = imagecolorallocate($o_Img, 255, 255, 255);
    $textcolor = imagecolorallocate($o_Img, 255, 0, 0);
    imagestring($o_Img, 5, 0, 0, $num, $textcolor);
    header("Content-type: image/png");
    imagepng($o_Img);[/PHP]
    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.


  • Closed Accounts Posts: 249 ✭✭OneIdea


    funkyflea wrote: »
    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]echo '<img src="/images/cart_count/' . $quantities . ' .jpg" border="0">'; [/PHP]


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    funkyflea wrote: »
    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]$woocommerce->cart->cart_contents_count[/PHP]


  • Advertisement
Advertisement