Page 1 of 1
Off topic; Table coloring question
Posted: Sat Apr 20, 2002 3:14 pm
by phice
I was wondering...
Let say you've got a normal MySQL database query'd, and you've got the information back, and set it up in a table, showing 10 results. I want it set up, to where Every other row is one color, and the other row is another. How would I go about doing this?
An example of this would be on
http://www.evilwalrus.com/viewrecent.php You can see each other row is gray, and the other is whitish...
Posted: Sat Apr 20, 2002 3:30 pm
by abionifade
Posted: Sat Apr 20, 2002 6:28 pm
by EvilWalrus
that code is just too much overhead, use the following (it's used on EvilWalrus.com):
Code: Select all
<?php
print "<table width="95%" align="center">
";
for ($x=0; $x<count($row); $x++)
{
$class = $class == "background: #eeeeee;" ? "background: #dddddd;" : "background: #eeeeee;";
print "<tr><td style="$class"><font size="1" face="Verdana, Arial, sans, sans-serif" ><b><a href="viewcode.php?codeEx=" . $rowї$x]ї'ID'] . "">" . $rowї$x]ї'Task'] . "</a></b> - (Submitted: <b style="color:red">" . $rowї$x]ї'Date'] . "</b>)</font></td></tr>
";
}
print "</table>
";
?>
Now, change the $class values to reflect the colors you want to alternate from/to. This is a simpleer one-line approach to all that bulky modulus code.
Hope it helps....
Posted: Sat Apr 20, 2002 10:46 pm
by jason
Also, if you are doing Record Set type programming, you can use the Table Class I have (
http://www.newbienetwork.net/phpcodems_code.php?id=50 ) which might help out.
It has a method (defined) below called Set2RowColors() that does this automagically for you.
Code: Select all
/**
* Set alternating colors.
*
* You simply put in two HTML compatible colors, like #ffffff, or 'white' and
* it will creat alternating color rows.
*
* @access public
* @param string $odd_colors The odd numbered rows bgcolor value
* @param string $even_colors The even numbered rows bgcolor value
* @param int $start What row to start outputting the alternating colors on. Defaults to 1 (the first row).
* @param int $end What row to stop outputting the alternating colors on. Defaults to the GetCurrentRow() value
*
*/
function Set2RowColors( $odd_colors, $even_colors, $start=1, $end=false )
{
if( $end === false )
{
$end = $this->GetCurrentRow();
}
for( $row = $start; $row <= $end; $row++ )
{
if ( ( $row % 2 ) != 0 )
{
$this->fstylesї"row"]ї$row]ї"bgcolor"] = $odd_colors;
} else {
$this->fstylesї"row"]ї$row]ї"bgcolor"] = $even_colors;
}
}
}
Posted: Sat Apr 20, 2002 11:39 pm
by EvilWalrus
too much overhead, IMHO.
Posted: Sat Apr 20, 2002 11:43 pm
by jason
For creating alternating color rows, maybe.
But for creating datagrid like work, definetly not. What you call overhead, I call easier to use and maintain, especially when realistically speaking, overhead isn't a concern.
Posted: Sun Apr 21, 2002 9:00 pm
by phice
EvilWalrus wrote:that code is just too much overhead, use the following (it's used on EvilWalrus.com):
Code: Select all
<?php
print "<table width="95%" align="center">
";
for ($x=0; $x<count($row); $x++)
{
$class = $class == "background: #eeeeee;" ? "background: #dddddd;" : "background: #eeeeee;";
print "<tr><td style="$class"><font size="1" face="Verdana, Arial, sans, sans-serif" ><b><a href="viewcode.php?codeEx=" . $rowї$x]ї'ID'] . "">" . $rowї$x]ї'Task'] . "</a></b> - (Submitted: <b style="color:red">" . $rowї$x]ї'Date'] . "</b>)</font></td></tr>
";
}
print "</table>
";
?>
Now, change the $class values to reflect the colors you want to alternate from/to. This is a simpleer one-line approach to all that bulky modulus code.
Hope it helps....
Could you show me what you've got the $row? Because, I've got everything else to work, except for $row.
Posted: Sun Apr 21, 2002 9:08 pm
by EvilWalrus
$row is just an array returned from my DB.. you can use it in a while() do(), for(), or any kinda loop... the possibilities are endless..
Posted: Sun Apr 21, 2002 9:14 pm
by phice
Here's what I've got...
Code: Select all
<?php
$connection = @mysql_connect("localhost");
$db = mysql_select_db("phicecom", $connection);
$sql = "SELECT * FROM updates";
$result = mysql_query($sql,$connection) or die("Couldn't execute query.");
while ($row = mysql_fetch_array($result)) {
$row = $row;
}
print "<table width="95%" align="center">
";
for ($x=0; $x<count($row); $x++)
{
$class = $class == "background: #eeeeee;" ? "background: #dddddd;" : "background: #eeeeee;";
$msg .= "<tr><td style="$class"><font size="1" face="Verdana, Arial, sans, sans-serif" >" . $rowї$x]ї'url'] . " - " . $rowї$x]ї'hits'] . " - " . $rowї$x]ї'membername'] . "</font></td></tr>
";
}
print "</table>
";
?>
Now, I know that I don't have a user/pass, due to my server doesn't require either one. It just puts out a blank table, with 2 -'s, and it's background is #eeeeee. Any thoughts?
bump
Posted: Sun Apr 28, 2002 1:30 am
by phice
bump.
Posted: Sun Apr 28, 2002 8:58 pm
by timmy
here's a simple way of doing it, it works for me:
Code: Select all
/* Alternate row colors */
$bg1 = "#666666";
$bg2 = "#000000";
static $bg;
if ($bg == $bg1)
{
$bg = $bg2;
}
else
{
$bg = $bg1;
}
sample table:
Code: Select all
echo "<tr><td bgcolor=$bg align=center>$blah</td>
";
obviously, i use a while statement to itterate through the variables to create the table cells. I can't remember where I got that from, someone else's code!
try that. To see it action, look here:
http://tim.timmy.ws/members/media/videos.phtml
good luck!
Posted: Mon Apr 29, 2002 11:32 am
by sam
Code: Select all
/* Alternate row colors */
for($i=0;$row = mysql_fetch_assoc($r);$i++){
$bg = ($i % 2)?"#666666":"#000000";
}
Adn andrew I don't feel that Jason's class is creating that much over head. Doing a numerical operation is a lot quicker than a string comparrison. Although the if statement is a little iffy (no pun).
Cheers Sam