simple Echo Error - php

i try to make a while{ part in php, to read out a mysql db. Works perfectly, only with the output i have problems:
echo "<div id=\"content\"><ul class=\"pageitem\"><li class=\"store\"><span class=\"image\" style=\"background-image: url('<?php echo \". $zeile['image'] . \"; ?>')\"></span><span class=\"name\"><?php echo \". $zeile['title'] . \"; ?></span><span class=\"arrow\"></span></li></ul>";
Error is:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\xampplite\htdocs\harti\products.php on line 40
Sry i'm really new with php, i think this is a simple mistake but cannot find it.
thx for help

Don't use <?php again...
echo "<div id=\"content\"><ul class=\"pageitem\"><li class=\"store\"><span class=\"image\" style=\"background-image: url('". $zeile['image'] . "')\"></span><span class=\"name\">". $zeile['title'] . "</span><span class=\"arrow\"></span></li></ul></div>";
Also, you forgot to close the DIV.
Instead, you could do something like this (outside <?php and ?>):
<div id="content">
<ul class="pageitem">
<li class="store">
<a href="index.html">
<span class="image" style="background-image: url('<?php echo $zeile['image']; ?>')"></span>
<span class="name"><?php echo $zeile['title']; ?></span>
<span class="arrow"></span>
</a>
</li>
</ul>
</div>

Read up on how strings work in PHP:
http://php.net/manual/en/language.types.string.php
If you use double quotes you can do this:
echo "<b>{$myarray[$index]}</b>";
Or you can do this:
echo 'blah';
Note how I used single quotes to avoid having to escape the double quotes. However, with a single quote, I cannot embed the variable within the quote.

Try this
echo "<div id='content'><ul class='pageitem'><li class='store'><a href='index.html'><span class='image' style='background-image: url(" . $zeile['image'] . ")'></span><span class='name'>" . $zeile['title'] . "</span><span class='arrow'></span></a></li></ul></div>";

Do not use
try this:
echo "<div id=\'content\'><ul class=\'pageitem\'><li class=\'store\'><a href=\'index.html\'><span class=\'image\' style=\'background-image: url(".$zeile['image'].")></span><span class=\'name\'>".$zeile['title']."</span><span class=arrow></span></a></li></ul></div>";
Check Single quotes also.
And even I agree with "Parkyprg", use html content outside php.

you may replace each " with "+"\""+".
It works in all scripting languages.

Related

PHP escaping qoutes

Just a simple line:
print "<li class=\"productImage\"style=\"background-image: url(\"images/products/1.jpg\");\">";
And the output is:
<li 1.jpg");"="" products="" images="" style="background-image: url(" class="productImage"><img src="images/products/2.jpg"></li>
What I am doing wrong?
Fixed:
print '<li class="productImage" style="background-image: url(\'images/products/1.jpg\');">';
Try with proper escape sequences, using same quotes are misjudged by browser (style in your case):
print "<li class='productImage' style='background-image: url(\"images/products/1.jpg\");'>";

Link PHP inside an echo tag

I am having a problem with this code
<?php
echo '<div class="post_note2">
<b>'.$lang['RENEW_SUCCESS'].'</b></div><br /><span class="orange"><b>HOME|VIEW AD</b></span>';
}
}?>
for some reason when the VIEW AD link is clicked it doesn't build it properly and still contains the php code in the link rather than the link to the actual ad page. is it an issue with an echo in an echo ?
I'm sure this isn't quite difficult to solve but I have been trying for far to long on my own and cant get it.
Thanks, any help would be great.
You actually had it right in the first part of your string. You can't have and echo statement inside of another echo statement. Use concatenation throughout your string:
<a href="' . $adurl . '"
You have two extra brackets at the end and php text inside your echo.
<?php
echo '
<div class="post_note2">
<b>'.$lang['RENEW_SUCCESS'].'</b>
</div>
<br />
<span class="orange">
<b>
HOME | VIEW AD
</b>
</span>';
?>
All fixed given that $adurl is defined.
This
<?php echo $adurl; ?>
Should be
' . $adurl . '
i.e.
echo '<div class="post_note2"><b>'.$lang['RENEW_SUCCESS'].'</b></div><br /><span class="orange"><b>HOME|<a href="'.$adurl.'>VIEW AD</a></b></span>';

Php echo " and '

Can someone help me with this code, I know what I'm doing wrong but I don't know how to repair it.
echo "<div onclick='showplayer('".$usersList["username"]."')' id='playerLookupNameResult' style='color:".$color.";'>Some text</div>
I need to use " after onclick= but it is already used after echo.
you can escape " like this \".
echo "<div onclick=\"'showplayer('".$usersList["username"]."')'\" id='playerLookupNameResult' style='color:".$color.";'>Some text</div>

PHP string concatenation problem?

function formatUpdate($tweet,$dt,$picture,$username)
{
if(is_string($dt)) $dt=strtotime($dt);
$tweet=htmlspecialchars(stripslashes($tweet));
$at = "#" . $username;
return'
<li>
<img class="avatar" src="images/' . $picture . '" width="48" height="48" alt="avatar" />
<div class="tweetTxt">
<strong>' . $username . '</strong> '. preg_replace('/((?:http|https|ftp):\/\/(?:[A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?[^\s\"\']+)/i','$1',$tweet).'
<div class="date">'.relativeTime($dt).'</div> <a class ="reply" href="?replyto=' echo $at; '">reply</a>
</div>
<div class="clear"></div>
</li>';
}
bolt is right. often concat issue has to do with a confusion of mixed in code, literals, and closing quotes/double-quotes. try to use heredoc instead to clean up your code-block.
for example, i would do the following to save my eyes staring at the code and to save my mind from insanity trying to find where the syntax error is (pseudo-coding only):
$at = "#$username";
$rt = relativeTime($dt);
$out = <<<raw
<div class="date">$rt</div>
<a class ="reply" href="?replyto=$at">reply</a>
raw;
just look at how much simpler it looks eh?
to learn about heredoc here's a reading reference.
ref: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
To append the value of a variable to a string you need not echo the variable.
You have
href="?replyto=' echo $at; '">reply</a>
Change it to
href="?replyto='. $at .'">reply</a>

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