<?php if ( is_user_logged_in() ) {
echo '<img id="visit-the-forums" src="<?php bloginfo('template_url') ?>/images/visit-the-forums.png" alt="Check out the Forums!" />'
} else {
echo '<img id="join-the-forums" src="<?php bloginfo('template_url') ?>/images/join-the-forums.png" alt="Join the Forums!" />'
}
?>
I think there is something wrong w/ the way I set up the "php bloginfo" code inside but I'm not sure how to fix it.
The code below should work for you. You had to make use of string concatenation:
<?php if ( is_user_logged_in() ) {
echo '<img id="visit-the-forums" src="' . bloginfo('template_url'). '/images/visit-the-forums.png" alt="Check out the Forums!" />'
} else {
echo '<img id="join-the-forums" src="' . bloginfo('template_url') . '/images/join-the-forums.png" alt="Join the Forums!" />'
}
?>
You have 2 problems:
Most likely this code is not
executing because you're echoing a
string that's delimited with single
quotes and inside it you've put
unescaped single quotes. (You can tell this is the case because even on this page, the syntax colouring is messed up :)
Even if you had escaped the single
quotes (e.g. <?php
bloginfo(\'template_url\') ?>) this
would not work because you're using
PHP to echo PHP code, which will then
be passed to the browser, instead of
being executed by the PHP engine.
What you need to do is to add the result of bloginfo() (or get_bloginfo(), see edit below) to the string you're outputting:
echo '<img id="visit-the-forums" src="'. bloginfo('template_url') . '/images/visit-the-forums.png" alt="Check out the Forums!" />'
(note the correct usage of single-quotes as delimiters, and the correct syntax highlighting on this page: strings are reddish, code is black)
EDIT: if bloginfo here is the WordPress function, you will want to replace it in my code above with get_bloginfo which actually returns the result rather than printing it, but your original question wasn't clear about what bloginfo is/does.
Related
I am using php and conditional code to give a dynamic url to a photo. The result should read as http://example.com/biophotos/1.jpg. But instead I am getting
http://example.com/%22http://example.com/biophotos/1.jpg%22
How can I force it to just give the one url and without the %22 space on the end?
if ($emresult[0]['photo'] = "y") {
echo '<img class=\"alignright\" src=\"http://example.com/biophotos/' .
$theID . '.jpg" width=\"150\" height=\"150\">';
}
else {
echo 'There is no author photo.';
}
There is no reason in escaping double quotes when your using single quotes.
echo '<img class="alignright" src="http://example.com/biophotos/' . $theID . '.jpg" width="150" height="150" />';
As much as I agree with #slik, you also might want to look into %22 (double quotes) added to url out of nowhere
Check if magic quotes is on in your php.ini file. You can look into html_entity_encode() to encode the %22 as a slash.
Am echoing php variables which works fine but when i tried to output image, nothing seems to work
working.php
echo ("addMarker($lat, $lon,'<b>$name</b>$address<br><br>$desc');\n");
not_working.php
for image display, i added
<img src='http://localhost/services/status/" .$pic. "'>
hence
echo ("addMarker($lat, $lon,<img src='http://localhost/services/status/" .$pic. "'>,'<b>$name</b>$pic<br><br>$desc');\n");
Any Help
The php documentation about strings should clarify your issue, i hope. In simple words, variables are not expanded (parsed) in single quotes.
Best solution is to use sprintf:
sprintf('<img src="http://localhost/services/status/%s">', $pic);
OK solution:
echo '<img src="http://localhost/services/status/' . $pic . '">'
Not so ok solution:
echo "<img src=\"http://localhost/services/status/$pic\">"
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.:)
I am currently trying to create a shopping cart for my website and I have images of products stored in a database and I want to include them within <img src> . By putting $get_row[imagesrc] within the src. I need to know the correct way to add it to the below code as I dont fully understand the ' and . tags
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['imagesrc'].
'<br/>£'.number_format($get_row['price'],2).'Add</p>';
This should achieve what you're looking for:
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'" /><br/>£'.number_format($get_row['price'],2).'Add</p>';
The ' character defines a string literal when it is wrapped around a series of characters.
The . character is used for concatenating strings for output or storage.
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'"><br/>£'.number_format($get_row['price'],2).'Add</p>';
. concatenates two strings, and ' is wrapped around a string.
so
echo 'Hello '.'World'; // Shows Hello World
I'd split yours up to make it easier to read:
echo '<p>';
echo $get_row['name'].'<br/>';
echo $get_row['description'].'<br/>';
echo '<img src="'.$get_row['imagesrc'].'" /><br/>';
echo '£'.number_format($get_row['price'],2);
echo 'Add';
echo '</p>';
But it all looks OK.
echo '<p>'.$get_row['name'].'<br/>
<img src="'.$get_row['imagesrc'].'" alt="'.$get_row['name'].'"><br/>
<br/>£'.number_format($get_row['price'],2).'
Add</p>';`
echo '<img src="'.$get_row['imagesrc'].'">';
Try that.
A specific answer has been given:
echo '<img src="'.$get_row['imagesrc'].'">';
Nonetheless, it's worth adding that you should:
You should escape output - with htmlspecialchars() or otherwise.
echo '<img src="' . htmlspecialchars($get_row['imagesrc']) . '">';
Read the documentation on PHP Strings.
Check out this way of including PHP in your HTML. It's much easier to read and maintain. The last line in the paragraph is your image tag.
<p>
<?php echo $get_row['name']; ?><br/>
<?php echo $get_row['description']; ?><br/>
<?php echo $get_row['imagesrc']; ?><br/>
£<?php echo number_format($get_row['price'],2); ?>
Add
<img src="<?php echo $get_row['imagesrc']; ?>" />
</p>
Ok, it's Wordpress related and I know about Wordpress Stack Exchange, but I'm asking here because this is mostly a PHP question.
I want my code to display something or nothing using if statement.
The problem is I'm going to have a variable and bloginfo('template_directory') in-bulit function.
I wrote this:
<?php if (!empty($instance['example']))
echo "<li><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></li>"; ?>
It works fine until $instance['example'] is not empty, when is - it still displays the template directory link including images/example.png.
Any ideas?
I've tried " . bloginfo('template_directory') . " but doesn't seem to work.
PHP if statements that do not have braces { } will only evaluate the first line thereafter. To resolve this,
<?php if (!empty($instance['example'])) {
echo "<li><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></li>"; } ?>
Try that and see if it works for your needs. All I did was insert the braces so that your if statement spans all of your arguments.
You forgot to add the : after the if statement to make an if endif; block. Alternatively use the standard curly brackets to enclose all your commands in the if statement.
Currently it's only checking if for the first echo command.
Try this code:
<?php if (!empty($instance['example']))
echo "<li><img src=".get_bloginfo('template_directory')."/images/example.png /></li>";
?>
Try this
<?php if (!empty($instance['example'])) {
echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?>
<?php echo "/images/example.png /></a></li>";
}
?>
I'd personally use.
<?php
if( !empty( $instance['example'] ) )
echo '<li><img src="' . get_bloginfo('stylesheet_directory') . '/images/example.png" alt="" border="0" /></li>';
?>
First we add those missing attribute quotes.
Second we use the stylesheet path to ensure it points to the correct location for a child theme.
Third we call get_bloginfo to get a return value for the echo statement.
Fourth, i also added a border="0" to the image, borders aren't usually wanted for an image inside a link and also added an alt tag, because it will at least help pass HTML validation, even if you leave it empty.
Same answer as others, but with better formatted code:
<?php
if(!empty($instance['example']))
{
echo '<li><a href=' . $example . '><img src=' . bloginfo('template_directory') . '/images/example.png /></a></li>';
}
?>
Added brackets, removed unnecessary opening/closing php tags, and converted strings to single quotes since there are no variables or special characters contained within them that need processing.