SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    creating unique classes for a <ul> list

    Hi

    My script outputs a list of categories as an unordered list.
    I can format the list using css but would like each list item to be formatted differently (eg background color).

    Is it possible to 'count' each list item - and use the each items 'number' as the class identifier?

    eg
    <ul>
    <li class="0">one
    <li class="1">two
    <li class="2">three
    </ul>

    TIA

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes.
    Code:
    <?php
    $c = count($categories);
    for($i = 0; $i < $c; $i ++){
    ?>
    <li class="<?= $i ?>"><?=$category[$i] ?></li>
    <?php } ?>

  3. #3
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure, but I don't think you can have numbers for css class names. Other than that it would just be a matter of having an incrementing $i in your loop that builds the <ul> and printing it, i.e.

    PHP Code:
    print "<li class='myLi".$i."'>".$row['columname']."</li>"

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See if this helps you figure something out:
    PHP Code:
    $values = array('one''two''three');

    echo 
    "<ul>\n";

    $count count($values);
    for(
    $i=0$i $count$i++)
    {
        echo<<<UNIQUEID
            <li class="myCoolStyle$i">$values[$i]</li>\n
    UNIQUEID;
    }

    echo 
    "</ul>\n"


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
  •