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 :)
Related
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
I apologize for any misuse of terminology...I'm a noob...
I have a dynamically created page that contains a dynamic link. I added an IF/ELSE statement to display a different word based on the number of items in the variable $rowsphoto.
The different words display correctly, but the URL that is generated contains all of the PHP instead of generating the correct URL.
This is the original code, which works fine:
<?php if($portfolioid != 0) { ?>
<div class="extrafield">Additional works in Portfolio:</div>
This is the code I have after adding the IF/ELSE statement:
<?php if($portfolioid != 0) { ?>
<div class="extrafield">
<?php
if ($rowsphoto <= 4){
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid;?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid;?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Edition:</a>";
} else {
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid;?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid;?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Portfolio:</a>";
}
?>
</div>
I ran the code through a couple syntax checks and they all came back with no errors. What am I doing wrong? Is this even possible?
You'd be better off using it correctly like this:
<?php if($portfolioid != 0): ?>
<div class="extrafield">
<?php if($rowsphoto <= 4): ?>
Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid; ?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid; ?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Edition:</a>
<?php else: ?>
Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid; ?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid; ?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Portfolio:</a>
<?php endif; ?>
</div>
<?php endif; ?>
You're mixing html and php very badly. You should be separating them to keep your code clean and concise.
Your issue with it displaying the php instead of the correct variables is because (as #scrowler said):
You can't use PHP tags inside PHP, you just need to escape the string
bounds and use the . concatenation operator instead of trying to open
new PHP tags, e.g. echo "String here" . $varname; as opposed to echo
"String here"
While Darren's answer is correct, alternatively you can just stay inside of php
<?php
if($portfolioid != 0) {
echo '<div class="extrafield">';
if ($rowsphoto <= 4){
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=$artistid&pid=$portfolioid&album=$albumid&id=$photoidd&Itemid=105' class='portfoliocol'>Edition:</a>";
} else {
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=$artistid&pid=$portfolioid&album=$albumid&id=$photoidd&Itemid=105' class='portfoliocol'>Portfolio:</a>";
}
echo '</div>';
}
?>
Anything wrong with this code, it works great, but I don't understand the forth line. Why is closing bracket all by itself? I am a fairly new to PHP and always Google for answers, but I cant figure this one out. Hopefully, I can help others someday. Thanks
<div class="errorbox">
<?php if(isset($error2)){?>
<strong class="error"><?php echo $error2;?></strong>
<?php } ?>
</div>
Nothing is wrong with it. You can break in and out of PHP, and that is what this code is doing. Sometimes it's easier to break out of a PHP block to write some HTML, then go back into PHP
It's ending the if statement created on line 2, but line 3 is outputting HTML so php is ended, only to begin on the next line to finish the open statement.
Write it like this you suddenly know:
<div class="errorbox">
<?php
if(isset($error2)) {
echo '<strong class="error">' . $error2 . '</strong>';
}
?>
</div>
Or like so:
<div class="errorbox">
<?php
if(isset($error2)) {
?>
<strong class="error"><?php echo $error2;?></strong>
<?php
}
?>
</div>
This is normal PHP templating. It is ouputting HTML. First bracket is opening, second bracket is closing
There are several ways of doing it:
The best way is as descibed in the question:
<div class="errorbox">
<?php if(isset($error2)){?>
<strong class="error"><?php echo $error2;?></strong>
<?php } ?>
</div>
Another way is by echoing:
echo "<div class="errorbox">";
<?php
if(isset($error2)){
echo "<strong class="error">". $error ."</strong>";
}
?>
echo "</div>";
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 } ?>
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;
?>