Correct PHP Syntax - php

I need to add this HTML tag:
<img src = "<?php echo esc_url( get_avatar_url( $post->post_author ) ); ?>" />
Inside PHP:
echo '<h3 class="nome-vendor-cat">',
$current_cat->name,
" di ",
'<a href="' . $vendor_link->get_shop_url() . '" class="nome-vendor">',
get_usermeta( $post->post_author, 'dokan_store_name' ),
'</span>',
'</h3>';
I have to insert that image in PHP, but it always generates syntax errors.

Based on:
<img src = "<?php echo esc_url(get_avatar_url($post->post_author));?>" />
Detect the PHP part:
esc_url(get_avatar_url($post->post_author))
Detect the HTML part:
<img src = "
//and
" />
Write it in PHP:
echo '<img src = "'.esc_url(get_avatar_url($post->post_author)).'" />';
And you should better use . for concating things.
Its ok to use , when you echo stuff.
But if you replace echo with an assignment $var = , you have to replace all , with . to get it work again.
So always use ..

Related

issue when i try to show image on php

i have only a small question on php also i want show an image on slider also i get error on data-thumb" " because the cod on it it's not good writen , can you check it please
echo '<img data-thumb="<img src="'.JURI::root().$image->file_url .'" />"
src="'.JURI::root().$image->file_url .'"
alt="'. $image->file_title .'" >';
also i get this result :
<img data-thumb="<img src=" https:="" website.com="" images=""
stories="" virtuemart="" product="" product-03.jpg"="">"
src="https://www.website.com/images/stories/virtuemart/product/product-03.jpg"
alt="VM Cart Logo " /> </div>
how can i fix it please ?
Your output isn't producing valid HTML. Try this.
echo '<img
src="'.JURI::root().$image->file_url.'"
alt="'.$image->file_title.'"
data-thumb="'.JURI::root().$image->file_url.'">';
Update
thanks also there is only a small issue that it must be an img on the
data-thumb to print like this result : ' src="sitecom.com/pic.png" alt="photo by
Barn Images"> how can i add my image source
src="'.JURI::root().$image->file_url.'" into the data-thumb like here
data-thumb='' ?
Can I ask why you want to embed an <img> tag in the data-thumb attribute? It can be done but usually, you'd just construct the <img> later when you actually use the contents of data-thumb. Anyhoo, I'll update my answer to match what you want.
This is valid HTML according to https://validator.w3.org/nu/.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>My Title</title>
</head>
<body>
<?php
$url = 'https://via.placeholder.com/350x150';
$title = 'This is my image title';
echo '<img
src="' . $url . '"
alt="' . $title . '"
data-thumb=\'<img src="' . $url . '">\'>';
?>
</body>
</html>
Using your variables, it would be.
$url = JURI::root().$image->file_url;
$title = $image->file_title;
echo '<img
src="' . $url . '"
alt="' . $title . '"
data-thumb=\'<img src="' . $url . '">\'>';

How to make a clickable image that carries a variable to another page in php?

