displaying images in php/html - php

I'm trying to set the src of an image to a value in a database but it doesnt seem to be working.. here's the lines in question,
<?php
echo " $date<br/>"?>
<img src="<?phpecho $image;?>"/><br/>
<?php echo"$text
";
?>
and the error:
Parse error: syntax error, unexpected T_VARIABLE in /home/a6349675/public_html/Blog.php on line 54

Need a space between phpecho:
<img src="<?php echo $image;?>"/><br/>
Also remove the quotes from the last echo:
<?php echo $text; ?>
Lol, and the first echo as well.
echo $date; ?><\br>

You should find yourself a good editor, then it would of shown the syntax error in your code.
Also 99% of the time having tidy code will make it easier to spot errors.
<?php echo $date."<br/>"; ?>
<img src="<?php echo $image;?>"/><br/>
<?php echo $text; ?>
or simply
<?php
echo $date.'<br/>',
'<img src="'.$image.'"/><br/>',
$text;
?>

Related

PHP syntax breaking the image tag

I am having an issue with my live site that works fine on a dev site (on a different host).
Essentially, it's brining in the image incorrectly and adding characters at the end... example:
https://www.idealhomeloans.com/wp-content/uploads/2016/08/refinance-1.jpg/%3E%20%3Cimg%20src=
it should just be: https://www.idealhomeloans.com/wp-content/uploads/2016/08/refinance-1.jpg
This is the php code I'm using to bring the image in...
<?php if( $image ): ?>
<?php
echo '<img class="'. $imagealigned.'" src="';the_sub_field('image');
echo '/>';
?>
<?php endif; ?>
<?php $image = get_sub_field('image');
//Checking if anything exists for the image field
if ($image) { ?>
<?php echo '<img src="';// display a sub field value
the_sub_field('image');
echo '/>';
?>
<?php } //if there is nothing for image then display
else { ?>
<?php }
?>
Can someone who knows PHP well take a look and see if there's something that would render the image tag to come in correctly?
Any guidance would be greatly appreciated!
Thanks
Try to put values into variables.
<?php if ($image): ?>
<img src="<?= $src; ?>" class="<?= $class; ?>" />
<?php endif; ?>
It is easier to keep the formats and tags of the html, and you could become less confused. Plus I think it is much cleaner.
Perhaps if you actually LOOKED at the html you're generating, you'd see that you're generating BAD html:
echo '<img class="'. $imagealigned.'" src="';the_sub_field('image');
^---start HTML attribute
echo '/>';
^----never end the attribute
So you're building
<img .... src="kittens.jpg>
and around and around the error merrygoround you go...
You are using a semicolon (;) instead of a point (.) to concatenate the_sub_field

Php's echo does not close properly

Hello I have this src path in codeigniter
src="<?php echo base_url();?>photos/<?php echo $data1; echo '"/>';?>
The problem is that when i'm going to continue my code it seems that all the below code is inside an echo ""; it looks like the echo is still open.. For example i want to continue my code with a html divand thediv` is still on an echo area.. Thanks
I think you are inside of quotes of src-property.
src="<?php echo base_url();?>photos/<?php echo $data1;?>" />
try this
src="<?php echo base_url();?>photos/<?php echo $data1;?>" />
try this
<?php echo 'src="'.base_url().'photos/'.$data.'" />'; ?>
or
<?php echo '<img src="'.base_url().'photos/'.$data.'" />'; ?>
it seems like you haven't closed the last echo . tyr this
src="<?php echo base_url();?>photos/<?php echo $data1;?>" />
there is more better way of doing this in codeigniter. pass the url as the arguement to the base_url function
src="<?php echo base_url('photos/'.$data1);?>" />
try it. for more details: click

PHP echo inside echo

I'm trying to call an HTML/PHP content that it's inside my database using:
<?php echo $row_content['conteudo']; ?>
When the row is called the HTML appears correctly but the PHP doesn't.
I belieave it's cause of the echo inside the main echo.
<?php echo "
<h3>Hello</h3>
<?php do { ?>
<div class=\"indios\">
<a href=\"indio.php?id=<?php echo $row_indiosct['id']; ?>\">
<img src=\"galeria/indios/<?php echo $row_indiosct['foto']; ?>\" alt=\"<?php echo $row_indiosct['nome']; ?>\" />
<br /><?php echo $row_indiosct['nome']; ?></a></div>
<?php } while ($row_indiosct = mysql_fetch_assoc($indiosct)); ?> "
?>
The line one of this code is the same echo as the first code field, it's not repeating, it's there just for help and to understand where is the problem.
I already fixed some quotation marks but it gives an error in the line of the 1st echo.
That is some of the ugliest code I have ever seen...
<?php
echo '
<h3>Hello</h3>';
while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>
You could also use the HEREDOC syntax.
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOC instead.
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOT execute. PHP is not a "recursively executable" language. If you're outputting a string, any php code embedded in that string is not executed - it'll be treated as part of the output, e.g.
echo "<?php echo 'foo' ?>"
is NOT going to output just foo. You'll actually get as output
<?php echo 'foo' ?>
You have misunderstood how PHP works. PHP is processed by the server. When it encounters your script, it sees the following:
<?php echo "some long piece of text that you have told PHP not to look at" ?>
What is the reasoning behind trying to nest PHP calls inside strings?
evaluate code php in string using the function eval(): this post Execute PHP code in a string
<?php
$motto = 'Hello';
$str = '<h1>Welcome</h1><?php echo $motto?><br/>';
eval("?> $str <?php ");
http://codepad.org/ao2PPHN7
also if your need the code buffer output in a string also you can using the ob_start() method:
<?php ob_start(); ?>
<h3>Hello</h3>;
<?php
while ($row_indiosct = mysql_fetch_assoc($indiosct)){ ?>
<div class="indios">
<a href="indio.php?id='<?php echo $row_indiosct['id']'">
<img src="galeria/indios/'<?php echo $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
<?php } ?>

