1

Pretty straight forward simple question, can you open a php code block to call image information in html? I don't think I phrased that right. Here is my code:

<img src="../inventory_images/' . <?php echo $item_number; ?> . '.jpg" width="150" height="150" border="2" />

This code is within the tags I'm just trying to post a photo using the $item_number variable (which is also the name of the image file i.e. $item_number = T3144 and the image file is name T3144.jpg ). Also if there is a better way to accomplish this suggestions are happily accepted. Sorry to take up bandwidth with such a remedial question but for some reason I can't seem to answer this question in research. Thanks for taking the time everyone.

7
  • <img src="../inventory_images/<?=$item_number;?>.jpg" /> I don't understand what the single quotes and . are for. Commented Mar 29, 2013 at 3:02
  • Yes, that should be fine. Did you try it? Commented Mar 29, 2013 at 3:02
  • BTW i have defined the $item_number variable properly (i've tested it separately and it worked when echoing) Commented Mar 29, 2013 at 3:03
  • @Leeish PHP short tags are EEEEEVIL... stay away. A lot of hosting servers don't have short tags enabled. The day you move your website to one of those hosts, you'll have php code spewing out all over in the HTML output. Commented Mar 29, 2013 at 3:05
  • regardless the ' and . was pointless i think. Why would people choose to disable such a usefull features Commented Mar 29, 2013 at 3:07

3 Answers 3

1

Your code is wrong, try:

<img src="../inventory_images/<?php echo $item_number;?>.jpg" width="150" height="150" border="2" />

with what you have it looks like the code you had would print

src="../inventory_images/' . whateveritem_numberis . '.jpg"

Sign up to request clarification or add additional context in comments.

Comments

0

Yes that is perfectly fine, but make sure that this code is in a file that ends with .php or it will not get parsed by PHP. Also, you need to take out the single quotes and periods:

<img src="../inventory_images/<?php echo $item_number; ?>.jpg" width="150" height="150" border="2" />

3 Comments

Huh ok I changed the code (I thought I had to concatenate because of the php tag) and the image source url is correct now when i view the page source but still getting no image (broken image in the corner). Any thoughts?
@MakD.Addy You can only use PHP's concatenate operator (.) inside <?php ?> tags. If it still doesn't work you might try adding an absolute URL instead of a relative one (e.g. src="http://yoursite.com/inventory_images/<?php echo $item_number; ?>.jpg" width="150"...)
Nice call. Like a dumb@ss I forgot to upload the inventory_images directory. Hey thanks everyone for the help. You guys are awesome!
0

Unless the above HTML is in an echo statement, you need to change it to this:

<img src="../inventory_images/<?php echo $item_number; ?>.jpg" width="150" height="150" border="2" />

That will in-turn look like this:

<img src="../inventory_images/T3144.jpg" width="150" height="150" border="2" />

Of course, that is going off of your example where $item_number = 'T3144';.

The single quotes and periods are used for concatenating variables inside of strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.