Off topic; Table coloring question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Off topic; Table coloring question

Post 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...
Last edited by phice on Mon Jul 04, 2016 5:54 am, edited 1 time in total.
Image Image
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

Post by abionifade »

take a look at this example -

http://www.zend.com/codex.php?id=146&single=1

-Abi :)
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post 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++)
&#123;
    $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&#1111;$x]&#1111;'ID'] . "">" . $row&#1111;$x]&#1111;'Task'] . "</a></b> - (Submitted: <b style="color:red">" . $row&#1111;$x]&#1111;'Date'] . "</b>)</font></td></tr>
";
&#125;
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....
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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 ) 
    &#123; 
        if( $end === false ) 
        &#123; 
            $end = $this->GetCurrentRow(); 
        &#125; 
        for( $row = $start; $row <= $end; $row++ ) 
        &#123; 
            if ( ( $row % 2 ) != 0 ) 
            &#123; 
                $this->fstyles&#1111;"row"]&#1111;$row]&#1111;"bgcolor"] = $odd_colors; 
            &#125; else &#123; 
                $this->fstyles&#1111;"row"]&#1111;$row]&#1111;"bgcolor"] = $even_colors; 
            &#125; 
        &#125; 
    &#125;
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post by EvilWalrus »

too much overhead, IMHO.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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++)
&#123;
    $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&#1111;$x]&#1111;'ID'] . "">" . $row&#1111;$x]&#1111;'Task'] . "</a></b> - (Submitted: <b style="color:red">" . $row&#1111;$x]&#1111;'Date'] . "</b>)</font></td></tr>
";
&#125;
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.
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post 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..
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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)) &#123;
$row = $row;
&#125;


print "<table width="95%" align="center">
"; 
for ($x=0; $x<count($row); $x++) 
&#123; 
    $class = $class == "background: #eeeeee;" ? "background: #dddddd;" : "background: #eeeeee;"; 
    $msg .= "<tr><td style="$class"><font size="1" face="Verdana, Arial, sans, sans-serif" >" . $row&#1111;$x]&#1111;'url'] . " - " . $row&#1111;$x]&#1111;'hits'] . " - " . $row&#1111;$x]&#1111;'membername'] . "</font></td></tr>
"; 
&#125;
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?
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

bump

Post by phice »

bump.
timmy
Forum Newbie
Posts: 15
Joined: Fri Apr 19, 2002 9:45 am
Location: Calgary, AB

Post 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) 
&#123; 
$bg = $bg2; 
&#125;
else 
&#123; 
$bg = $bg1; 
&#125;
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!
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

Code: Select all

/* Alternate row colors */ 
for($i=0;$row = mysql_fetch_assoc($r);$i++)&#123;
   $bg = ($i % 2)?"#666666":"#000000";

&#125;
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
Post Reply