I'm trying to format a PHP value from my script with a CSS style so it look the same then my html text preceding it.
Here is my code:
<p>Your surname is: </p><?php echo "<div id='php'>" $surname; "</div>" ?>
what i'd like to get is
Your name is Remi
all formatted the same way, what I get now is just an error and my PHP page does not show at all.
You have to use . (a dot) to concatenate your strings:
<p>Your surname is: <?php echo "<span id='php'>" . $surname . "</span>"; ?></p>
edit: you should use a span tag, not a div, to enhance your php output.
Your error is that you have a space between your string literal and your variable.
There is no need to echo the div tags from PHP, so don't.
You also shouldn't echo out raw text into the page, convert it to HTML first.
<p>Your surname is: </p>
<div id='php'><?php echo htmlspecialchars($surname); ?></div>
<p>Your surname is: <span id="php"><?php echo $surname; ?></span></p>
You could also go for the shorter notation:
<p>Your surname is: </p>
<div id='php'><?= $surname ?></div>
The semi-colon ends the statement. So PHP thinks that "</div>" after the semicolon is another PHP command. That causes an error and prevents the page from appearing. Move the semicolon so it's just before the ?>, and it should work.
Someone correct me if I'm wrong: Using a might cause some layout issues. I don't believe that "Your surname is" and the $surname value will appear on the same line.
Related
I want a comment to appear under a cell, whenever the $comment value exists.
Therefore I came with the following code:
$comment = "Enabled"; // add comment, whenever this value is not set to "0"
<td>
<form action="go.php" method="POST" target="_blank">
<input type="submit" class="a" value="click"></form>
<?= !$comment ? "" : "<div class=\"design\">Comment: " . $comment;"</div>" ?>
</td>
But in the output, I get unwanted space after Enabled which looks like:
<div class="design">Comment: Enabled </div>
What causes this?
$comment = "Enabled"; // add comment, whenever this value is not set to "0"
<td>
^^^^^^^^^^^^ 12
See here:
Enabled </div>
^^^^^^^^^^^^ 12
It's caused by the 12 spaces up there (I counted them). Plus, since you did not show your actual code as to where you closed your ?> tag, that will also contribute to it.
Therefore and as an example:
<?php
$comment = "Enabled"; // add comment, whenever this value is not set to "0"
?><td>
However, the following is off. The semi-colon's in the wrong spot and is missing a dot to concatenate:
<?= !$comment ? "" : "<div class=\"design\">Comment: " . $comment . "</div>"; ?>
So, avoid any spaces after closing ?> tags or before <?php. PHP's adding spaces for it and look at your HTML source; it's just as good a developer tool as any.
remove semicolon ";" and replace with dot "." in
". $comment;"</div>" ?>
I am trying to display a PHP variable within a <p> tag but I either keep getting errors or the name of the variable is shown but not it's content.
Here is the code:
echo '<div class="item" style="background-color:'.$row[colour].';width:'.$row[width].'px;"><p>$row[name]</p></div>';
Where I have written:
<p>$row[name]</p>
will not show up in the browser correctly and instead will just show the name of the variable. If I surround the variable with '' I get an error regaurding syntax.
I have also tried to echo the variable:
<p><?php echo $row[name] ?></p>
But on the website nothing is displayed at all and when I look in the FireFox inspector I see this:
The code has automatically been commented out?
You need to add '. $row[name] . '
like this
echo '<div class="item" style="background-color:'.$row[colour].';width:'.$row[width].'px;"><p>' . $row[name]. '</p></div>';
You have to concatenate the php variable.
Replace
echo '<div class="item" style="background-color:'.$row[colour].
';width:'.$row[width].'px;"><a href="'.$row[link_url].
'" title="'.$row[name].'"><p>$row[name]</p></a></div>';
with
echo '<div class="item" style="background-color:'.$row[colour].
';width:'.$row[width].'px;"><a href="'.$row[link_url].
'" title="'.$row[name].'"><p>'.$row[name].'</p></a></div>';
Notice '"><p>'.$row[name].'</p></a></div>';
When interpolating array values in strings you need to use curly brackets and double quotes.
echo ...."\"><p>{$row['name']}</p></a></div>";
When you use single quotes, php does not interpolate variable names inside the string. Instead, it just prints the names, which is what you're seeing in the source code.
And your array keys should be strings, so ['name'] instead of [name]
Not sure if this is 100% as I am half asleep but at least it will help you with your array a bit.
echo "<div class=\"item\" style=\"background-color:".$row['colour'].";width:".$row['width']."px;\"><p>".$row['name']."</p></div>";
Here is my variable that I am actually getting from my MySQL Database:
<h1>This is a H1</h1>
<p>NOT BOLD</p>
<p> </p>
<p><strong>BOLD</strong></p>
I am using TinyMCE to format the code.
Here is how I echo it
<?php
// WHILE LOOP GETTING $ROW FROM MYSQL
$conContent = $row['content'];
Then, when I go to the page, it displays the output like this...
http://i.snag.gy/BbMqx.jpg
I want the variable to make the echo formatted. So like then it will have all the html formatting.
You can insert your variable inside the <strong> tags using the following method:
<?php
/* getting row from your table */
$conContent = $row['content'];
?>
<strong> <?php echo $conContent; ?> </strong>
Another solution is:
$conContent = $row['content'];
echo "<strong>" . $conContent . "</strong";
//or echo "<strong> $conContent </strong";
If the styles are to be applied to all the rows, then you could use a foreach loop:
foreach($row as $v) {
echo "<strong>$v</strong";
}
Note: This assumes that you've the mysql array result stored in a variable called $row.
It's not just for <strong tags. You can use <h1>, <p>, <div> -- it doesn't matter. PHP will output the variable content in the location you specify.
Hope this helps!
Can you check the HTML source of the output? Is the HTML still around? It looks like strip_tags() or HTMLPurifier removes your HTML. Otherwise you would either see the formatting applied or the tags in the output.
If you have HTML code in your database you don't have to do anything with it in PHP, but can directly print it.
I'm trying to populate a form with some data that contains special characters (e.g. single quote, double quote,<,>,?,","".~,,!##$%^&*()_+}{":?<<>,./;'[.] etc) :
<input type="text" name="message" size="200" maxlength="200"
value =<?php echo $message;?>>
However, $message, which comes from a MySQL table, isn't displayed correctly - any HTML output that should be in $message is broken.
How do I do this properly?
This will prevent your tags from being broken by the echo:
<?php echo htmlentities($message); ?>
If you want to display it
echo htmlspecialchars($messge, ENT_QUOTES, 'UTF-8');
That's what I usually do.
Since the answers are difference:
htmlentities-vs-htmlspecialchars is worth checking out.
I normally use the following code, see htmlspecialchars
<?php echo htmlspecialchars($videoId, ENT_QUOTES | ENT_HTML5); ?>
whats wrong with using a constant ?
<?php
define(foo,'<,>,?,","".~,,!##$%^&*()_+}{":?<<>,./;');
$foo2="'[.]";
echo constant('foo').$foo2;
?>
you need to put the '[.]' into a variable, as a constant will break on a ' (single quote).
I currently have the following code coming from a database table:
<h1 class="widgetHeader">My Friends</h1>
<div class="widgetRepeater">
<p class="widgetHeader">Random Selection</p>
<?php
$friends = $user->getFriends();
?>
<p class="widgetContent">
<?php
for ($i=0; $i<count($friends);$i++) {
$friend = $friends[$i];
?>
<span class="friendImage" style="text-align:center;">
<?php print $friend->username; ?>
</span>
<?php
}
?>
</p>
</div>
Now, ive tried using the eval function in php but i get a parse error unexpected '<'. I've also tried using the output buffer method (ob_start) without success too. Any ideas as to how i can get this code to evaluate without giving me an error?
note: the database code is stored in a variable called $row['code'].
The PHP eval function expects PHP code to execute as it's parameter, not HTML. Try enclosing your DB values with PHP close and open tags:
eval('?>' . $row['code'] . '<?php');
eval = evil!
Especially if the eval'd code comes from a db... one mysql injection = full php execution = full control.
Rather use some placeholders and replace them (like any other good templating system does).
You could store this in your database:
<h1 class="widgetHeader">My Friends</h1>
<div class="widgetRepeater">
<p class="widgetHeader">Random Selection</p>
{%friendstemplate%}
</div>
Then str_replace the placeholders with the content they should have. In your example i would also add a subtemplate per friend like this:
<span class="friendImage" style="text-align:center;">
{%username%}
</span>
... which you could loop and insert into {%friendstemplate%}.
You cant use eval on markup code. Either save the code to a temporary file so that you can include it, or rewrite the code so that it's not markup, something like:
print "<h1 class=\"widgetHeader\">My Friends</h1>";
print "<div class=\"widgetRepeater\">";
print "<p class=\"widgetHeader\">Random Selection</p>";
$friends = $user->getFriends();
print "<p class=\"widgetContent\">";
for ($i=0; $i<count($friends);$i++) {
$friend = $friends[$i];
print "<span class=\"friendImage\" style=\"text-align:center;\">";
print $friend->username;
print "</span>";
}
print "</p>";
print "</div>";