SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can you get it to be every other colour?

    Like the list of topics is like this:

    blue
    white
    blue
    white
    blue
    white
    blue
    white


    Etc.


    Just curious as to how to do it.

    Thanks,
    ~someonewhois

  2. #2
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are about 100 different ways to do it. I do it like this:

    PHP Code:
    $color 1;

    while(
    loop_through_topics) {
       if (
    $color) {
          echo 
    "<font color='#0000ff'>".$topic."</font>";
       } else {
          echo 
    "<font color='#ffffff'>".$topic."</font>";
       }
       
    $color = !$color;


  3. #3
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do it like this
    PHP Code:
    $c 0;
    while (
    something) {
      if (
    $c == 0) {
        
    $co "#ffffff";
      } else {
        
    $co "#0000ff";
      }
      echo 
    "<tr><td style=\"color:" .$co";\">Something</td></tr>";
      
    $c++;

    Last edited by notepad_coder; Jun 21, 2002 at 17:49.
    - the lid is off the maple syrup again!

  4. #4
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IO've also seen it done like this:
    PHP Code:
    $change TRUE;
    while (
    something) {
      if (
    $change == TRUE) {
        
    $c "#0000ff";
        
    $change FALSE;
      } elseif (
    $change == FALSE) {
        
    $c "#ffff00";
        
    $change TRUE;
      }
      echo 
    "<font color=\"" .$c"\">Something</font>";

    - the lid is off the maple syrup again!

  5. #5
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by notepad_coder
    IO've also seen it done like this:
    PHP Code:
    $change TRUE;
    while (
    something) {
      if (
    $change == TRUE) {
        
    $c "#0000ff";
        
    $change FALSE;
      } elseif (
    $change == FALSE) {
        
    $c "#ffff00";
        
    $change TRUE;
      }
      echo 
    "<font color=\"" .$c"\">Something</font>";


    Thank you all for your replies, but I think I'm going t otry this one.

    What's in the while(something), the mysql_fetch_object part?

    Thanks,
    ~someonewhois

  6. #6
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's in the while(something), the mysql_fetch_object part?
    yes, or mysql_fetch_array...or anything you're using to loop through all your results...
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  7. #7
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, thanks Redux and Notepad_Coder!

  8. #8
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    personally, I like the modulus operator method:
    http://209.151.66.65/showthread.php?...ternating+rows

    Works nicely.

    Sketch
    Aaron Brazell
    Technosailor



  9. #9
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <table width "400" border "0">
    while (
    rows rowsneeded)
    {
     
    $color == "#FFFFFF" // white
      
    echo "<tr bgcolor=" $color "><td>hey</td></tr>" 
     
    $color == "#CCCCCC" // Grey
      
    echo "<tr bgcolor=" $color "><td>hey</td></tr>" 
    }
    </
    table
    Sorry, I'm trying to learn php.. hehe, so I might reply to a few threads that have already been answered

    anything wrong with that method ^^

  10. #10
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW, heh, the modulas operator is awful... I can't stand it even though I can see it's uses

  11. #11
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by platinum
    PHP Code:
    <table width "400" border "0">
    while (
    rows rowsneeded)
    {
     
    $color == "#FFFFFF" // white
      
    echo "<tr bgcolor=" $color "><td>hey</td></tr>" 
     
    $color == "#CCCCCC" // Grey
      
    echo "<tr bgcolor=" $color "><td>hey</td></tr>" 
    }
    </
    table
    Sorry, I'm trying to learn php.. hehe, so I might reply to a few threads that have already been answered

    anything wrong with that method ^^
    It appears as if it would work, but it's nto the best method, since it's the rows needed stuff. I guess you could do mysql_num_rows in there, but I think the ohter one is best.

    Thanks everybod y.

    Thanks,
    ~someonewhois

  12. #12
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    platinum,

    The code you wrote will work. But if you want to use it for something like results from a database, it's going to be like trying to fit a square peg in a round hole.

    The problem is your actually outputting 2 rows for each loop iteration. This will give you lots of trouble when you need to use mysql_fetch_* to get the results, because you would have to do a mysql_fetch_* inside the loop to get the information for the second row.

    That's going to result in code that is difficult to understand and fragile.

    Another thing to consider is in your method, you have two echo statments, one for each row. This is not the most optimized way to do this because if you need to make changes to the way the row is printed out, you have to make the changes twice.

    This is trivial when outputting simple tables like we are here for demonstration, but if you want to output row that includes 10 fields from a database, it will get unwieldly.

    The code I posted shared the same problem in that it had 2 echo statments doing what one could.

    Looking at all these methods, I think the modulus one is best.

    Not to mention using a modulus is l33t uber-programmer stuff.

  13. #13
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not to mention using a modulus is l33t uber-programmer stuff.
    AND, when you're using a bitwise value (2 rows), the modulo operator isn't the fastes operator, as

    PHP Code:
    $dark false;
    while (...)
    {
        ...
        
    $dark = !$dark;

    is faster (although not much).

    BUT, if you want to do this trick with more than 2 rows, the modulo operator is the way to go.

    Vincent

  14. #14
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by platinum
    PHP Code:
    <table width "400" border "0">
    while (
    rows rowsneeded)
    {
     
    $color == "#FFFFFF" // white
      
    echo "<tr bgcolor=" $color "><td>hey</td></tr>" 
     
    $color == "#CCCCCC" // Grey
      
    echo "<tr bgcolor=" $color "><td>hey</td></tr>" 
    }
    </
    table


    I'm sorry...was that our PLATINUM posting PHP...? Now that is funny.

    Sketch
    Aaron Brazell
    Technosailor



  15. #15
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    3 words -- Shut Your Face

  16. #16
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm proud of him, our little boy's growing up

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  17. #17
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe, what's he been donig all this time?

    Why would anybody want anything but PHP?

  18. #18
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hate to mention the name in here, but ASP

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  19. #19
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)



    Why?!

    *runs in terror at the word*


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
  •