place html inside php code the right way and that works - php

i am trying to make html works and the right way to place it so it works placing html inside php code
<?php echo <td class=\"network\">".$show."</td>;?>

This does not give the impression that you've really tried much, or researched the issue.
<?php echo "<td class=\"network\">".$show."</td>";?>
^^ Should work to output HTML tags. (Though invalid as td should exist inside tables.)
Also note you can use different quotes to reduce the need for escaping. E.g.
<?php echo '<td class="network">'.$show.'</td>';?>

Related

PHP output in HTML file

Ay guys,
I do know two possibilites to display PHP in HTML:
<?php function(); ?> or the shorter method <?= function(); ?>
But I often see something like {METHOD} or {OUTPUT} in the HTML part of bigger scripts f.e.:
<div class="test">{OUTPUT}</div>
In my opinion this is a way tidier. Could somebody tell me more about this?
I have used php to generate html using the echo function and have used php inside html too.(If this clarifies your doubt in anyway)
echo $projectname; inside html file tags
echo the html file
Cheers :)
When echoing data in HTML without a template engine, short tags are preferred as they look cleaner, and are easier to read. They're great when using control structures too: http://php.net/manual/en/control-structures.alternative-syntax.php
For short tags to work short_open_tag needs to be enabled.
The example you shown with the curly brackets is usually specific to a template engine such as Twig.
you can use this method only with the function ECHO with double quotes :
1 - this works
$name = 'Mark';
echo "<div>GoodMorning {$name}</div>";
2 - this does not work
$name = 'Mark';
echo '<div>GoodMorning {$name}</div>';

Is that possible to have two classes in the same time in PHP?

Hi I am transferring html code to php code, but i get some problems.
For example in html I am using <ul div class="country city">...</ul>, it contains two classes (country & city),
But when I using PHP, I need to change it to echo "<ul div class=country city>...</ul>" since php does not accept "" in the middle. And as a result, the system only accept the first class(country) instead of two classes.
So could I ask how to change it,
Thanks :-)
use simple quote for example as HTML accept it as double quote
echo "<ul div class='country city'>...</ul>"
You can also escape your " so PHP can accept it:
echo "<ul div class=\"country city\">...</ul>"
You can swap the quotations to make them work. like this
echo "<ul div class='country city'>...</ul>"
If you want to introduce any variable in between, you can add this by
echo "<ul div class='country city".$variable."'>...</ul>"
This is called concoction

Getting PHP to write clean html

My PHP tends output html in really long, difficult to read html.
If my PHP is written as:
<?php
echo "<li>";
echo "<strong>Hello</strong>";
echo "</li>";
?>
it outputs HTML like this
<li><strong>Hello</strong></li>
which dosnt look that bad, but imagine if thats within a foreach loop which out putted variants of that, all on one line..
Is there a way to get my PHP to output as neatly composed HTML ?
There is: include the whitespace in your output (for example, add \n after each tag).
However, doing that is really an exercise in futility. If you want to view the HTML yourself, get an HTML pretty printer (or use the one included in your browser's developer tools). If it's meant for a browser, the browser doesn't care.
Use a template engine like SMARTY. This will allow you to keep all your html in completely different files than your PHP (it does compile as PHP). This will improve the readability of all of your code. You can then format the html any way you see fit.
You can use the \n to make a line break.
<?php
echo "<li>\n";
echo "<strong>Hello</strong>\n";
echo "</li>\n";
?>
But why use your time on it? Chrome details console will fix it if its because you use the html source as a debug tool.
Whether this is nice or not is subjective, but it works:
<?php
for ($i = 0; $i < 5; $i++)
{
?>
<li><strong>Hello</strong></li>
<?php
}
?>
What I'm trying to get at here is that you can go in and out of PHP mode, so if you have long strands of HTML, you can format them as such, instead of echoing everything.

Array contents not displaying well, when placed between certain lines of the code

Whenever I move the codeblock that generates courses with a grade of "F", it does not not echo out the courses that meets the criteria. But when I move it to beginning of the main div header of the script it displays well.. What could the problem be? I actually want it to be at the end of the script for the sake of printing out. I've also checked the source code and the parameters I was looking for wasn't there.
Pastebin of full code
echo "<table bgcolor = red >";
echo "<tr align= \"center\">";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<th>"."Failed Courses : "."</th>";
if($score_count !== 0){
while ($row8 = mysql_fetch_assoc($query8)) {
echo"<td>". $row8['course_code']."</td>;
}
}
echo "</tr>\n";
echo "</table>";
There are inconsistencies between your provided example here and the pastebin code you linked to. Which version are you using?
If it's the pastebin version you may be having issues with this line:
echo "<th>"."Failed Courses";
You are missing the </th> tag which could be messing with your output.
It seems like it is going to be a long bit of code so I'm posting it here, I don't intend for this to be an answer to your problem as I can't get it to do what you're saying.
PHP allows for information to be set outside of the PHP tags, and when that happens it is treated as regular HTML.
E.G.
<?php
If(2==2){
?><b>HELLO!</b><?php
}
?>
And
<?php
if(2==2){
echo "<b>HELLO!</b>";
}
?>
Will both result in <b>HELLO!</b> to be output to the screen. This is useful for things that might require a lot of HTML in more than one block, as well as HTML that may require extensive style definitions or other places where double quotes will be needed, for example
<div id="list_row[$i]" class="something something2 something3"> would have to be escaped as
echo <div id=\"list_row[$i]\" class=\"something something2 something3\"> whereas it could just be put more or less intact in php using the above mentioned fact. Now, there would still need to be an echo statement for the $i portion, as I don't think PHP processes the text, but I've never tried it so I can't be sure.
As for your HTML,
<th> tags are meant to be a header, aka in the top part of a table. <td> are meant to be table-data cells.
Seems like you're making a lot of unnecessary calls to the same MySQL tables, and I'm hoping I can condense it down a bit.
Why are you doing this?
echo "<th>"."Failed Courses : "."</th>";
do
echo "<th>"."Failed Courses : </th>";
and also before you do a while loop try to see what your results looks like.
$row8 = mysql_fetch_assoc($query8)
then print_r($row8);
post your results.
Also the table structure should be
<table>
<th></th>
<tr>
<td></td>
</tr>
</table>

Create an HTML Class in a PHP Document

I'm currently trying dynamically create an html table in PHP. Further, I want the first two lines to be a different color. The solution I see is to give them a class and then use css to color the lines.
if ($count<=2){
echo "<tr> class='color'";
}else
echo"<tr>";
//other code
echo"</tr>";
Problem is my code prints out the words "class='color'"
Does anyone know why this is happening? Thanks!
Of course. What is this
echo "<tr> class='color'";
instead of
echo "<tr class='color'>";
?
No offense intended, but try checking the plain HTML code before turning it into something dynamic.

Categories