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!
Related
I want to make a table more readable by alternating row colours. I've been through the other answers but can't see how to apply them to my code. I tried various toggle methods in the last bit of the code but just broke everything.
I can't use jquery or CSS because I can only access this part of the code.
I want to toglle to bgcolor="#8BC6FD"
<table cellspacing=0 cellpadding=2 border="thin">
<col width=150>
<col width=100>
<tr bgcolor="#D4E9FC" style="outline: thin solid">
<td class="viewN"><b>TYPE:- </b></td>
<td class="viewN"><b>PACKING:- </b></td>
</tr>
<?php
$dbconn = pg_connect("host=127.0.0.1 XXXX password=YYYY)") or die('Could not connect: ' . pg_last_error());
$query = "SELECT * FROM stock
ORDER BY name" ;
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td>%s</td><td>%s</td></tr>", htmlspecialchars($myrow['type']),htmlspecialchars($myrow['packing']));
}
?>
</table>
I can't use jquery or CSS because I can only access this part of the code.
It's unclear as to which part of code you're referring. Assuming you can access all the code showed, you can place the <style> tag before or after the <table>.
It's as simple as defining n-th CSS like
<style>
/* tr:nth-child(odd) {background-color: #0000cc} */
tr:nth-child(even) {background-color: #D4E9FC}
</style>
I have a problem in inserting table from my database to div.
html code:
<div class="content">
<?php
$query = "SELECT name, surname FROM players";
$response = #mysqli_query($dbc, $query);
if($response) {
echo' <table align="left"
cellspacing="5" cellpadding="8" >
<tr><td align="left"><b>First Name</b></td>
<td align="left"><b>Last Name</b></td></tr>';
while($row = mysqli_fetch_array($response)) {
echo '<tr><td align="left">' .
$row['imie'] . '</td><td align="left">' .
$row['nazwisko'] . '</td><td align="left">';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query";
echo mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</div>
css code:
.content {
width: 1000px;
margin-left: auto;
margin-right: auto;
background-color: #ffffff;
color: #000000;
border-bottom: 14px solid #333333;}
Is there a way to insert this table to this div? Because when I'm loading this, the table is under my div'content'. The table should be in blank field 'a'.
Screenshot
Correctly indenting your code would have allowed you o spot the error relatively easily - there is an open td element. Either remove the last td from the echo statement in the loop or add additional td pair to first row in table so that they balance ( unless you use colspan attributes )
<style>
.content table tr td{ align:left;padding:5px;color:black;background:white; }
</style>
<div class='content'>
<?php
$sql = 'select `name`, `surname` from `players`';
$response = mysqli_query( $dbc, $sql );
if( $response ) {
echo "
<table align='left' cellspacing='5' cellpadding='8' >
<tr>
<td><b>First Name</b></td>
<td><b>Last Name</b></td>
</tr>";
while( $row = mysqli_fetch_array( $response ) ) {
echo "
<tr>
<td>{$row['imie']}</td>
<td>{$row['nazwisko']}</td>
</tr>";
}
echo "
</table>";
} else {
printf( 'Couldn\'t issue database query: %s', mysqli_error( $dbc ) );
}
mysqli_close( $dbc );
?>
</div>
There is an open <td> tag in your code you should close it before closing the <tr>
I want a zebra striped table.
The data is extracted from the database and every instance creates a new table.
I want the zebra-striped on the <table> level. this would mean that every other <table> element gets a different color.
I tried to add a class to my <table class="oddeven"> but this still does the changing on every tr.
Here is the code I use it on:
<?php
global $wpdb;
$sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'group' AND meta_value='$group'";
$results = $wpdb->get_results($sql) or die(mysql_error());
echo'<table cellpadding="0" cellspacing="0" border="0" width="100%">';
foreach( $results as $result )
{
$post_id = $result->post_id;
$company_name = get_post_meta($post_id, 'company_name', true);
$address = get_post_meta($post_id, 'address', true);
$postal_code = get_post_meta($post_id, 'postal_code', true);
$city = get_post_meta($post_id, 'city', true);
$phone = get_post_meta($post_id, 'phone', true);
echo '
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="oddeven">
<tr>
<td width="75%">
<strong>'.$company_name.'</strong>
</td>
<td rowspan="4"><img class="table_image" src="'.get_bloginfo('template_directory').'/images/arrow.png"></td>
</tr>
<tr>
<td>
'.$address.'
</td>
</tr>
<tr>
<td>
'.$postal_code.' '.$city.'
</td>
</tr>
<tr>
<td>
'.$phone.'
</td>
</tr>
</table>
</td>
</tr>';
}
echo '</table>';
?>
This is the styling:
(while typing this I realise that this is on tr:level)
.oddeven tr:nth-child(odd){
background: #ffffff;
}
.oddeven tr:nth-child(even){
background: #000000;
}
I hope I make myself clear
If you change your foreach loop to a for loop like this:
for($i = 0; $i < count($results); $i++) {
$result = $results[$i];
...
Then you can figure out if the current row is even or odd by testing if $i % 2 == 0. If that evaluates to true, then add an 'even' class; else add an 'odd' class.
If you do not want to propagate the change to child tables, you can also enforce the same color on the children :
.top tr:nth-child(odd) {
background-color: red;
}
.top tr:nth-child(odd) tr {
background-color: red;
}
.top tr:nth-child(even) {
background-color: yellow;
}
.top tr:nth-child(even) tr {
background-color: yellow;
}
I assume that's what your want since you already have stripped rows on your nested tables
Or maybe I got it wrong, you might have to explain what the <table> level is, since your have nested tables.
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>';
Problem:
To style the last row in a SQL query without using any CSS3 selectors.
PHP code:
while ($item = mysql_fetch_assoc($itemsresult))
{
$answer = "SELECT IID, Comment FROM betyg_answers WHERE EID = '{$EID}' AND CID = '{$item['CID']}' ORDER BY ID ASC";
$answerresult = mysql_query($answer) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
$answer = mysql_fetch_assoc($answerresult);
if ($answer['IID'] == $item['IID'])
{
$html .= '
<tr>
<td class="arrow" style="'.$arrow.'"><img src="./img/circle_arrow_right.png" class="arrowimage"></td>
<td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$text.'">'.$item['Description'].'</td>
</tr>
';
}
else
{
$html .= '
<tr>
<td class="arrow" style="'.$arrow.'"> </td>
<td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$text.'">'.$item['Description'].'</td>
</tr>
';
}
$itemcounter++;
}
Last row in SQL query should instead print:
$html .= '
<tr>
<td class="arrow" style="'.$lastarrow.'"> </td>
<td class="numbers" style="'.$lastnumbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$lasttext.'">'.$item['Description'].'</td>
</tr>
';
Question:
What needs to be added in the while loop so it recognizes the last row and print a different code instead?
Use a counter:
$i = 0;
$c = mysql_num_rows($itemresult);
while ($item = mysql_fetch_assoc($itemsresult)) {
$i++;
if ($i == $c) {
// This is a last row
} else {
// Regular row
}
}
there is several ways to do that :
use :last-child ( but this isn't working in IE8)
table tr:last-child {
background-color:#ff0000;
}
using jQuery method ( this is browser independent)
in document load function, add following jQuery code,
$("td:last-child").css({background-color:"#ff0000"})