I have things saved in a database such as:
Apple Banana Orange Pear
They are all saved in the database on one line, separated by spaces.
I want to retrieve this information from the database, and put them into html like so:
<li>Apple</li>
<li class="alt">Banana</li>
<li>Orange</li>
<li class="alt">Pear</li>
How would I go about doing this? I’ve looked into explode() and then foreach, is that the best way?
$fruits = explode(" ", $q['fruits']);
$i = 1;
foreach( $fruits as $fruit ){
if ($i % 2 == 0){
$alt = ' class="alt"';
}else{
$alt = '';
}
echo "<li" . $alt . ">" . $value . "</li>";
$i++;
}
If
$q['fruits']contains string"Apple Banana Orange Pear"then…Code:
Output: