I want to echo the following in PHP:
echo "<td><p style='visibility: hidden' id='joindata$rowindex'>$joindata</p><a style='visibility: visible;' onclick='toggleDisplay('joindata$rowindex'); toggleDisplay('showjoindata$rowindex')' id='showjoindata$rowindex'>Show</a></td>";
But it is not echoing due to the nested ' ' Any Ideas how I can echo HTML elements which have events which call javascript functions which have parameters in PHP would be much appreciated, thanks :)
Just use double-quotes to delimit your attribute values, and escape them in your PHP string:
echo "<td><p style=\"visibility: hidden\" id=\"joindata$rowindex\">$joindata</p><a style=\"visibility: visible;\" onclick=\"toggleDisplay('joindata$rowindex'); toggleDisplay('showjoindata$rowindex')\" id=\"showjoindata$rowindex\">Show</a></td>";
Or, you could separate behaviour from content and just add the event handlers dynamically.
You have to add some double quotes, Try this:
echo "<td><p style=\"visibility: hidden\" id=\"joindata".$rowindex."\">$joindata</p><a style=\"visibility: visible;\" onclick=\"toggleDisplay('joindata".$rowindex."'); toggleDisplay('showjoindata".$rowindex."')\" id=\"showjoindata".$rowindex."\">Show</a></td>";
PHP have best performance and scalability when you separate the views from the data. However in this case you could do:
<td>
<p style="visibility: hidden" id="joindata<?php echo $rowindex; ?>">
<?php echo $joindata; ?>
</p>
<a style="visibility:visible" onclick="toggleDisplay('joindata<?php echo $rowindex; ?>'); toggleDisplay('showjoindata<?php echo $rowindex; ?>')" id="showjoindata<?php echo $rowindex; ?>">Show</a>
</td>
Also take a look on the jQuery docs to avoid hard-coding the javascript functions. Your app will run faster.
Related
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>';
I have php file index.php
In this file to use html code I am doing:
index.php
echo '
<div>
<a class="fragment" href="">
<div>';
In href I want to put value of some php variable i.e. $url How could be done?
is this correct way?
<a class="fragment" href="<?php echo $url; ?>">
You concatenate the string by ending it and starting it again:
echo '
<div>
<a class="fragment" href="' . $url . '">
<div>';
Though I personally prefer to stop the PHP tags and start them again (if I have a lot of HTML) as my IDE won't syntax highlight the HTML as it's a string:
?>
<div>
<a class="fragment" href="<?php echo $url; ?>">link</a>
</div>
<?php
Since you are printing several lines of HTML, I would suggest using a heredoc as such:
echo <<<HTML
<div>
<a class="fragment" href="$url">
<div>
HTML;
HTML can be anything as long as you use the same tag both in the beginning and the end. The end tag must however be on its own line without any spaces or tabs. With that said, specifically HTML also has the benefit that some editors (e.g. VIM) recognise it and apply HTML syntax colouring on the text instead of merely coluring it like a regular string.
If you want to use arrays or similar you can escape the variable with {} as such:
echo <<<HTML
<div>{$someArray[1]}</div>
HTML;
if you are echoing php directly into html i like to do this
<div><?=$variable?></div>
much shorter than writing the whole thing out (php echo blah blah)
if you are writing html in php directly then there several options
$var = '<div>'.$variable.'</div>'; // concatenate the string
$var = "<div>$variable</div>"; // let php parse it for you. requires double quotes
$var = "<div>{$variable}</div>"; // separate variable with curly braces, also requires double quotes
Do it like
<?php
$url='http://www.stackoverflow.com';
echo "<div><a class='fragment' href='$url' /></div>";
If you want to maintain the echo statement you can do either
echo '<a class="fragment" href="'.$url.'"><div>';
or
echo "<a class=\"fragment\" href=\"$url\">";
The first is better for performances and IMHO is more readable as well.
If you want to input/output large blocks of HTML with embedded variables you can simplify the process by using Heredocs:
echo <<<_EOI_
<div>
<a class="fragment" href="$url">
<div>
_EOI_;
You don't have to worry about escaping quotes, constant concatenation, or that ugly dropping in and out of <?php echo $var; ?> that people do.
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 echoing quotes through htmlentities($str) and it works the first time, but it's for caption's on images that are being swapped via jQuery when clicked- then the caption shows the html entity ".
How can I echo the text with quotes so that it still appears as a " instead of the $quot; after clicked?
Here's my html
<div class="smallImageWrapper">
<?php if(empty($row_rsPTN['img1'])): echo "" ?>
<?php else: ?>
<span class="smallImage"><img src="../images/products/pbetn/50x50/<?php echo $row_rsPTN['img1']; ?>" alt="<?php echo htmlentities($row_rsPTN['img1caption']); ?>" name="smallImage" id="smallImage1" height="50px" width="50px" /></span>
<?php endif; ?>
and here's my jQuery to swap the images:
$("#smallImage1").bind("click", function() {
$("#largeimage").attr("src","../images/products/pbetn/180x280/<?php echo $row_rsPTN['img1']; ?>");
$("#largeimage").attr("alt","<?php echo htmlentities($row_rsPTN['img1caption']); ?>");
$(".caption").text("<?php echo htmlentities($row_rsPTN['img1caption']); ?>");
});
here's a link to my test site where you can see it occurring:
http://www.imsmfg.com/new/test/products/ptn.php?menuproduct=Lacing%20Strips
Let me know if you need more info.
Thanks!
Change this line:
$("#largeimage").attr("alt","<?php echo htmlentities($row_rsPTN['img1caption']); ?>");
To this:
$("#largeimage").attr("alt","<?php echo addslashes($row_rsPTN['img1caption']); ?>");
jQuery entitizes things automatically, so you don't need to put entities in that quote. You just need to escape any quotes. Hence addslashes.
this is going to an easy one i think but for the life of me i cant get it to work.
i have a variables $id i want to put it in a img src that i have echo (eg. "uploaded/$id.jpg")
i have tried lots of way and looked all over the net and cant get it to work
echo "
<td>
<img src="uploaded'.$id'.jpg">
</td>
";
this is what the echo looks like if anyone can tell me why it is not work would be a big help
It's because you're mixing your double quotes with single quotes and you forgot the . concat operator after $id. Try this:
echo '
<td>
<img src="uploaded/'.$id.'.jpg">
</td>
';
No one stated the obvious:
Separate HTML and PHP, don't echo HTML:
<td>
<img src="uploaded/<?php echo $id; ?>.jpg">
</td>
You are mixing single and double quotes. Try this:
echo "<td>
<img src='uploaded/{$id}.jpg' />
</td>";
You're using double quotes inside a double quoted string.
echo "<td>
<img src='uploaded{$id}.jpg'>
</td>
";
Your mixing of quote styles is the problem. This should work.
or
echo "<td>
<img src=\"uploaded{$id}.jpg\">
</td>
";
if you're worried about valid html... 100 ways to skin a cat.
Try this:
echo "<td>
<img src=\"uploaded/{$id}.jpg\">
</td>";
EDIT: I think you were missing a / in the image path. Is "uploaded" a directory?
You were mixing single and double quotes. Double quotes allow you to embed variable in them like this... "Hello, $name" but if you tried "name$firstBlue", PHP would assume you were trying to insert the value of $firstBlue. You'd have to use "name{$first}Blue" for that to work. Always placing variables in {curly braces} is a good habit, because it prevents silly mistakes.
why it is not working.
echo "//double quotes begins here
<td>
<img src="/*it will end here later portion will not be printed*/ uploaded'.$id'.jpg">
</td>
";
you can use
echo '<td>
<img src="uploaded/'.$id.'.jpg">
</td>
';