SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    Non-Member lostseed's Avatar
    Join Date
    Sep 2005
    Location
    Zurconion Soon!
    Posts
    1,549
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Even and Odd rows of color, my best try.

    This is the only way I could figure this out.
    Is this a good way to go about it? I dont know if I have errors or not.
    Is there perhaps an easier way?

    I tried to make it decide by even/odd #'s skipping 0
    (I dont know how to work the even/odd thing very well so i hope its right)

    PHP Code:
    <?php
    function row($y) {
        echo 
    $y++;
        return 
    $y;
    }
    $y 1;

    if (
    $y== ) {
        echo 
    "#fff";
    }
    else {
        echo 
    "#000";
    }
    ?>
    then place this in the page:
    PHP Code:
    <?php include_once ("color-rows.php"); ?>
    And place this in the style:
    PHP Code:
    <div style="background-color:"<?php $yrow($y); ?>;">

  2. #2
    Codehead. hamidof's Avatar
    Join Date
    Dec 2005
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at this:
    http://www.hamidof.com/Programmers_F...opic.php?t=156

    Its not exactly what you asked, but it might help!

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you just want to alternate between 2 colors, its pretty easy

    you can use any type of loop, im just going to use foreach to demo
    PHP Code:
    $y 0;
    foreach (
    $foo as $bar) {
        
    $color = ($y++ % 2) ? 'red' 'green';
        echo 
    $color;
    }
    //     $color = ($y++ % 2) ? 'red' : 'green'; is the same as the following, just shorter
    if (($y 2) != 0) { 
        
    $y++;
        
    $color 'red';
    } else {
        
    $y++;
        
    $color 'green';


  4. #4
    Non-Member lostseed's Avatar
    Join Date
    Sep 2005
    Location
    Zurconion Soon!
    Posts
    1,549
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats a lot easier way to do it, I am still uncomfortable with using the ()?x:y function even though Ive read over it a few times.

    I dont understand what $foo and $bar is. What would replace those variables?

  5. #5
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,411
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    This is a method I found somewhere and use.
    PHP Code:
    // Define your colors for the alternating rows
        
    $color1 "#90BCDE";
        
    $color2 "#D2E3F0";
        
    $row_count 0;

    while (
    $row mysql_fetch_array($result)) {

    /* Now we do this small line which is basically going to tell
        PHP to alternate the colors between the two colors we defined above. */

        
    $row_color = ($row_count 2) ? $color1 $color2;

    // Display the results
    echo "<TR bgcolor='".$row_color."'><TD align='center'>".$row['referance_num']."</TD></TR>";

    // Add 1 to the row count

        
    $row_count++;


    Working example - http://www.cpa-ely.com/cat/cat2.php
    Last edited by Rubble; Jul 30, 2006 at 02:08. Reason: Added link

  6. #6
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Kansas City, MO
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will be the easiest and quickest method for accomplishing this.

    CSS File
    --------
    tr.row0 { background-color: red; }
    tr.row1 { background-color: blue; }

    PHP File
    -------
    PHP Code:
    // inside while loop for displaying data
    $c ^= 1;
    echo 
    "<tr class=\"row$c\">"

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lostseed
    I dont understand what $foo and $bar is. What would replace those variables?
    those variables are irrelevant. i just wanted to show you that you place the code inside of a loop of some kind, whether it be a foreach() loop, a while() loop etc...it doesnt matter.

    in fact, you actually dont even need to place it inside of a loop, but i would imagine thats what you want to do.

  8. #8
    SitePoint Zealot DewChugr's Avatar
    Join Date
    Sep 2005
    Location
    Illinois
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ZareMedia
    This will be the easiest and quickest method for accomplishing this.

    CSS File
    --------
    tr.row0 { background-color: red; }
    tr.row1 { background-color: blue; }

    PHP File
    -------
    PHP Code:
    // inside while loop for displaying data
    $c ^= 1;
    echo 
    "<tr class=\"row$c\">"
    This is similar, but a line shorter than what I found somewhere. I used,

    .table_row_0 {background-color:#EEEEEE;}
    .table_row_1 {background-color:#DDDDDD;}

    $row_style = 0;
    while($row = mysql_fetch_row($result)) {
    $row_style = 1 - $row_style;
    ...
    <tr class='table_row_".$row_style."'>

  9. #9
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ZareMedia View Post
    This will be the easiest and quickest method for accomplishing this.

    CSS File
    --------
    tr.row0 { background-color: red; }
    tr.row1 { background-color: blue; }

    PHP File
    -------
    PHP Code:
    // inside while loop for displaying data
    $c ^= 1;
    echo 
    "<tr class=\"row$c\">"
    I would love to use this code, but am unsure where to place the php part ?

    At the moment, on http://www.130605.com/alternate-rows I have this in the page :

    <?
    $tablerow_count=0;
    function tablerowswitch() {
    global $tablerow_count;
    $tablerow_count++;
    if ($tablerow_count % 2) {
    echo "odd";
    }
    else {
    echo "even";
    }
    }
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta name="description" content="Test">
    <link rel="stylesheet" type="text/css" href="http://www.130605.com/styles.css">
    </head>

    <body>
    <table summary="Alternate rows">
    <tr class="<? tablerowswitch(); ?>">
    <td>First row</td>
    </tr>
    <tr class="<? tablerowswitch(); ?>">
    <td>Second row</td>
    </tr>
    <tr class="<? tablerowswitch(); ?>">
    <td>Third row</td>
    </tr>
    <tr class="<? tablerowswitch(); ?>">
    <td>Fourth row</td>
    </tr>
    </table>

    </body>

    </html>


    Any help much appreciated.

    Dez.

    p.s. I can't seem to get the table into the center, but that's another problem!

  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Hi Dez, not much wrong with what you have now. You are just missing one last ingredient.....

    you haven't echoed the class!
    PHP Code:
    <tr class="<?php echo tablerowswitch(); ?>">
    As for centering, either use table align="center" or wrap it in a div with margin: auto; in the style
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for that SpikeZ, it's appreciated. Thanks also for the aligning tip - got that one solved now.

    Is there any way of not including any php code within the tables and for it to still work please ? Looking at some of these posts on this thread, it seems as though there might be a way to do it, but because I'm a little new to this, am a little unsure. It would sure be great to add and take out rows, without worrying about the php code. It would be great if this could be done without javascript anywhere.

    Any help very much appreciated.

    Dez.
    Last edited by Dez; May 2, 2007 at 07:49.

  12. #12
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dez View Post
    Many thanks for that SpikeZ, it's appreciated. Thanks also for the aligning tip - got that one solved now.

    Is there any way of not including any php code within the tables and for it to still work please ? Looking at some of these posts on this thread, it seems as though there might be a way to do it, but because I'm a little new to this, am a little unsure. It would sure be great to add and take out rows, without worrying about the php code. It would be great if this could be done without javascript anywhere.

    Any help very much appreciated.

    Dez.

    The reason for asking the above, is, it would be great, to be able to just switch rows around, add rows, delete rows, without going through all the rows afterwards to alter the codes for the rows.

    Any help much appreciated.

    Dez.

  13. #13
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rather than doing the modulus (&#37 .... just do a compare... % is more cpu intensive than a simple compare (not much, but its obviously in a loop);

    use classes (css) and just do the class names...like this

    then just put the class name in. Having a function for it is WAY overkill... (my opinion, of course)

    PHP Code:

    $class 
    = ($class == 'row1') ? 'row2' 'row1'
    Zare's method above is about the only faster way. But his method is limted to classes ending in 1 or 0 .. so its not ideal for me (my css clasess are always row0 (header), row1(first color), row2(second color)

  14. #14
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about for the alternate two colors?
    PHP Code:
    while($rows $objDb->FetchRows($result)){
        
    $bgcolor = ($bgcolor == "#E4E4E4") ? "#FFFFFF" "#E4E4E4";
        echo 
    '<tr bgcolor="' $bgcolor '">';
        echo 
    '<td align="left" style="height:24px;">&nbsp;</td>';
        echo 
    '<td align="left">' $rows['company_name']. '</td>';
        echo 
    '<td align="left">' $rows['project_name'] .'</td>';
        echo 
    '<td align="left">&nbsp;</td>';
        echo 
    '<td align="left">&nbsp;</td>';
        echo 
    '<td align="left">' $rows['leadfromdate'] .'</td>';
        echo 
    '<td align="left">&nbsp;</td>';
        echo 
    '</tr>';

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  15. #15
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rajug...

    that works...same idea as mine...

    I would never do that because 1) bgcolor is depreciated... and 2) classes will save you when the client decides he wants to change the colors on all the tables on the site

  16. #16
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rjm1982 View Post
    rather than doing the modulus (%) .... just do a compare... % is more cpu intensive than a simple compare (not much, but its obviously in a loop);

    use classes (css) and just do the class names...like this

    then just put the class name in. Having a function for it is WAY overkill... (my opinion, of course)

    PHP Code:

    $class 
    = ($class == 'row1') ? 'row2' 'row1'
    Zare's method above is about the only faster way. But his method is limted to classes ending in 1 or 0 .. so its not ideal for me (my css clasess are always row0 (header), row1(first color), row2(second color)
    Many, many thanks for all the help on this from everyone - as usual, it's always appreciated.

    I must say that I do like the above idea of having separate colors for the headers and footers

    I'm fairly new to php, how would all this link together please ?

    Dez.

  17. #17
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just use a static variable!
    Code:
    function rowcolor() {
    static $col = 1;
    if($col == 1) {
      $col = 2;
      return "#fff";
    } else {
     $col = 1;
     return "#000";
    }
    }
    then whenever you need a color, just call rowcolor();

  18. #18
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for that - it's appreciated.

    Would that work for the headers and footers as well please ?

  19. #19
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you would need to create a new function for each set of rows, or use this modified function:
    Code:
    function rowcolor($num=1) {
    static $col$num = 1;
    if($col$num == 1) {
      $col$num = 2;
      return "#fff";
    } else {
     $col$num = 1;
     return "#000";
    }
    }
    then for each section just call it with something like rowcolor("3"); using a different number for each distinct section (like 1 for the header, 2 for the main, and 3 for the footer).

  20. #20
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I spotted this tutorial today. Shows a couple of ways of doing the same thing, has timings too, showing which is quickest.

  21. #21
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    I spotted this tutorial today. Shows a couple of ways of doing the same thing, has timings too, showing which is quickest.
    I got a 404 on that one

  22. #22
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dez View Post
    I got a 404 on that one
    Got it, although it got close to timing out.

    Looks good.

    I'll work through the night a little on this and show you lot the results, but could you guide me a little along the way please ?

    Dez.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •