SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do i loop throught these and stop on the second to last one?

    HI,

    i have a list of categories spit out from my db. the list items all have a class on them applying a
    underline to them...problem is i dont want the underline on the last list item....how do i do this? ive tried a
    for loop in my code but, of course, it doesnt work because it just iterates through all of the list items. What i want to say is print out the list items with this
    class until you get to the last list item...when you're on the last list item use this class ( without the underline)...any ideas how to do this?

    Heres the code as it is with me trying to get it working with the for loop:

    Code PHP:
    $sql = "SELECT * FROM services order by serviceID";
    $result = mysql_query($sql);
    $no = mysql_num_rows($result);
     
     
    while($row = mysql_fetch_assoc($result)){
    for($i = 0; $i < $no; ++$i) {
    ?><li><a href="services_detail.php?s=<?php echo $row['name']; ?>"><?php echo $row['displayname']; ?></a></li><?php
    }
    if($i == $no) { ?>
    <li><a style="color: red;" href="services_detail.php?s=<?php echo $row['name']; ?>"><?php echo $row['displayname']; ?></a></li>
    <?php
    }
    }

  2. #2
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No need for a for loop I think - try this:
    PHP Code:
    <?php
    $sql 
    "SELECT * FROM services order by serviceID";
    $result mysql_query($sql);
    $no mysql_num_rows($result);

    $i 1;
    while(
    $row mysql_fetch_assoc($result)){
        if(
    $i == $no) { ?>
    <li><a style="color: red;" href="services_detail.php?s=<?php echo $row['name']; ?>"><?php echo $row['displayname']; ?></a></li>
    <?php
        
    }
        else{
    ?>
    <li><a href="services_detail.php?s=<?php echo $row['name']; ?>"><?php echo $row['displayname']; ?></a></li>
    <?php
        
    }
    $i++;
    }
    ?>
    Last edited by mudshark; Jun 11, 2007 at 22:51. Reason: code correction

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aha thanks...i was doing something similar but i abandoned it! Damm!

    thanks

  4. #4
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by elduderino View Post
    aha thanks...i was doing something similar but i abandoned it! Damm!

    thanks
    Just noticed I forgot to omit the first <li></li> line - should work perfectly 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
  •