A list with different css classes and php loop

I have a list with different css styles :


   <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

CSS classes can have numbers in them, they just can’t start with a number. So “style1” is a valid class name. so


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


$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?

thanks for reply but I means a query:


$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?

or a list like this:


<ul>
<li class="pdn5">one</li>
<li class="pdn5">two</li>
<li class="no-pdn">two</li>
</ul>

If you want to use increasing numbers you could use a for loop instead.


$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 % arrLen]


$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, …

@pmw57 Sir, you have just given me a wonderful idea. Thank you! (no thank you button?)

Something like this?
:tup:

or even this?
:lman:

Although, I will settle for this:
:drink:

Cannot arrange for that, but I am sharing my :coffee: with you, @pmw57 ! Tnx

@pmw57
:tup:
:beer:
:drink:
:lman:

Thanks alot

:blush: :good:

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>”;
}



This way would be more flexible i think.

Thank you

Oh no. :nono:

You’re mixing layers there fella. :wink:

@AnthonySterling: I didn’t get you what you meant btw :shifty:

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.

No, the problem is storing presentation information with data.

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

You’re dead right, it’s vital to preserve that separation of content and presentation,

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? :wink:

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…