This question already has answers here:
php - insert a variable in an echo string
(10 answers)
Closed 6 years ago.
I'm relatively new to php & was wondering if I can pass echo statement within variable. I have this in wordpress trying to retrieve profile picture using variable id to be able to integrate in wp comment section. Tried this.
$idd= author_details(get_the_ID());
$authordata = get_userdata( $idd );
$au=$authordata->display_name;
$authorAvatar = '<img src="http://www.example.com/photos/<?php echo getimg($au);?>" class="avatar user-1-avatar avatar-28 photo" width="40" height="40" alt="profile pic" >';
Here is the result image url I'm getting:
http://www.example.com/photos/%3C?php%20echo%20getimg($au);?%3E
It's apparent that I cannot pass echo statement within variable, but the sad thing is I'm unable to find solution for this simple stuff.
Note: The code works perfectly when they are put outside variable.
Voluntary help will be highly appreciated.
Thanks.
You don't need the PHP tags in the middle.
$var = getimg($au);
$authorAvatar = '<img src="http://www.example.com/photos/'.$var.'" class="avatar user-1-avatar avatar-28 photo" width="40" height="40" alt="profile pic" >';
Related
I need to store a hyperlinked image inside a variable, and then echo out that variable for my WordPress website. I find it more convenient to use bloginfo('template_directory') so that I can go back and forth between local and live server.
But bloginfo('template_directory') within a variable does not work, it displays nothing. But bloginfo('template_directory') will work in other areas of my website. What could be the problem?
CODE A works. Notice 127.0.0.1 in the URL
<?php
$var = '<a href="http://www.twitter.com">
<img src="http://127.0.0.1/wp-content/themes/twentyfourteen/images/social/twitter.png" />
</a>';
echo $var;
?>
CODE B displays nothing. Why is the below not working? Notice below I am using bloginfo('template_directory') instead of 127.0.0.1
<?php
$var = '<a href="http://www.twitter.com">
<img src="<?php bloginfo('template_directory'); ?>/images/social/twitter.png" />
</a>';
echo $var;
?>
EDIT
CODE C how to replace the word "twitter" with a variable? (I am referring to line 3 below, the word "twitter" that is located in img src)
<?php
$var = '<a href="http://www.website.com">
<img src="' . get_bloginfo("template_directory") . '/images/social/twitter.png" />
</a>';
echo $var;
?>
You need to use get_bloginfo for you want it in a var. Also no need for php tags when in a string.
http://codex.wordpress.org/Function_Reference/get_bloginfo
<?php
$var = '<img src="'. get_bloginfo('template_directory') .'/images/social/twitter.png" />';
echo $var;
?>
Also also suggest using something like this instead for child themes.
http://codex.wordpress.org/Function_Reference/get_template_directory_uri
<?php
$var = '<img class="svg " src="'. get_template_directory_uri() .'/images/social/twitter.png">'
echo $var;
?>
In response to your EDIT C code block, I'm guessing you are trying to loop through some social media icon links. This is how you might do that:
<?php
// Array of social sites
$sites = array(
'twitter',
'facebook',
'instagram'
);
// Loop through each
foreach($sites as $site){
$var = '<img src="'. get_template_directory_uri() .'/images/social/'. $site .'.png">';
echo $var;
}
?>
Don't use get_bloginfo( 'template_directory' ) and for that matter get_bloginfo( 'stylesheet_directory' ) which is used for child themes. These was used in early Wordpress versions. In later versions these was "replaced" with more appropriate functions.
The correct functions to use is get_template_directory_uri() for parent themes and get_stylesheet_directory_uri() for child themes. I would suggest that you work through these pages carefully
As for your syntax, as explained in the other answer, you cannot use HTML and PHP in the same string without separating them. This is done using the string operators which you can go and read up on in the link provided
It seems that you need to add social media icons. If so, check out these two posts. Specially read the one that #toscho done to my question. That should help you quite a lot with a few issues
listing repeating social media buttons. Take your time on this one. This is an excellent post that helped me out big time when I just started out
Social share buttons using genericons
You can't have PHP tags in PHP tags:
Just concatenate the strings:
<?php
$var = '<a href="http://www.twitter.com">
<img src="' . get_bloginfo("template_directory") . '/images/social/twitter.png" />
</a>'; //↑ ↑ See here
echo $var;
?>
Hey guys I have this code and I am trying to display an image if the src="" is empty.
I am using !empty to try to display my placeholder image as you can see here:
<img src="'.
(!empty( $cardata["PictureRefs"]))
? (explode(',', $cardata["PictureRefs"])[0])
: "db-content/theme/media/img/no-image.jpg" . '" data-src="'.
(explode(',', $cardata["PictureRefs"])[0]) .'" alt="'.$cardata["Variant"].'"/>
<img class="ms-thumb" src="'.
(!empty( $cardata["PictureRefs"]))
? (explode(',', $cardata["PictureRefs"])[0])
: "db-content/theme/media/img/no-image.jpg" .'" alt="'.$cardata["Variant"].'" />
For some reason this isn't working on my page, it just breaks the page.
Any idea where I might be going wrong? I am just left with the external url that the PDO is fetching from the DB displaying in text on the page.
CODE:
<?php $src = "db-content/theme/media/img/no-image.jpg";
if(!empty( $cardata["PictureRefs"])){
$src = explode(',', $cardata["PictureRefs"])[0];
}
$dataSrc = explode(',', $cardata["PictureRefs"])[0]; ?>
<?php echo '
<div class="ms-slide">
<img src="'.$src.'" data-src="'.$dataSrc.'" alt="'.$cardata["Variant"].'"/>
<img class="ms-thumb" src="'.(explode(',', $cardata["PictureRefs"])[0]).'" alt="'.$cardata["Variant"].'" />
</div>' ?>
It just displays the 404 for the image it was trying to receive in the first place.
Although I don't know what is going wrong, I refactored the code for the first image in order to make it easier to read and help others to understand:
$src = "db-content/theme/media/img/no-image.jpg";
if(!empty( $cardata["PictureRefs"])){
$src = explode(',', $cardata["PictureRefs"])[0];
}
$dataSrc = explode(',', $cardata["PictureRefs"])[0];
echo '<img src="'.$src.'" data-src="'.$dataSrc.'" alt="'.$cardata["Variant"].'"/>';
Are you using PHP 5.5 or later? Array dereferencing in this way (from the result of the explode function) requires PHP 5.5 or later.
It may be useful to view the source of the resulting page and post that.
This question already has answers here:
What is the "?" symbol in URL used for in php?
(7 answers)
Closed 9 years ago.
Sorry if this is a stupid question but I want to include two variables in an href
the href is not in a form
<div class="button">
View
</div>
int he page I am sending it to i have a Get method
if (isset($_GET['Name']) && isset($_GET['pid'])) {
$id2 = $_GET['Name'];
$id = $_GET['pid'];
Summary
I want to include $id in the a href so i will be able to get both name and id in the page product.php
You can append as many URL parameters as you like using &:
<a href="<?php fetchdir($ipages); ?>product.php?Name=<?php echo $name; ?>&id=<?php echo $id; ?>" ...
product.php?Name=$name&pid=$pid
You can use & to add two or more GET parameters to a URL.
One good way to build a property encoded is to use the http_build_query function, this will ensure that the data sent in the request is properly encoded.
<?php
$qstr = http_build_query(array("Name"=>$row["Name"], "id"=>$row["pid"]));
?>
<a href="<?php fetchdir($ipages) ?>product.php?<?= $qstr ?>">
This of course can also be done inline
<a href="<?php fetchdir($ipages) ?>product.php?<?= http_build_query(array("Name"=>$row["Namw"], "id"=>$row["pid"])) ?>">
This question already has answers here:
CodeIgniter img tag
(3 answers)
Closed 9 years ago.
I want to include an image alt attribute using PHP.
I currently have the following code...
<?php echo img(IMAGEPATH . 'logo.png'); ?>
How would I go about adding the alt attribute - "my awesome alt" - to this image using PHP so that when I go into view source I see something like this for the image?
<img src="http://examplesite.com/img/logo.png" alt="my awesome alt"/>
I am using codeigniter.
<?php echo img(array('src'=>'image/picture.jpg', 'alt'=> 'alt information')); ?>
Try like
<?php echo img(IMAGEPATH . 'logo.png',array('alt'=> 'alt information')); ?>
I am trying to create a code which obtains uploaded images, stores them and also displays the image preview and confirms that the image was successfully uploaded.
<?php
$name=$_FILES['myfile']['name'];
$tmp=$_FILES['myfile']['tmp_name'];
$error=$_FILES['myfile']['error'];
$path='myweb/';
if(move_uploaded_file($tmp,$path.$name)==1){echo 'success';}else{echo $error;};
echo ('<img src="$path.$name" height="100px" width="100px"/>');
<?php
The problem is that images are not displaying.
I have also tried
echo ('<img src="$path$name" height="100px" width="100px"/>');
but it still doesn't work.
How can I get the images to display?
problem was with single and double quotes.
echo '<img src="' . $path.$name . '" height="100px" width="100px"/>';
You have used single quote and because of that it was not taking the variable name.
You have to include the variable in the printed string like this:
echo ('<img src="'.$path.$name.'" height="100px" width="100px"/>');
You can read more about it in the documentation.
If you don't do so, PHP will think that you want to print the text $path.$name instead the variables content.
<img src="<?php echo $path,$name; ?>" height="100px" width="100px"/>
OR
echo '<img src="' . $path.$name . '" height="100px" width="100px"/>';
There is a difference between using double "" and single '' quotes.
Double quotes are getting parsed, which means that
$variable = 10;
echo "$variable";
will output:
10
Single quotes don't get parsed:
$variable = 10;
echo '$variable';
will output:
$variable
you use variables within '' which means they don't get parsed.
i replaced line 13 in above code like so.
echo (" <img src=$path.$name height=100px width=100px/>");
And its working now.
Seems like double quotes were the problem here.
I'l read m0re about it.
Thank u s0 much guys u all've been very helpful.:)