add line break between 2 variables - php

I am echoing back these 2 variables in to a table, but wanted to know how to add a line break between these 2?
echo $row ['username'] . $row ['date_time'];

if you're printing an HTML output to your browser:
echo $row["username"]."<br />".$row["date_time"];
EDIT:
when printing to HTML - you better print the variables after passing them through htmlspecialchars function in order to avoid Cross-site-scripting (XSS), I'll do it this way:
echo htmlspecialchars($row["username"],ENT_QUOTES)."<br />".htmlspecialchars($row["date_time"],ENT_QUOTES);
if you want to print it to a file or something similar:
echo $row["username"]."\n".$row["date_time"];

you can always echo normal html from php.
echo "<div id=\"mydiv\"> Div content goes here </div>";
note the backslashes for double quotes wrapping mydiv to escape them.
so you can add a tag in between them to get a new line.
echo $row["username"] . "<br />" . $row["date_time"];

Related

How can i json_encode long html?

If I have a large html markup that gets populated with values from the database and gets echoed containig lots of divs that have classes:
echo "<div>";
echo"<div class='className'> {$_results['value']} </div>";
echo"</div>";
. . .
// large markup incoming
How can I save this in a variable so I can send it back as json ? is it possible to do that ?
This is what I am trying to do:
$html = "echo "<div>";
echo"<div class='className'> {$_results['value']} </div>";
echo"</div>";"
echo json_encode(array('html'=> $html, 'otherValue' => $_results['otehr']);
I just don't know how to save all the html in a variable so I can send it back in an array along with other values that need to be used separately.
Using echo means that you output strings. So, if you don't need to output all strings, then concatenate them into one and assign this final string to a variable, e.g.:
$html = "<div>"
. "<div class='className'>" . $_results['value'] . "</div>"
. "</div>";
echo json_encode(array('html'=> $html, 'otherValue' => $_results['otehr']));
A simple fiddle.
I will give you what I think is a great advice.
Use a template system for this, I will recommend you mustacheJS
It will be a little difficult the first time, but you will gain a better and clear code.

php json_array echo tweet text

I am attempting to echo a Tweet button in a table and have the tweet text from a json_array response.
The following code work unless the json response has a single quote(apostrophe) in it.
If the response has a single quote ( example I'm unhappy) this breaks the tweet text and stops after the I
is there away to strip all single quoyes from an array? or is there a better way to do what I am trying to accomplish?
echo "<table border='0'>";
echo "<tr><td><a href='https://twitter.com/share' class='twitter-share-button'{count} data-text='";
echo $json_array["reasons"][0]["author_name"], "says ", $json_array["reasons"][0]["content"];
echo " data-via='OnRecall' data-hashtags='OnRecall'>Tweet</a></td></tr>";
echo "<tr><td>Name: </td><td>", $json_array["reasons"][0]["author_name"];
echo " -> Liked: ", $json_array["reasons"][0]["like_count"], " times.</td></tr>";
echo "<tr><td width= '20'></td><td>", $json_array["reasons"][0]["content"];
echo "</td></tr></table><p><hr></hr></p>";
So if $json_array["reasons"][0]["content"] has a single quote in it, the tweet text stops at it.
Thank you for reading.
You can use PHP's htmlspecialchars function to accomplish this. With the ENT_QUOTES flag, this function will encode all quotes as HTML entities (' becomes ') and also significantly reduce your vulnerability to Cross-Site Scripting attacks -- this is especially important when the data you're displaying comes from an untrusted source (like someone else's Twitter feed):
$content = htmlspecialchars($json_array["reasons"][0]["author_name"], ENT_QUOTES)
. "says "
. htmlspecialchars($json_array["reasons"][0]["content"], ENT_QUOTES);
echo "<a ... data-text='$content'>';
You need to escape all the single quotes in your string. You can use str_replace() function to replace all ' to \', like this:
$content = str_replace("'","\'",$json_array["reasons"][0]["content"]);
// now echo $content
Here's the reference:
str_replace()

Syntax Error in concat echo statement and html code

Please help me to understand syntax of concat following
i want following code inside echo to understand concate
1--> <div class="alert echo $_REQUEST['error_id'];?>"\>
I try following code but getting syntax error that there is two echo..
2--> echo "<div class=\"alert" . echo $_REQUEST['error_id']; . "\">";
where mistake is that i cant get it... and i want two answer both using single quote and double quote
EDIT
Thank #Rizier123 now it working but css is not working as i have apply in calss="alert"
while using following code its working fine
<div class="alert echo $_REQUEST['error_id'];?>"\>
But after applying your code its not working..only text appear but background color is not comming nad boarder is also dissappear as below
class name is alert shows the status of login..
EDIT
Thanks again i just forget to put space after class name..
You don't need to write echo and ; again!
So this should work:
//echo with double quotes
echo "<div class=\"alert" . $_REQUEST['error_id'] . "\">";
//echo with single quotes
echo '<div class="alert' . $_REQUEST['error_id'] . '">';
Maybe you need to add a space if your class name includes spaces!(..."alert "...)
try this
echo '<div class=\"alert"' . $_REQUEST['error_id'] . '">';
You can do either 2 ways
1. PHP in HTML
<div class="alert<?php echo $_REQUEST['error_id'];?>">
2. HTML in PHP
echo "<div class='alert". $_REQUEST['error_id'] ."'>";
Or you can also do like
echo "<div class=\"alert". $_REQUEST['error_id'] ."\">";

PHP : echo a href

I have this php line which works fine:
echo "<p>" . $post['message']. "</p>";
But I want to change it so it will link to my page (not to a single post). So it should look like that.
echo "<p>" . $post['message']. "</p>";
I have tried a lot many proposition gathered on different website, but each time I am getting an error.
Any idea ?
Thanks a lot!
Using single and double quotes, you avoid escaping issues. Try this:
echo '<p>'. $post['message']. '</p>';
i see that you didn't escaped from double quote that closes href attribute:
echo "<p><a href=\"https://www.facebook.com/rscmovement\" target=\"_blank\">"
I guess You have missed the back slash () before " after www.facebook.com/rscmovement.
"https://www.facebook.com/rscmovement\" "\"target=\"_blank\">" will

$id is not going with the link

I am creating a hyperlink in foreach loop. It is working fine. When I am passing $id in URL parameter then it is not working. my link is showing http://****/test/index.php/test/view?id=**. i don't what i am doing wrong here.
foreach($list as $item)
{
$rs[]=$item['uname'];
$id=$item['uid'];
//var_dump($id); here it's printing $id value...
echo '<b> '.$item['uname'].'<br/>';
}
I want to pass $id value with hyperlink. Please suggest me.
It's of course getting printed -- your browser is just not displaying it to you since it's not being correctly parsed as HTML due to the extra " around the $id variable.
Set your header as follows:
header('Content-Type: text/plain');
and you'll see that it returns something like:
<b> FOOBAR<br/>
^ ? ^
As you can see, the issue is the extra double-quote before 55.
Change your code to:
echo '<b> <a href="/test/index.php/test/view?id=' . $id .'">'.
$item['uname'] . '</a><br/>';
Alternatively, you could also use double-quotes and enclose your variables inside {}, like so:
echo "<b> <a href=\"/test/index.php/test/view?id=$id\">{$item['uname']}
</a><br/>";
I'd use sprintf as it's cleaner.
echo sprintf('<b> %s<br/>', $id, $item['uname']);
You have another ".
Change this:
echo '<b> '.$item['uname'].'<br/>';
To this:
echo '<b> '.$item['uname'].'<br/>';
Try this:
echo "<b><a href='/test/index.php/test/view?id=$id'>$item</a></b><br/>";
This works!
And is the easiest and cleanest option. Inside of the double escaping, the simple is used for the html and through the double escaping all variables are written inside :) . Very simple.

Categories