I have the following code and I would like to style it. Are there any ways to do this?
Specifically I want to center the Columns headings and the text within each cell.
echo "Variable Profile";
echo "<table>";
echo "<th>"."STATE"."</th>";
echo "<th>"."$column_name"."</th>";
foreach($lotsofasians as $lotsofasian){
echo "<tr>";
echo "<td>".$lotsofasian->state."</td>";
echo "<td>".$lotsofasian->$column_selected."</td>";
echo "</tr>";
}
echo "</table>";
I have tried putting something like align= 'center' in the tag but I cant get it to work.
With regard to styling HTML, there's nothing special about the fact that PHP is outputting it. You can still give your elements classes, IDs, inline styling or whatever - it's just that if PHP is involved you'll have to reference these in the echo output statements.
Just change the echo statement to include classes as required, e.g.
echo "<table class='some_class'>";
Try using css rather than using inline styles
css
table th,table td{
text-align:center;
}
See demo here
Your php is going to output something like this:
<table>
<th>state value</th>
<th>column value</th>
<tr>
<td>value</td>
<td>value</td>
</tr>
</table>
I would sneak a <tr> in there for your <th> elements as well:
<table>
<tr>
<th></th>
</tr>
Then, create some css rules:
table th, table td { padding: 5px; text-align: center; }
Use css-classes or inline styles.
Why you quote Variables? Your HTML is not valid.
simplest way:
echo 'Variable Profile';
printf('<table>
<thead>
<tr>
<th>%s</th>
<th>%s</th>
</tr>
</thead>
<tbody>', 'State', $column_name);
foreach($lotsofasians AS $index => $lotsofasian) {
$style = array();
/* Example 1: center text */
$style[] = 'text-align: center;';
/* Example 2: Set Background each second output */
if($index % 2 == 0) {
$style[] = 'background: #DDDDDD;';
}
printf('<tr style="%">
<td>%s</td>
<td>%s</td>
</tr>', implode('', $style), $lotsofasian->state, $lotsofasian->{$column_selected});
}
echo '</tbody>
</table>';
Related
Need some help.
I am trying to create a script that sends an html email to a shopper with a list of all the orderd items. I want the items fetched from the database to be stored in a single variable so that I can embed it in the message variable of mail() function. Or Just the Items fetched to be embeded in the email.
Have tried the code below but it only displays 1 item and not all that are in the loop. How can I do it?
I'll be grateful for your help.
$message3 = "
</td>
</tr>
<tr bgcolor='#A8CE58'><td><b>ITEMS ORDERED</b></td></tr>
<tr>
<td>
<table border='' style='margin-bottom: 10px; margin-top:10px; padding: 1px;border-right: : 1px solid #000;'>
<tr align='left'>
<th>---</th>
<th>Name</th>
<th>Qty</th>
<th>Total</th>
</tr>";
$ress= mysqli_query($server, "SELECT * FROM ordered_products WHERE session='$session'");
mysqli_data_seek($ress, 0);
while($colms=mysqli_fetch_array($ress, MYSQLI_NUM))
{
$message4 =
"
<tr align='left'>
<td><img src='e_images/$colms[8]' height='auto' width='50px'></td>
<td>$colms[2]</td>
<td >$colms[9]</td>
<td>ksh.$colms[5]</td>
</tr>";
}
$message = "$message3". $message4;
You need to concatenate the string
$message4 = '';
while($colms=mysqli_fetch_array($ress, MYSQLI_NUM)) {
$message4 .= //your html
}
What you doing now is overwrite the value of $message4 each time in the loop
With
.=
You solve this. Don't forget to declare the variable before the loop, else you will get a warning from PHP.
I'm filling an HTML table with data from the following query:
$content_downloads = mysql_query( "SELECT
s.site, s.machine, d.projectid, d.directorySize, d.connectivityError,
d.blocked, d.blockedBy, d.blockedSince, d.errorString, d.dashboardTimeStamp
FROM rtcdb.site_machines s
LEFT JOIN rtcdb.downloadstatuses d
on (s.site = d.site and s.machine = d.machine)
where s.site in (select d2.site from rtcdb.downloadstatuses d2
where d2.dashboardTimeStamp > (SELECT DATE_ADD(max(dashboardTimeStamp),INTERVAL -1 DAY)
from rtcdb.downloadstatuses)) order by s.site, s.machine asc", $con) or die(mysql_error());
This query is going into an HTML table based on the following HTML/PHP:
<table>
<thead>
<tr>
<th>Site</th>
<th>Machine</th>
<th>Project ID</th>
<th>Folder Size</th>
<th>Error Description</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_assoc($content_downloads)) {
$error_string = "";
if ($row["connectivityError"] == true ) {
$error_string = "COULD NOT CONNECT: " . $row["errorString"];
}
elseif ($row["blocked"] == true ) {
$error_string = "BLOCKED BY LOCK OWNED BY: " . $row["blockedBy"] . " " . $row["blockedSince"];
}
echo '
<tr>
<td>'.$row["site"].'</td>
<td>'.$row["machine"].'</td>
<td>'.$row["projectid"].'</td>
<td>'.$row["directorySize"].'</td>
<td style = "text-align: left; color: red;">'.$error_string.'</td>
</tr> ';
}
?>
</tbody>
</table>
The error description column in this table rarely has contents. As you can see, when it does, a PHP conditional function (the if and ifelse functions) places a statement before the error description to make the description more understandable. One thing you may notice is that I am coloring the text of error descriptions red. The thing that I am struggling with that I need your help on is to selectively style an entire row with a background-color of yellow, when the error description cell in that row has a value. In other words, when the two conditionals I wrote are true, I would like to make the background-color of the row that includes that error description yellow.
It seems pretty simple, but I have tried various options and can't seem to figure it out. Any tips Stack Overflow?
Check if the error string is empty:
echo '
<tr' . (empty($error_string) ? '' : ' style="background-color:yellow;"') . '>
<td>'.$row["site"].'</td>
<td>'.$row["machine"].'</td>
<td>'.$row["projectid"].'</td>
<td>'.$row["directorySize"].'</td>
<td style = "text-align: left; color: red;">'.$error_string.'</td>
</tr> ';
What I would try is setting a class on the row of interest:
<tr class="highLight">
and then use CSS to set the background color of the cells in that row:
tr.highLight td {background-color: yellow; }
Set the class attribute is your error string has content.
Try this:
if($error_string != "")
{
echo '<tr class="error-row"';
}else{
echo '<tr>';
}
echo '<td>'.$row["site"].'</td>
<td>'.$row["machine"].'</td>
<td>'.$row["projectid"].'</td>
<td>'.$row["directorySize"].'</td>
<td style = "text-align: left; color: red;">'.$error_string.'</td>
</tr> ';
and add this to your stylesheet:
.error-row{background:yellow;}
if you want less code you can use the if shorthand too like this:
echo '<tr' . ($error_string == "" ? '' : 'class="error-row"') . '>
<td>'.$row["site"].'</td>
<td>'.$row["machine"].'</td>
<td>'.$row["projectid"].'</td>
<td>'.$row["directorySize"].'</td>
<td style = "text-align: left; color: red;">'.$error_string.'</td>
</tr> ';
Ok, I figured out what to do, and I apologize for not using jQuery or class attributes. I appreciate everyones help, and am being honest when I say that everyone's comments and answers helped me figure it out. Here's the solution:
if ($error_string != ""){
echo '
<tr style = "background-color: yellow;">
<td>'.$row["site"].'</td>
<td>'.$row["machine"].'</td>
<td>'.$row["projectid"].'</td>
<td>'.$row["directorySize"].'</td>
<td style = "text-align: left; color: red;">'.$error_string.'</td>
</tr> ';
}
else {
echo '
<tr>
<td>'.$row["site"].'</td>
<td>'.$row["machine"].'</td>
<td>'.$row["projectid"].'</td>
<td>'.$row["directorySize"].'</td>
<td style = "text-align: left; color: red;">'.$error_string.'</td>
</tr> ';
}
Thanks for everyone's help!
Is there a way to check if a table cell has a specific content and to mark that cell with the symbol " ◤ " in the top left corner of that cell?
Thanks
use this find and rerplace function:
function ReplaceCellContent(find, replace)
{
$("#table tr td:contains('" + find + "')").html(replace);
}
You probably don't want to replace the content, just mark interesting cell with the "◤".
You can use CSS :before to do that
<table>
<tr>
<td>lorem</td>
<td>lorem</td>
</tr>
<tr>
<td>ipsum</td>
<td>lorem</td>
</tr>
</table>
JS:
$("td:contains('ipsum')").addClass('found');
Working example:
http://jsfiddle.net/3NWQD/1/
Example JSFiddle answer: http://jsfiddle.net/ATV4G/1/
Javascript:
function markCell(containingText, replaceWith) { //making our function
$("td:contains('"+containingText+"')").prepend("<span class='abs'>"+replaceWith+" </span>");
}
markCell("first", "◤"); //calling the function
CSS:
.abs {
position: absolute;
}
HTML:
<table>
<tr>
<td>first</td>
<td>second</td>
</tr>
</table>
These are the minimum requirements. If you want to beautify the output, you can write further CSS to achieve requirements. (Please check JSFiddle example)
Hope this helps you :)
try this
HTML
<table border="1" id="table">
<tr><th>No</th><th>Player Name</th></tr>
<tr><td>1</td><td>Sachin</td></tr>
<tr><td>2</td><td>Dhoni</td></tr>
<tr><td>3</td><td>Raina</td></tr>
<tr><td>4</td><td>Yuvi</td></tr>
<tr><td>5</td><td>Sachin</td></tr>
</table>
jQuery
function MarkCell(PlayerName)
{
$("td").each(function(){
//alert($(this).text());
if($(this).text() == PlayerName)
{
$(this).html('◤' + PlayerName);
}
});
}
MarkCell('Sachin');
live fiddle here
Here is a PHP solution.
Let's say you have a MySQL database you're connected to and the table has columns 'state_or_province', 'city', and 'country'. If you want to mark all cells with the state or province value of "GA", you would use something like this:
echo "<table>
<tr>
<th>State</th>
<th>City</th>
<th>Country</th>
</tr>";
while($row = mysql_fetch_array($myresults)){
echo "<tr>";
if($row["state_or_province"] == "GA"){
echo "<td class = 'red'>".$row["state_or_province"]."</td>";
}
else{
echo "<td>".$row["state_or_province"]."</td>";
}
echo "<td>".$row["city"]."</td>
<td>".$row["country"]."</td>
</tr>";
}
echo "</table>";
In your CSS style section, include the following
.red{
background-color: red;
}
In this example, all cells that have the value 'GA' under the 'State' column of our rendered table would appear in a red cell. Just edit the above CSS if you want to have the "◤" symbol appear on the top left hand corner instead
UPDATE
Change
if($row["state_or_province"] == "GA"){
echo "<td class = 'red'>".$row["state_or_province"]."</td>";
}
to
if($row["state_or_province"] == "GA"){
echo "<td class = 'red'>◤".$row["state_or_province"]."</td>";
}
and you'll see the triangles inside the text boxes
I'm trying to use if-statement to change <td>-s color.
Basically, I have a simple query to retrieve information from the database.
Additionaly, I have a column there which keeps the information like if the task is accomplished or not. When I retrieve the information I get all of them, but I need the accomplished tasks to be green, and others without any color.
I've searhed for the answer, but I couldn't find anything that satisfies me.
For example:
$qry = mysql_query("select * from table");
$recs = array();
while($row = mysql_fetch_array($qry))
$recs[]=$row;
mysql_free_result($qry);
I've tried to add while statement to the code above, but I was confused and it didnt work :(
I'm printing the results using heredoc:
How to give them color here?
<?php
$last_id=0;
foreach($recs as $rec)
{
$html=<<<HTML
<tr>
<td><b>Номер</b></td>
<td>$rec[0]</td>
</tr>
<tr>
<td><b>Номер документа</b></td>
<td>$rec[1]</td>
</tr>
<tr>
<td><b>Дата регистрации</b></td>
<td>$rec[8]</td>
</tr>
<tr>
<td><b>От кого</b></td>
<td>$rec[2]</td>
</tr>
<tr>
<td><b>По</b></td>
<td>$rec[4]</td>
</tr>
<tr>
<td><b>Краткое содержание</b></td>
<td>$rec[3]</td>
</tr>
<tr>
<td><b>Исполнитель</b></td>
<td>$rec[5]</td>
</tr>
<tr>
<td><b>Срок исполнения</b></td>
<td>$rec[6]</td>
</tr>
<tr>
<td><b>Срок исполнения продлен до</b></td>
<td><b>$rec[10]</b></td>
</tr>
<tr>
<td><b>Прислан</b></td>
<td>$rec[9]</td>
</tr>
<tr>
<td><b>Примечание</b></td>
<td>$rec[7]</td>
</tr>
<tr>
<td bgcolor="#838B83"> </td>
<td bgcolor="#838B83"> </td>
</tr>
HTML;
print $html;
if($rec[0]>$last_id)
$last_id=$rec[0];
};
$new_id=$last_id+1;
?>
rather than colour use a class, so you can change it in CSS
<td<?php if($row['complete']) echo ' class="complete"'; ?>>data</td>
<table>
<tr>
<td>column heading</td>
</tr>
<?php
$qry=mysql_query("select * from table");
while($row=mysql_fetch_array($qry)) {
if($row['urcolumnn']==1)
{
echo "<tr bgcolor=green>";
}
else
{
echo "<tr>";
}
?>
<td>
<?php echo $row['urcolumn']; ?>
</td>
</tr>
<?php } ?>
</table>
This is an example code. think this will help you. here i give the background color to <tr> like this if u want to give color to <td> use this <td style="background-color:green;">
I would suggest you to use ternary operator, rather than using IF statement, or your could use another workaround to use arrays to define colors, this would help you to define various colors for each status value globally, please find an example below:
$aryColor = array(
'complete' => 'green',
'incomplete' => "red",
'pending' => 'orange'
.....
);
//you can specify values for all of your status, or leave them blank for no color, the keys in the array are the possible values from your status field
foreach($recs as $rec) {
echo '<tr bgcolor="'.$aryColor[$rec['status_field']].'">
<td>'.$rec['title_field'].'</td>
</tr>';
}
I hope this helps you out, and you can easily edit this for HEREDOC.
first you should change your column values change its structure to int and set default value as "0".so when your task is complete the field value should be change to "1".
now come to your code:
$qry = mysql_query("select * from table");
while($row = mysql_fetch_assoc($qry)){
if($row['status']==1){
echo "<td color="green">Data</td>"
}
else{
echo "<td color="white">Data</td>";
}
}
Hence you can get the rows in green which status is==1 means complete and white for incomplete.
I know it's such a basic thing, but a Google search hasn't shown me how to re-sort the rows after clicking the th links.
I've got this:
<table border="1">
<tr>
<th>Type:</th>
<th>Description:</th>
<th>Recorded Date:</th>
<th>Added Date:</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['recorded_date'] ?></td>
<td><?php echo $row['added_date'] ?></td>
</tr>
<br />
<?php
}
mysql_close();
?>
</table>
I need to be able to click type and sort alphabetically, and click on either Recorded Date or Added Date and sort by date. I see that I need to have the MySQL queries do this, but do I set them up as conditionals with a href tags?
The easiest way to do this would be to put a link on your column headers, pointing to the same page. In the query string, put a variable so that you know what they clicked on, and then use ORDER BY in your SQL query to perform the ordering.
The HTML would look like this:
<th>Type:</th>
<th>Description:</th>
<th>Recorded Date:</th>
<th>Added Date:</th>
And in the php code, do something like this:
<?php
$sql = "SELECT * FROM MyTable";
if ($_GET['sort'] == 'type')
{
$sql .= " ORDER BY type";
}
elseif ($_GET['sort'] == 'desc')
{
$sql .= " ORDER BY Description";
}
elseif ($_GET['sort'] == 'recorded')
{
$sql .= " ORDER BY DateRecorded";
}
elseif($_GET['sort'] == 'added')
{
$sql .= " ORDER BY DateAdded";
}
$>
Notice that you shouldn't take the $_GET value directly and append it to your query. As some user could got to MyPage.php?sort=; DELETE FROM MyTable;
That's actually pretty easy, here's a possible approach:
<table>
<tr>
<th>
Type:
</th>
<th>
Description:
</th>
<th>
Recorded Date:
</th>
<th>
Added Date:
</th>
</tr>
</table>
<?php
$orderBy = array('type', 'description', 'recorded_date', 'added_date');
$order = 'type';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
}
$query = 'SELECT * FROM aTable ORDER BY '.$order;
// retrieve and show the data :)
?>
That'll do the trick! :)
A SIMPLE TABLE SORT PHP CODE:
(the simple table for several values processing and sorting, using this sortable.js script )
<html><head>
<script src="sorttable.js"></script>
<style>
tbody tr td {color:green;border-right:1px solid;width:200px;}
</style>
</head><body>
<?php
$First = array('a', 'b', 'c', 'd');
$Second = array('1', '2', '3', '4');
if (!empty($_POST['myFirstvalues']))
{ $First = explode("\r\n",$_POST['myFirstvalues']); $Second = explode("\r\n",$_POST['mySecondvalues']);}
?>
</br>Hi User. PUT your values</br></br>
<form action="" method="POST">
projectX</br>
<textarea cols="20" rows="20" name="myFirstvalues" style="width:200px;background:url(untitled.PNG);position:relative;top:19px;Float:left;">
<?php foreach($First as $vv) {echo $vv."\r\n";}?>
</textarea>
The due amount</br>
<textarea cols="20" rows="20" name="mySecondvalues" style="width:200px;background:url(untitled.PNG);Float:left;">
<?php foreach($Second as $vv) {echo $vv."\r\n";}?>
</textarea>
<input type="submit">
</form>
<table class="sortable" style="padding:100px 0 0 300px;">
<thead style="background-color:#999999; color:red; font-weight: bold; cursor: default; position:relative;">
<tr><th>ProjectX</th><th>Due amount</th></tr>
</thead>
<tbody>
<?php
foreach($First as $indx => $value) {
echo '<tr><td>'.$First[$indx].'</td><td>'.$Second[$indx].'</td></tr>';
}
?>
</tbody>
<tfoot><tr><td>TOTAL = <b>111111111</b></td><td>Still to spend = <b>5555555</b></td></tr></tfoot></br></br>
</table>
</body>
</html>
source: php sortable table
//this is a php file
<html>
<head>
<style>
a:link {color:green;}
a:visited {color:purple;}
A:active {color: red;}
A:hover {color: red;}
table
{
width:50%;
height:50%;
}
table,th,td
{
border:1px solid black;
}
th,td
{
text-align:center;
background-color:yellow;
}
th
{
background-color:green;
color:white;
}
</style>
<script type="text/javascript">
function working(str)
{
if (str=="")
{
document.getElementById("tump").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("tump").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getsort.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body bgcolor="pink">
<form method="post">
<select name="sortitems" onchange="working(this.value)">
<option value="">Select</option>
<option value="Id">Id</option>
<option value="Name">Name</option>
<option value="Email">Email</option>
<option value="Password">Password</option>
</select>
<?php
$connect=mysql_connect("localhost","root","");
$db=mysql_select_db("test1",$connect);
$sql=mysql_query("select * from mine");
echo "<center><br><br><br><br><table id='tump' border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>";
echo "<tr>";
while ($row=mysql_fetch_array($sql))
{?>
<td><?php echo "$row[Id]";?></td>
<td><?php echo "$row[Name]";?></td>
<td><?php echo "$row[Email]";?></td>
<td><?php echo "$row[Password]";?></td>
<?php echo "</tr>";
}
echo "</table></center>";?>
</form>
<br>
<div id="tump"></div>
</body>
</html>
------------------------------------------------------------------------
that is another php file
<html>
<body bgcolor="pink">
<head>
<style>
a:link {color:green;}
a:visited {color:purple;}
A:active {color: red;}
A:hover {color: red;}
table
{
width:50%;
height:50%;
}
table,th,td
{
border:1px solid black;
}
th,td
{
text-align:center;
background-color:yellow;
}
th
{
background-color:green;
color:white;
}
</style>
</head>
<?php
$q=$_GET['q'];
$connect=mysql_connect("localhost","root","");
$db=mysql_select_db("test1",$connect);
$sql=mysql_query("select * from mine order by $q");
echo "<table id='tump' border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>";
echo "<tr>";
while ($row=mysql_fetch_array($sql))
{?>
<td><?php echo "$row[Id]";?></td>
<td><?php echo "$row[Name]";?></td>
<td><?php echo "$row[Email]";?></td>
<td><?php echo "$row[Password]";?></td>
<?php echo "</tr>";
}
echo "</table>";?>
</body>
</html>
that will sort the table using ajax
This is the most simple solution that use:
// Use this as first line upon load of page
$sort = $_GET['s'];
// Then simply sort according to that variable
$sql="SELECT * FROM tracks ORDER BY $sort";
echo '<tr>';
echo '<td>Title<td>';
echo '<td>Album<td>';
echo '<td>Artist<td>';
echo '<td>Count<td>';
echo '</tr>';
It depends on nature of your data. The answer varies based on its size and data type. I saw a lot of SQL solutions based on ORDER BY. I would like to suggest javascript alternatives.
In all answers, I don't see anyone mentioning pagination problem for your future table. Let's make it easier for you. If your table doesn't have pagination, it's more likely that a javascript solution makes everything neat and clean for you on the client side. If you think this table will explode after you put data in it, you have to think about pagination as well. (you have to go to first page every time when you change the sorting column)
Another aspect is the data type. If you use SQL you have to be careful about the type of your data and what kind of sorting suites for it. For example, if in one of your VARCHAR columns you store integer numbers, the sorting will not take their integer value into account: instead of 1, 2, 11, 22 you will get 1, 11, 2, 22.
You can find jquery plugins or standalone javascript sortable tables on google. It worth mentioning that the <table> in HTML5 has sortable attribute, but apparently it's not implemented yet.