-
November 27th, 2014, 08:09 AM
#1
MySQL table rows colors
here it is my code to print headers with rows from MySQL table:
PHP Code:
while($row = mysql_fetch_row($result))
{
echo "<tr class='tr4'>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
I would like to ask how could i change header "Baigtis" cell to green (if WIN), red (if LOSE), gray (if DRAW).
-
November 27th, 2014, 08:11 AM
#2
Re: MySQL table rows colors
Full Script:
PHP Code:
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';
$database = 'test';
$table = 'progs';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<table cellpadding='2' align='center' width='100%'><tr class='tr3'>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr class='tr4'>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>
-
November 29th, 2014, 12:19 PM
#3
Re: MySQL table rows colors
Since you're using a foreach loop, just calculate the last $cell and then compare the string output of WIN or LOSE.
PHP Code:
foreach($row as $k => $cell) { $bg = '#ffffff'; if ($k == count($row) - 1) { // only alter if last cell in row $bg = ($cell == 'WIN') ? '#00ff00' : '#ff0000'; } echo '<td style="background: ' . $bg . ';">' . $cell . '</td>'; }
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|