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.
Related
Anyone know how should I write to get long hash value inside the single quote but when writing this code inside the php.
For example at the moment my code is below which I know its wrong:
$raw_tbody .= "<td width='10%'><a onclick='customRuleEdit('{$row['hash']}')' >";
So when I write like above its display on HTML side like below: (Which is wrong as you can see the single and double quotes)
<a onclick="customRuleEdit(" f6710ecaa686546bb424525686c792823f8d5193')'="">
What I want is as below Output:
<a onclick="customRuleEdit('f6710ecaa686546bb424525686c792823f8d5193')">
So anyone knows what I should write to make the output like above??
Thanks
Try this code for helpful..
$raw_tbody .= "<td width='10%'><a onclick=\"customRuleEdit('{$row['hash']}')\" >";
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>';?>
I'm a beginner PHP programmer, and I was wondering what was wrong with my code.
Here is the small excerpt from the affected spot:
echo "<form action='?tab=4' name='toedit5' method='get'><input value='text' onblur='edit('toedit5')' /></form>";
In Chrome's Developer Tools, the form element totally disappears, and the edit('toedit5') becomes edit(' toedit5').
The edit() function doesn't execute.
Is there anything wrong with this one line of code? Otherwise it is outside code messing with it. Sorry I didn't include it, but I don't know what to include. If you need more information, please tell me.
Thanks.
You need to escape your quotes inside your quoted echo'd statement, like this:
<?php
echo "<form action='?tab=4' name='toedit5' method='get'>";
echo "<input value='text' onblur='edit(\"toedit5\")' />"; // escaped..!
echo "</form>";
?>
It helped me to think about it like this when I was starting out: how does your browser know if the second single quote in onblur='edit('toedit5')' is closing your onblur statement or opening up the parameter? In this example, your browser will pair up the first 2 quotes it sees and assign that to the onblur attribute, i.e.: onblur='edit(' only!
Update 1:
Using the code above, I inspected a quick PHP page I created in Chrome's developer tools and was able to see the following (form available for inspection):
You really should use the more standard double quotes around the HTML properties and use single quotes around your string, with escaped single quotes within the javascript method calls. Like this:
echo '<form action="?tab=4" name="toedit5" method="get"><input value="text" onblur="edit(\'toedit5\')" /></form>';
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>
I'm working on a new CMS to use on repeative projects. Basically this chunk of code connects to a server, grabs all the names of the tables and uses them to generate a simple navigation. It works pretty great for what I need however I'm just wondering if maybe I can optimize this code sniplet even more and make it even more simple. Maybe making a class that could customize the formating? Etc. I tried to make this as "bare-bones" as possible.
The only thing that is there that I would like to explain is that it checks to see if the table name isn't "includes" this is a default table my CMS uses to know what data to display on the front end as far as data.
<?php
echo '<div class="dynamic_nav_head">Navigation</div>';
echo '<div class="dynamic_nav">';
include('data.php');
$tables = mysql_list_tables($database);
while (list($table) = mysql_fetch_row($tables)) {
if($table!='includes'){
echo "<div class='cat'>".ucwords($table)."</div>";
echo "<div class='cat_item'>";
echo "<a href='?page=read§ion=".$table."'>View " . ucwords($table) . "</a>";
echo "</div>";
echo "<div class='cat_item'>";
echo "<a href='?page=add§ion=".$table."'>Add New ". ucwords($table) ."</a>";
echo "</div>";
} // End If not in Includes.
} // End While
echo '</div>';
?>
Any Suggestions on how I can make this code even leaner, cleaner, and more swift? Thanks in advance!
Edit:
MySQL Version: 4.1.22
I suggest you to visit http://refactormycode.com/
echo '<div class="dynamic_nav_head">Navigation</div><div class="dynamic_nav">'; // on less echo
include('data.php');
$tables = mysql_list_tables($database);
while (list($table) = mysql_fetch_row($tables)) {
if($table!='includes'){
$ucTable= ucwords($table); // just one function call
// just one echo;
// you where also using quotes and double quotes backwards
echo '<div class="cat">'.$ucTable.'</div><div class="cat_item">View ' .$ucTable.'</div><div class="cat_item">Add New '. $ucTable .'</div>';
} // End If not in Includes.
} // End While
echo '</div>';
How do you know the code is slow? What does your profiler say about the code? Which statement is slowing it down? What platform are you on? What version of mysql? How many tables are in this catalog? Are you suffering from premature optimization?
The code is not bad. One improvement for readability also would be build an array from the database rather than having mysql_fetch_row within the 'while' construct. This would also allow you to filter out the unwanted names before entering the loop. You could also map the ucwords method on the array allowing you to take this out of the while loop construct. The point about the double quotes is a valid one but I would keep seperate echo statements for readability since this is not really going to make an appreciable difference here.
Hope that helps.