SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2001
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How I was wondering if anyone could help me out, Iím trying to work out how to pull data from the database and place each line into a table and make the tables Red, Greeen, Red, kind of like what is done on this board.

    Any help would be most welcome

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is done using the modulus operator (%), like this:

    PHP Code:
    $num 0;
    printf('<table width="70%">');
      while (
    $row mysql_fetch_array($result)) {
      
    $content $row["content"];
        if (
    $num == 0) {
        
    $color "#E9E9E9";
        } else {
        
    $color "#666666";
        }
      
    printf('<tr><td bgcolor="%s">%s</td></tr>'$color$content);
      
    $num++;
      }
    printf('</table'); 
    Or, if you'd rather use the ternary operator (I would!), and don't mind the fact that it looks a little confusing...

    PHP Code:
    $num 0;
    printf('<table width="70%">');
      while (
    $row mysql_fetch_array($result)) {
      
    $content $row["content"];
        
    $color = ($num == 0) ? "#E9E9E9" "#666666";
      
    printf('<tr><td bgcolor="%s">%s</td></tr>'$color$content);
      
    $num++;
      }
    printf('</table'); 
    The modulus operator (%) divides one number by another, and returns the remainder - not the actual number of times it goes into it. Therefore, we divide $num by 2 - if it is an even number, it will go in evenly and leave a remainder of 0.

    If it is an odd number, it will not equal 0 - we assign a HEX value to $color for each of these outcomes.
    Last edited by TWTCommish; Feb 17, 2001 at 10:25.

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An even easier way not that Chris has not given exceptional adivce would be, also keep in mind that in printf() to be able to use a % sign like in the table tag with width="70%"
    you need two % signs like width="70%%"
    PHP Code:
    $i 1;
    printf('<table width="70%%" border=1>');
        while(
    $row mysql_fetch_array($result)) {
            
    printf('<tr bgcolor="%s"><td>%s</td></tr>', (is_int($i/2)) ? "#cccccc" "ff0000"$row["cat_name"]);
            
    $i++;
            }
    printf('</table>'); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ack, I just KNEW Freddy would swoop down and give me another unexpected lesson. But yes, that should be easier.

    I just figured the modulus operator is an easier way to start - combining the is_int() function with the ternary operator like that would have confused the heck outta me a few months ago.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, True, although I saw the 70% thing and couldn't resist testing another method of alternating row colors and posting my results.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, I don't blame you - the idea is to present as many possible solutions to the problem as possible, and let the originator of the question choose which they prefer.

    Anyway, my work is done here, for now.


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
  •