How can I mix PHP and HTML code? - php

I have a while loop and I have a HTML code inside the while loop. Inside this HTML code there is PHP code reading a variable that depends on the condition of the while loop.
This variable should be evaluated by PHP. I wrote the code below to solve this but it doesn't work.
$row[0] depends on the condition of while. My code only outputs an empty space for this variable.
<?php
while ($row = mysql_fetch_row($stt)) {
$html='<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0 <span>likes</span>
</p>
<p>
_____________________
</p>
</div>';
echo $html;
}
?>

If you want to display something row by row, do this:
<?php
while($row = mysql_fetch_row($stt)){
$html="<div class='data'>" . nl2br($row[0]) . "\n<p>comment<a href=''>like</a><a href=''>share</a></p><p>0<span>likes</span></p><p></p></div>";
echo $html;
}
?>

> <?php echo nl2br($row[0]."\n"); ?>
this part should be outside of $html.
The interpreter on server side couldnt run.Accordin to php, this part is not a code block. It is just a text.

I found the answer of my question!!!
This is the solution:
<?php
while($row = mysql_fetch_row($stt)){ ?>
<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0
<span>likes</span>
</p>
<p>
_____________________
</p>
</div>
<?php
}
?>

Related

<br> does not work in my while

I come to you because I encounter a problem with my while loop.
I did a while loop to get the id of tickets and ticket titles.
Except that, I want to add a <br> but it does not work, it does not move.
I tried with two <br> <br> it's the same, can you help me please?
Here is my code :
<?php while ($row = $req_5->fetch()) { ?>
<?php echo $row['titre']."<br><br>"; ?>
<?php } ?>
<?php while ($row = $req_5->fetch()) { ?>
<?php echo $row['titre']?>
<br><br>
<?php } ?
just put the break tag outside of the anchor element

PHP syntax simplify

IN PHP I noticed that if we have code like below:
<?php if ( function('parameter')):?>
<?php //do something here ?>
<?php endif; ?>
why can't we write this code like:
<?php if ( function('parameter'))
//do something here
endif; ?>
I am new to PHP, Thanks a lot!!
The PHP code has to be inside <?php ?> and the HTML markup needs to be outside. You can also print out the HTML markup with echo.
Here is an example (much cleaner in my opinion, than example 2). The HTML markup is inside a PHP string. The return value of the_field(), a string, is then concated with .:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
echo '<p class="btn">Request a Quote</p>';
}
if(get_field('rfq_pdf_url')) {
echo '<p class="btn">Download PDF</p>';
}
?>
And here is another valid example (2). You can end the PHP part with ?> and output regular HTML markup and then start the PHP part again with <?php:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) { ?>
<p class="btn"><a href="
<?php the_field('quote_url'); ?>
">Request a Quote</a></p>
<?php }
if(get_field('rfq_pdf_url')) { ?>
<p class="btn"><a href="
<?php the_field('rfq_pdf_url');?>
">Download PDF</a></p>
<?php }
?>
It would however be redundant to start with <?php on every line and end it then again with ?>.
Another possibility would be:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
?>
<p class="btn"><a href='<?php echo the_field('quote_url'); ?>'>Request a Quote</a></p>
<?php
}
if(get_field('rfq_pdf_url')) {
?>
<p class="btn">Download PDF</p>
<?php
}
?>

HI~ I am wondering if there is a method to require a PHP file in an echo

i am wondering if there is a method to require another php file in an echo like this:
<?php
echo "
<label>Post Notifications</label>
<div>
<?php require_once('libraries/notifications/post_notifications.php'); ?>
</div>";
?>
I know the <?php require_once('libraries/notifications/post_notifications.php'); ?> isn't correct but just hope you can get the idea. Thank you!
Use ob_get_contents. Everyting between this tags is stored in variable end cleaned.
ob_start();
include('libraries/notifications/post_notifications.php');
$output = ob_get_contents();
ob_end_clean();
So this Looks output like:
<?php
echo "
<label>Post Notifications</label>
<div>
{$output}
</div>
";
?>

Show div only if mySQL info exists

I have a MySQL database storing some basic user info, so: name, email, and two phone numbers. I want to make a page with dynamic php text showing the users info, but in the database only one phone number is required. On the page I have:
<p id="first"> Some non-dynamic text </p>
<p> <?php echo $row_rsCustomer['first_name']; ?> </p>
<p> <?php echo $row_rsCustomer['email']; ?> </p>
<p> <?php echo $row_rsCustomer['phone_one']; ?> </p>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<p id="second"> Some non-dynamic text </p>
I dont want the #second to be pushed down if the #phonetwo is empty. I was thinking of using some javascript like this:
if( #phonetwo.innerhtml == ""){
#phonetwo.style.display="none";
But I was wondering if there's a way to do this using php? The javascript solution will work I suppose, but I'm pretty sure I've seen somewhere a more "correct" way to do this, I just don't remember what it was nor can I find it anywhere.
Just wrap the paragraph in a conditional
<?php if(isset($row_rsCustomer['phone_two']) && $row_rsCustomer['phone_two']):?>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<?php endif?>
<?php if (!empty($row_rsCustomer['phone_two'])) { ?>
<p id="phonetwo"><?php echo $row_rsCustomer['phone_two'] ?></p>
<?php } ?>
Just follow this pattern anytime you need to hide HTML from PHP:
<? if (your condition) { ?>
Html
<? } ?>
It is much better than doing it in JS since it does't send any data to the browser.
First check is it empty or not
<?php if(!empty($row_rsCustomer['phone_two'])) { ?>
<p id="phonetwo"> <?php echo $row_rsCustomer['phone_two']; ?> </p>
<?php } ?>
check if $row_rsCustomer['phone_two'] is not empty and then display your <p> :
<?php if (isset($row_rsCustomer['phone_two'])) { ?>
<p id="phonetwo"><?php echo $row_rsCustomer['phone_two'] ?></p>
<?php } ?>

PHP - Placing html in variable on out of php tag

I have question, how to placing my html content like this:
<?php
$html =
?>
//in this space, i will place my html
<span></span>
<?php
;
?>
// and i print it
<?php echo $html;?>
Why not just do that between the PHP tags?
<?php
$html = '<span></span>';
echo $html;
?>
You need output buffering for this.
<?php
ob_start();
?>
<!-- your html code here -->
<span></span>
<?php
$html = ob_get_clean();
?>
From what I understand you might want to have a look at heredoc syntax. However, your question is not exactly clear.
<?php
$html = <<<EOT
<span></span>
<!-- You can place anything in here "without escaping" -->
EOT;
echo $html;
?>

Categories