I'm trying to use a variable in a .php file to choose img src.
Here's my attempt:
<?php echo "<img src=\"$ICON\">"; ?>
or
<img src="<?php echo $ICON; ?>"/>
Neither have worked, is there anything obviously wrong? There's nothing displaying, not even the code, when I render the page.
Any guidance would be much appreciated, especially a correct implementation of the above. Sorry for poor code, only looked at these languages since yesterday.
Related
I am trying to use the get_option() function in Wordpress to set the SRC of an image in a theme file. The file is index.php, and represents the homepage.
The current image code is:
<img class="outside-collage-image" <?php echo 'src="'.get_option('article-image-1').'"'; ?>>
However, the image doesn't display, and when I inspect it, it simply says src(unknown) where it should have the proper SRC
I have tried a myriad of fixes, including leaving the SRC out of the php area and simply calling the function, but it doesn't seem to work.
The oddest part is, if I put on the page <p><?php echo 'src="'.get_option('article-image-1').'"'; ?></p> to check the output, it outputs into the <p> the proper code: src="http://image-site.com/my-image.jpg" (obviously the link to the image is fake, but the point is, it's a valid link.)
Any idea what could be causing this?
You can try this to get the image article-image-1.jpg in your root.
<img class="outside-collage-image" src="<?php echo get_site_url().'/article-image-1.jpg'; ?>">
It turns out that I screwed up, and the above function works just fine. I was simply applying it to the wrong image, and didn't realize that until looking at it a day later, when it wasn't 1:30 AM and I wasn't so tired. Thanks to all that answered.
headdesk
I am doing some sort of online storefront and each item has a corresponding image in the database. i need to echo the image of that specific item, how do i do it?
This is what i've done, But it doesn't seem to work:
<?php
$prebuy = "SELECT lot_image FROM lots WHERE lot_id= '$lot_id'";
$prebuyres = mysqli_query($mysqli, $prebuy) or die(mysqli_error($mysqli));
$lot_name = mysqli_fetch_assoc($prebuyres);
?>
<img src="C:\\xampp\htdocs\storefront\img\<?php echo ucwords($lot_name['lot_image']); ?>"
The image does not appear but there is no error either. What am i doing wrong? Please, help if you can.
Thanks in advance!
You missed enclosing img tag. try this way...and what about ucwords, to be sure first check result before using it.
echo ucwords($lot_name['lot_image']);
<img src="C:\\xampp\htdocs\storefront\img\<?=ucwords($lot_name['lot_image'])?>"/>
Edit:
set you image source relative to your php script in server like
<img src="img/<?=ucwords($lot_name['lot_image']);?>"/>
my image is saved in a file ....and i have saved its path in the database....when i am fetching the image path from the database and trying to display it on a given position on the web page it is simply not getting displayed instead a text "loading..." is appearing there
furthermore the inspect element is displaying the image path clearly, here it is
'<img id="image" alt="mgm axis" title="mgm axis" src="polybazaar_files/images/1.jpg" style="display: block;"></img>'
now part of my code is here
while( $result=mysqli_fetch_array($runquery))
{$GLOBALS['image']=$result['image'];}
<img src="<?php echo $image; ?>" title="<?php echo $prodname; ?>" alt="<?php echo $prodname; ?>" id="image" />
where on a different page echo'<img src="'.($result['image']).'" />'; worked for the same image on the database....
i tried the suggestion mentioned here Displaying an image using a php variable and also tried suggestion mentioned at other places but none of them work
also my page has lot of java script in between ......are they creating problem?
any valuable and informative reasons or solutions will be appreciated ....thanks
got it fixed .....
actually the address at the top of my script
<base href="http://polybazaar.in/shop/" />
needed to be put inside the comment, since i am working on a local server....or the address of a DEFAULT TARGET should be given if one wants its server request to be accessed from that URL.....
thanks all for their valuable comments
I have an page for viewing posters with the following code:
<div class="img"><img src="<?= $image; ?>" alt="" /></div>
but when I validate it at w3c it could not find the alt attribute.
maybe I have to escape the PHP element in someway?
How could I do that?
The error I get:
An img element must have an alt attribute, except under certain
conditions. For details, consult guidance on providing text
alternatives for images.
I fixed it. i had 2 times <ul id="grid"> on the same page. i still don't know why the error for the alt attribute appeared but when i edited the second id to grid2 the page was passed with 0 errors.
Thanks for the help anyway :)
I've never seen <?= before, so I would think your problem is that the PHP isn't being executed and the < and/or > is messing up the validator's parsing. If <?= is valid, or if you're validating by file upload or direct input, then I definitely think the problem is the arrows messing up the validator's parsing.
Also, just doing $image; won't output the value of the variable, you need to use echo as well.
I have a problem in my wordpress theme.
The thumbnail doesn't appear on every post.
I have a website with games and every game has a thumbnail (image) . But now the image doesn't appear. When I try to see the image I get this:
Invalid src mime type:
The problematic code is:
<img src="<?php bloginfo('template_url');?>/thumb.php?src=<?=$thumb;?>&w=183&h=140&zc=1" class="thumb" alt="<?php the_title(); ?>" />
What might be wrong?
Browsing to your site I saw what the issue is. If you look at your code, it's being generated like this:
<img src="http://jocuri2k.com/wp-content/themes/Games/thumb.php?src=<?=$thumb?>... ?>
It seems that your PHP parser isn't picking up the php in the tag. Try using this instead:
<img src="http://jocuri2k.com/wp-content/themes/Games/thumb.php?src=<?php=$thumb?>... ?>
It's possible your php configuration doesn't allow for "short-tags"
<?
code here
?>
but instead require the full php tags, which are:
<?php
code here
?>
You might be able to override this in your php.ini, but if you don't have access to that, simply use the full php tags and you should be good to go.
<?php echo $thumb; ?>