SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a list with different css classes and php loop

    I have a list with different css styles :

    Code:
       <ul>
         <li class="one">One</li>
         <li class="two">two</li>
         <li class="three">three</li>
       </ul>
    how can I write while code when there is different css styles?
    Thanks in advance

  2. #2
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    CSS classes can have numbers in them, they just can't start with a number. So "style1" is a valid class name. so

    PHP Code:
    <? for ($i 1$i 5$i++): ?>
    <li class="style<?= $i ?>"> </li>
    <? endfor ?>

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $classes = array('one''two''three');
    $i 0;
    loop (condition) {
        echo 
    $classes[$i];
        
    $i++;

    But, I'm guessing your loop might need to execute more time than how many different classes you have? For example, you have three classes, but need to output 5 li elements.

    What should happen? Should they be cycled like
    one
    two
    three
    one
    two

    Or should the remaining li elements just get no class at all?

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for reply but I means a query:

    PHP Code:
    $sql "SELECT * FROM db";
    $query mysql_query($sql);

    while(
    $row mysql_fetch_assoc($query))
    {
       echo 
    "<ul>";
       echo 
    "<li>{$row['title']}</li>";
       echo 
    "</ul>";

    but how can i write this loop when i have different classes?

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or a list like this:
    PHP Code:
    <ul>
    <
    li class="pdn5">one</li>
    <
    li class="pdn5">two</li>
    <
    li class="no-pdn">two</li>
    </
    ul

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by gfxdizayn View Post
    thanks for reply but I means a query:

    PHP Code:
    $sql "SELECT * FROM db";
    $query mysql_query($sql);

    while(
    $row mysql_fetch_assoc($query))
    {
       echo 
    "<ul>";
       echo 
    "<li>{$row['title']}</li>";
       echo 
    "</ul>";

    but how can i write this loop when i have different classes?
    If you want to use increasing numbers you could use a for loop instead.

    Code php:
    $sql = "SELECT * FROM db"; 
    $result = mysql_query($sql); 
    $resultLen = mysql_nom_rows($result);
     
    for ($i = 0; $i < $resultLen; $i++) {
        $row = mysql_fetch_assoc($result)) 
       echo '<ul>'; 
       echo '<li class="style',$i,'">',$row['title'],'</li>'; 
       echo '</ul>'; 
    }

    If you want the numbers to start from 1 then use $i = 1 for the initial-condition.

    If you want to loop through an array of specific values, then store the array length before the for loop with $arrLen = count($arr) and then inside the array use $arr[$i &#37; arrLen]

    Code php:
    $sql = "SELECT * FROM db"; 
    $result = mysql_query($sql); 
    $resultLen = mysql_nom_rows($result);
     
    $arr = array('one', 'two', 'three');
    $arrLen = count($arr);
     
    for ($i = 0; $i < $resultLen; $i++) {
        $row = mysql_fetch_assoc($result)) 
       echo '<ul>'; 
       echo '<li class="',$arr[$i % $arrLen],'">',$row['title'],'</li>'; 
       echo '</ul>'; 
    }

    Which causes the class to proceed in a waltz-like fashion.
    one, two, three, one two, three, one, two, three, ...
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Enthusiast presotrader's Avatar
    Join Date
    Mar 2009
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @pmw57 Sir, you have just given me a wonderful idea. Thank you! (no thank you button?)
    Manage PDF workflows with Hummingbird. Use a Nitro PDF coupon

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by presotrader View Post
    @pmw57 Sir, you have just given me a wonderful idea. Thank you! (no thank you button?)
    Something like this?


    or even this?


    Although, I will settle for this:
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Enthusiast presotrader's Avatar
    Join Date
    Mar 2009
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cannot arrange for that, but I am sharing my with you, @pmw57 ! Tnx
    Manage PDF workflows with Hummingbird. Use a Nitro PDF coupon

  10. #10
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Something like this?


    or even this?


    Although, I will settle for this:
    @pmw57





    Thanks alot

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gfxdizayn View Post
    thanks for reply but I means a query:

    PHP Code:
    $sql "SELECT * FROM db";
    $query mysql_query($sql);

    while(
    $row mysql_fetch_assoc($query))
    {
       echo 
    "<ul>";
       echo 
    "<li>{$row['title']}</li>";
       echo 
    "</ul>";

    but how can i write this loop when i have different classes?
    since you are getting data from table. it would be easy to create one field called css_class and store the related class their while inserting the data and perform as below thereafter:
    while($row = mysql_fetch_assoc($query))
    {
    echo "<ul>";
    echo "<li class="{$row['css_class']}">{$row['title']}</li>";
    echo "</ul>";
    }
    [/PHP]

    This way would be more flexible i think.

  13. #13
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHPycho View Post
    since you are getting data from table. it would be easy to create one field called css_class and store the related class their while inserting the data and perform as below thereafter:
    while($row = mysql_fetch_assoc($query))
    {
    echo "<ul>";
    echo "<li class="{$row['css_class']}">{$row['title']}</li>";
    echo "</ul>";
    }
    [/PHP]

    This way would be more flexible i think.
    Thank you

  14. #14
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHPycho View Post
    since you are getting data from table. it would be easy to create one field called css_class and store the related class their while inserting the data and perform as below thereafter:
    while($row = mysql_fetch_assoc($query))
    {
    echo "<ul>";
    echo "<li class="{$row['css_class']}">{$row['title']}</li>";
    echo "</ul>";
    }
    [/php]

    This way would be more flexible i think.
    Oh no.

    You're mixing layers there fella.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  15. #15
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @AnthonySterling: I didn't get you what you meant btw

  16. #16
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    He's got direct database information being mixed in with the view.

    Normally you would translate the database information into an array, then later on loop through that array to generate the html code.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  17. #17
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, the problem is storing presentation information with data.

    Build the css off the data if you need to, not the other way around.

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You're dead right, it's vital to preserve that separation of content and presentation,
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  19. #19
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The problem is you're proposing to mix presentation information with data. Your data and the presentation of this data should be distinct, separate concerns.

    If for example you build a web-service ontop of your database for third parties, what use is this css_class column? Sure you could just *not* use it, but does that seem right to you? Having redundant data just sat there?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  20. #20
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Its up to the OP to explain what differentiates two or more class styles probably from other values in the row from the database.

    Is it a sales figure, a timestamp or something like that - or is it just the first 2 results red the rest blue?

    Using "Select * from .... " gives us no clue either...


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
  •