Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: logic behind making the <li>=>divs alternate colors?

  1. #1
    Join Date
    Feb 2008
    Posts
    137
    Thanks
    18
    Thanked 2 Times in 2 Posts

    Question logic behind making the <li>=>divs alternate colors?

    It seems my <li>'s need to be coded by hand not sure.

    All I need is the logic behind making the <li>=>divs alternating colors dynamically
    Code:
     
    <div class="post_body nicelist">
    <ol>
    
    <li class="palt"> <!-- background: #eee;-->
    <div class="comment_author left">
    <span class="comment"><?php echo $row_rsnews['txtHeading']; ?></span></div>
    <div class="clearer"></div>
    <div class="body">
    <p><?php echo nl2br($row_rsnews['memdescription']); ?></p>
    </div>
    </li> <!--color end background: #eee;-->
    
    
    <li id="comment-256"> <!--comment num random; background: #fff;-->
    <div class="comment_author left">
    <span class="comment"><a href="#">Elementum</a></span>
    <div class="date"><a href="#">December 21st, 2007 at 12:46 pm</a></div>
    </div>
    <div class="clearer"></div>
    <div class="body">
    <p>Comments are cool!</p>
    </div>
    </li> <!--color end background: #fff;-->
    
    </ol>
    </div>
    Cheers

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You might find this basic example useful:
    PHP Code:
    <ul>
    <?php
    for($i=0;$i<10;$i++)
    {
        if(
    $i%2==0)
            echo 
    '<li style="background:#eee;">Content of Even list</li>';
        else
            echo 
    '<li style="background:#fff;">Content of Odd List</li>';
    }
    ?>
    </ul>
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Feb 2008
    Posts
    137
    Thanks
    18
    Thanked 2 Times in 2 Posts

    Smile

    I managed to figure it out, atually really simple.
    PHP Code:
    <style type="text/css">
    li.palt {background: #eee;}
    </style>
     
    <ol>
    <?php do { 
    $cls = ($cls =='palt' '' 'palt' ); // Switch the background color. ?>
    <li class="<?php echo $cls ?>">
    <div class="comment_author left">
    <span class="comment">some_text</span></div>
    <div class="clearer"></div>
    <div class="body">
    <p>some_text</p>
    </div>
    </li>
    <?php } while ($row_rsnews mysql_fetch_assoc($rsnews)); ?>
    </ol>
    Thanks

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    you shouldn't have an empty class, so my only suggestion would be to replace

    $cls = ($cls =='palt' ? '' : 'palt' ); // Switch the background color. ?>
    <li class="<?php echo $cls ; ?>">
    with

    Code:
    <li<?php echo $cls=='palt' ? '>' : ' class="palt">'; ?>
    which would produce

    Code:
    <li>
    and

    Code:
    <li class="palt">
    respectively

  5. #5
    Join Date
    Feb 2008
    Posts
    137
    Thanks
    18
    Thanked 2 Times in 2 Posts

    Thumbs up

    Ok fair enough, not good to have an empty class;
    Your method doesn't actually work.
    Fixed:
    PHP Code:
    $cls = ($cls =='palt' 'alt' 'palt' ); // Switch the background color. ?> 
    Cheers
    Last edited by student101; 09-15-2008 at 06:14 PM.

  6. #6
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    For CSS aficionados, you can do this with pure CSS (assuming you're using a modern browser that supports advanced CSS selectors). Look into the nth-child selector.

  7. #7
    Join Date
    Feb 2008
    Posts
    137
    Thanks
    18
    Thanked 2 Times in 2 Posts

    Thumbs up

    Quote Originally Posted by Medyman View Post
    For CSS aficionados, you can do this with pure CSS (assuming you're using a modern browser that supports advanced CSS selectors). Look into the nth-child selector.
    My problem is more dynamic, the data comes from the DB and it needs to change accordingly, including the OLD guys who are viewing has IE5.5 / 6, they for some odd reason are happy with that.

    Neat idea.

    Cheers
    Last edited by student101; 09-16-2008 at 05:56 AM.

  8. #8
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by Medyman View Post
    For CSS aficionados, you can do this with pure CSS (assuming you're using a modern browser that supports advanced CSS selectors). Look into the nth-child selector.
    that is a CSS3 property and at best will not be supported by IE for probably another two years, although if history is proving anything they may take 3/4 if they ever support it LOL

    My problem is more dynamic, the data comes from the DB and it needs to change accordingly, including the OLD guys who are viewing has IE5.5 / 6, they for some odd reason are happy with that.
    if i used IE regularly i would use verision 6 myself. I do not like the new interface of 7 and 8

  9. #9
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Quote Originally Posted by boogyman View Post
    that is a CSS3 property and at best will not be supported by IE for probably another two years, although if history is proving anything they may take 3/4 if they ever support it LOL
    It's getting there. At least IE8 will have full CSS 2.1 compliance. They really seem to be focused on meeting the requirements of the standards community. Good for them, I say.

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There's nothing wrong with having an empty class. The modulo method is more flexible and doesn't require an extra variable.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •