Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts

    Trying to use variable to call image .....

    Hello.

    I have a php page.

    On this page, I break out and use html to call an image.
    Rather than hand code the folder name in the image path, I would rather refer to the folder using a php variable.

    (The reason is that there will be multiple similar pages, refering to different geographical areas, and each one has it's variable set at the top of the page.

    The variable (that refers to the folder is called $area). In this case $area= "Apache Junction"

    First, I will show you the code as it is now (and does work).
    Then I will show you the code that I tried (and doesn't work).
    PS: do not get a php error, but it does not find the image)

    I appreciate any help. Buffmin

    Current Code:
    Code:
    <img src="images/Apache Junction/image1.jpg">
    PHP code that DOES NOT work
    Code:
    <img src="images/<? $area ?>/coulter13.jpg" title="">
    PS Again.... It seems like maybe it is because I have to leave a space on each side of the variable name?
    (I have to leave the spaces or in Dreamweaver, the tags turn from red to blue)
    Last edited by Buffmin; 04-25-2012 at 07:10 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, the whitespace itself is irrelevant in PHP, so do whatever to make your editor happy. The use is incorrect though:
    PHP Code:
    <?php

    $variable
    ?>
    Won't result in anything. You need to actually print it: <?php echo $area;?>. Double check as well that it doesn't show as PHP in the html source and link. If it does, make sure the page is a .php script or set to process as PHP. Always use full <?php tags and never short tags with <? (although they will officially make <?= a shortcut echo valid in any PHP build, I don't think that its actually happened yet).

    Although you do have me slightly confused. If this variable is just a path to an image, then that image path will show in its entirety in the link.

    Edit:
    I stand corrected. <?= will now work regardless of short_open_tag directive as of PHP 5.4.0 (which is brand new mind you).

  • Users who have thanked Fou-Lu for this post:

    Buffmin (04-25-2012)

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Ahh. perfect! Thanks for the help (and tips). I forgot about using the echo.
    Buffmin

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    if you have more images of this sort, consider using a printf() function (which also doesn’t require you to break out of PHP).

    ex.
    PHP Code:
    // define an image template
    define("IMG"'<img src="images/%s/image%d.jpg">');

    // somewhere in your code
    printf(IMG$folder11);
     
    // somewhere else
    printf(IMG$folder33); 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    Buffmin (04-26-2012)

  • #5
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Good idea. Thank you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •