Off topic; Table coloring question
Moderator: General Moderators
Off topic; Table coloring question
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...
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...
Last edited by phice on Mon Jul 04, 2016 5:54 am, edited 1 time in total.
- abionifade
- Forum Commoner
- Posts: 34
- Joined: Thu Apr 18, 2002 5:32 pm
- EvilWalrus
- Site Admin
- Posts: 209
- Joined: Thu Apr 18, 2002 3:21 pm
- Location: Springmont, PA USA
that code is just too much overhead, use the following (it's used on EvilWalrus.com):
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....
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>
";
?>Hope it helps....
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.
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;
}
}
}- EvilWalrus
- Site Admin
- Posts: 209
- Joined: Thu Apr 18, 2002 3:21 pm
- Location: Springmont, PA USA
Could you show me what you've got the $row? Because, I've got everything else to work, except for $row.EvilWalrus wrote:that code is just too much overhead, use the following (it's used on EvilWalrus.com):
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.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> "; ?>
Hope it helps....
- EvilWalrus
- Site Admin
- Posts: 209
- Joined: Thu Apr 18, 2002 3:21 pm
- Location: Springmont, PA USA
Here's what I've got...
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?
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>
";
?>bump
bump.
here's a simple way of doing it, it works for me:
sample table:
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!
Code: Select all
/* Alternate row colors */
$bg1 = "#666666";
$bg2 = "#000000";
static $bg;
if ($bg == $bg1)
{
$bg = $bg2;
}
else
{
$bg = $bg1;
}Code: Select all
echo "<tr><td bgcolor=$bg align=center>$blah</td>
";try that. To see it action, look here:
http://tim.timmy.ws/members/media/videos.phtml
good luck!
- sam
- Forum Contributor
- Posts: 217
- Joined: Thu Apr 18, 2002 11:11 pm
- Location: Northern California
- Contact:
Code: Select all
/* Alternate row colors */
for($i=0;$row = mysql_fetch_assoc($r);$i++){
$bg = ($i % 2)?"#666666":"#000000";
}Cheers Sam