Error on if else statement

I am getting the below error -
Parse error: syntax error, unexpected $end.
This is driving me nuts! All Im trying to do is:
If there is a URL in the database to their website, hyperlink their name.
If not just echo their name. Simple stuff, what am I missing?
<?php
if ($row_clientsP['website']){
?>
<h2><? echo $row_clientsP['customerName']; ?></h2>
<?
}
else
{
?>
<h2><? echo $row_clientsP['customerName'];
}
?></h2>
You likely have an unclosed {.
The could be from elsewhere in the code or from using short tags <?, as noted by Mark Baker. You can check the value (short_open_tag)[http://www.php.net/manual/en/ini.core.php#ini.short-open-tag] in your php.ini. As a rule though, I discourage them.
If you don't have short tags <? enabled these lines will cause that error -
<?
}
AND
<h2><?
It is always best to use <?php tags, as most php installations turn off short tags by default.
see also - http://php.net/manual/en/ini.core.php#ini.short-open-tag
It's almost certainly an issue with the short open tags. They are "on" on my server and the script parses correctly. This also parses correctly and uses the full php tags.
<?php // RAY_temp_smitty.php
error_reporting(E_ALL);
if ($row_clientsP['website']){
?>
<h2><?php echo $row_clientsP['customerName']; ?></h2>
<?php
}
else
{
?>
<h2><?php echo $row_clientsP['customerName'];
}
?></h2>
Try to Use (full php tags)
<?php
if ($row_clientsP['website']){
?>
<h2><?php echo $row_clientsP['customerName']; ?></h2>
<?php
}
else
{
?>
<h2><?php echo $row_clientsP['customerName'];
}
?></h2>

use Img source with php variable?

i have the following code in my detailansicht.php:
<div class="file_description_box">
<b>Beschreibung:</b><br /><br />
<?php
if(!empty($beschreibung))
echo '<div align="center">';
echo '<img src="$coverlink" alt="Cover">';
echo '</div><br />';
echo format_content($beschreibung);
**else**
echo '<i>Keine Beschreibung vorhanden</i>';
?>
</div>
but i think there has to be a mistake in the img tag. everytime i trie to open the page, he shows me an error: "Parse error: syntax error, unexpected T_ELSE in bla/bla/include/detailansicht.php on line 123" (Line 123 is the else marked bold).
I tried several methods but i always get this error.
It would be nice, if someone could help me with this.
-sTarbuZz
You are missing some curly brackets and your PHP variable wasn't embedded property. Your code should look like this:
<div class="file_description_box">
<b>Beschreibung:</b><br /><br />
<?php
if(!empty($beschreibung)){
echo '<div align="center">';
echo '<img src="'.$coverlink.'" alt="Cover">';
echo '</div><br />';
echo format_content($beschreibung);
}else{
echo '<i>Keine Beschreibung vorhanden</i>';
}
?>
</div>
Just on a side note it really doesn't matter what you use PHP for, if there was a fault with the image tag and it wasn't being displayed properly it would generally be a HTML problem assuming you have outputted it correctly.
In this case it was a PHP problem because there were a few errors in the code, it hasn't really got anything to do with the image tag.
<?php
if(!empty($beschreibung)) {
echo "<div align=\"center\">";
echo "<img src=\"$coverlink\" alt=\"Cover\">";
echo "</div><br />";
echo format_content($beschreibung);
} else {
echo "<i>Keine Beschreibung vorhanden</i>";
}
?>
You are missing curly braces:
if () {
...
} else {
...
}
so your PHP is not syntactically correct and will not be parsed by PHP hypertext pre-processor.
Try using curly braces:
<?php
if(!empty($beschreibung)) {
echo '<div align="center">';
echo '<img src="$coverlink" alt="Cover">';
echo '</div><br />';
echo format_content($beschreibung);
} else {
echo '<i>Keine Beschreibung vorhanden</i>';
}
?>
use
echo '<img src="', $coverlink', " alt="Cover">';
PHP variables inside in single quote won't be evaluated
Yep, you're missing the curly braces. Simply formatting the code nicely with tabs won't make it a block.
Also you missed the ending of the img tag (/>), but that's unrelated to your question.
thanks for all your answers...
I'm trying Kris' method now...
And the thing with the brackets... i don't know how this could happen... i didn't write the original script (i just added the part with the cover) and i didn't recognize that there were missing brackets, because $beschreibung is never empty and i think php ignores the if and else if the brackets aare missing...
I would go for the alternative syntax which i feel is easier on the eyes intermixed with html:
<? if(!empty($beschreibung)) : ?>
<div align="center">
<img src="<?= $coverlink; ?>" alt="Cover">
</div><br />
<?= format_content($beschreibung); ?>
<? else : ?>
<i>Keine Beschreibung vorhanden</i>
<? endif ; ?>
PS: i am not advocating putting logic inside the markup, just noting it can be done.
edit: fixed syntax error (; after endif instead of :)

Categories