My question is pretty simple. I tried doing some research into this but I never got it working with images.
I want to have a clickable image so when a user clicks on that image, it carries over some type of variable (ID) to another php page, so I can know which image that user clicked on and give the result.
My current code is this:
echo '<img src=' . $covers->src . 'height="300" width="190" value=' . $test . ' name="view" />';
I tried doing this on my second php page:
$var_value = $_GET['view];
echo $var_value;
but I get undefined error on line 1.
You try to fetch a parameter called view with $_GET. Simply add a parameter to your link called view and then its value.
echo '<img src="' . $covers->src . '" height="300" width="190" name="view" />';
In this example, using
echo $_GET['view'];
when at viewer.php will print "theValueIWantToBringAlong".
If your value is is stored in a variable, then do like this:
$myValue = 'theValueIWantToBringAlong';
echo '<img src="' . $covers->src . '" height="300" width="190" name="view" />';
Why not simply:
echo '<a href="viewer.php?view="' . $test . '">';
echo '<img src="' . $covers->src . '" height="300" width="190"/></a>';
Note also that you did not provide quotes around the src value of the image and
that you had no space before height.
echo "<a href=viewer.php?view={$test}><img src='{$covers->src}' height=300 width=190 name=view /></a>";
You want to put the values in the href of the <a> tag, not within the attributes of the <img>.
Change:
echo '<img src=' . $covers->src . 'height="300" width="190" value=' . $test . ' name="view" />';
to:
echo '<img src=' . $covers->src . 'height="300" width="190"/>';
An <a> isn't like a <form>. It can't pass values from elements inside of it to the link. The only way to do this with an <a> is to put the values within the href. The key/value pairs after the ? are then accessible in $_GET.
value and name are not valid attributes for img, see w3schools.com.

PHP variable with string

I'm trying to get an image using the following
$html ='<img alt="" src="'.bloginfo('stylesheet_directory').'/images/my-image.png">';
When I return $html the path from bloginfo('stylesheet_directory') shows in the top portion of the page instead in the src attribute of the image.
You should use use get_stylesheet_directory_uri() instead. And you are missing a quote (as CyberJunkie pointed out). Try this:
$img_path = get_stylesheet_directory_uri() . '/images/my-image.png';
$html = '<img alt="" src="' . $img_path . '" />';
There is also a sample in the wordpress codex:
<img src="<?php echo get_stylesheet_directory_uri() ?>/images/aternus.png" alt="" title="" width="" height="" />
bloginfo will automatically echo out the directory you are requesting, and has no return value. The output happens when you are building your string, not whenever you are echoing the $html variable.
I think the function you are looking for is get_stylesheet_directory_uri
Example usage of bloginfo (notice no echo):
<?php bloginfo('name'); ?>
Example usage of get_stylesheet_directory_uri:
<?php echo get_stylesheet_directory_uri(); ?>

Echo not displaying image correctly

Part of the code that's supposed to display an image:
$userav = getUserAvatar($_SESSION['login']);
$image1 = '<img src="$userav" alt="avatar.gif" width="80" height="80"/>';
echo '<span class="devpanellog">userav: '.$userav.'</span>';
?>
<div style="position:absolute; top:3px; left:3px; font-size: 12">
<?php
echo $image1;
?>
</div>
With $userav being userav: /wamp/www/graphics/avatars/defaultavatar.gif, the files exists there, there's nothing wrong with the image itself.
And the image that's being echo'ed:
http://prntscr.com/3ffb0j
I ran out of ideas, for now at least.
I have tried using /www/graphics/avatars and /graphics/avatars as the path to the dir but it didn't work either.
The script that's supposed to display the image is located in a different subfolder, /www/scripts/somescript.php while images are in /www/graphics/
After you noticed the pathetic fails I haven't noticed I fixed it to (also tried the other anwsers)
$image1 = '<img src="'.$userav.'" alt="avatar.gif" width="80" height="80"';
and it still doesn't display the image.
Fixed. The solution was wrong quotes, missing />(edit - it works even without /> lol) and wrong path.
Some random function($pathtoavatars = "/graphics/avatars/")
$userav = getUserAvatar($_SESSION['login']);
$image1 = '<img src='.$userav.' alt="avatar.gif" width="80" height="80"';
echo '<span class="devpanellog">userav: '.$userav.'</span>';
?>
<div style="position:absolute; top:3px; left:3px; font-size: 12">
<?php
echo $image1;
?>
</div>
your image variable is wrong try:
$image1 = '<img src="' . $userav . '" alt="avatar.gif" width="80" height="80" />';
Also your $userav variable is wrong - you'll just need the path from your webroot so assuming your web root is www you just need the /graphics/avatars/defaultavatar.gif part of it
Or you could try
$image1 = '<img src="' . str_replace($_SERVER['DOCUMENT_ROOT'], '', $userav) . '" alt="avatar.gif" width="80" height="80" />';
This will never work:
$image1 = '<img src="$userav" alt="avatar.gif" width="80" height="80"';
Do this instead:
$image1 = '<img src=" /graphics/avatars/'. $userav .'" alt="avatar.gif" width="80" height="80">';
Not sure but I think you should change the quotation marks from ' to " and vice versa. You didn't close the image tags and also, I think you don't input the image right.
Try this:
$userav = getUserAvatar($_SESSION['login']);
$image1 = "<img src='" . $userav . "' alt='avatar.gif' width='80' height='80'>";
echo "<span class='devpanellog'>userav: " . $userav . "</span>";
?>
<div style="position:absolute; top:3px; left:3px; font-size: 12">
<?php
echo $image1;
?>
</div>
You are you single quotes, Single quotes mean literal display (the $var instead of its value).
To remedy this, you do this:
$image1 = '<img src="'.$userav.'" alt="avatar.gif" width="80" height="80" />';
---------------^^ ---^^
You can see the syntax highlighting change. Also, you forgot to close the image-tag :)
A topic with a more detailed explanation:
What is the difference between single-quoted and double-quoted strings in PHP?
Some examples:
$var = "Hello";
echo 'Say $var World'; // screen will say [Say $var World]
echo "Say $var World"; // screen will say [Say Hello World]
echo 'Say '.$var.' World'; // screen will say [Say Hello World]
echo "Say ".$var." World"; // screen will say [Say Hello World]

Link to be displayed as imgsrc

I have a code here that outputs a image link like http://img.domain.com/2515.jpg
<?php echo IMG_URL . $code . ".jpg" ?>
But i want to make it print this entire thing <img src="http://img.domain.com/2515.jpg" alt="" title="Created by domain.com" />
How can i format that php string <?php echo IMG_URL . $code . ".jpg" ?>
to include that entire img src link?
I am trying to fit it in this html code below
<li><a class="linkInsert" data-value="<?php echo IMG_URL . $code . ".jpg" ?>">Direct Link (email & IM)</a></li>
Update:
i figured it out below with just using '
<li><a class="linkInsert" data-value='<img src="<?php echo IMG_URL . $code ?>.jpg" alt="">'>HTML Image (websites / blogs)</a></li>
Try this,
<?php echo '<img src="'.IMG_URL.$code.'.jpg" alt="" title="Created by domain.com" />' ;?>
You just have to use single quotes and double quotes alternatively.
PHP can be embedded inside HTML:
<img src="<?php echo IMG_URL . $code ?>.jpg" alt="">

Categories