显示每个foreach索引的不同值(Display different value for each foreach index)
我有css类,我不想添加到每个li标签。 应该添加到一个li中,然后li应该是空的等等......
我试着补充一下
例如
<?php $css_class = 'class="pull_rigt"'; echo "<ul>"; foreach($posts as $key => $value ) { echo "<li $css_class>"; echo $value['data']; echo "</li>"; } echo "<ul>"; ?>输出应该是
<li class="pull_rigt"> data <li> <li> data </li>我尝试在每个foreach索引中匹配两个数字的幂,但无法计算出数学。
<?php $css_class = 'class="pull_rigt"'; echo "<ul>"; $i = 0; foreach($posts as $key => $value ) { <li <?php echo ($i & ($i - 1)) == 0 ? $css_class; ?> echo $value['data']; echo "</li>"; $i++; } echo "<ul>"; ?>I have css class that I don't want to add into every li tag. It should be added into one li and next li should be empty and so on...
I tried to add
for example
<?php $css_class = 'class="pull_rigt"'; echo "<ul>"; foreach($posts as $key => $value ) { echo "<li $css_class>"; echo $value['data']; echo "</li>"; } echo "<ul>"; ?>the output should be
<li class="pull_rigt"> data <li> <li> data </li>I tried matching power of two numbers in every foreach index but couldn't figure out the math.
<?php $css_class = 'class="pull_rigt"'; echo "<ul>"; $i = 0; foreach($posts as $key => $value ) { <li <?php echo ($i & ($i - 1)) == 0 ? $css_class; ?> echo $value['data']; echo "</li>"; $i++; } echo "<ul>"; ?>最满意答案
你不一定要添加一个类来应用这种公式样式,如果你的li一个接一个出现,你可以根据它们是odd还是使用以下CSS来设置它们的样式:
li:nth-of-type(odd){}要么
li:nth-of-type(even){}示例代码:
li:nth-of-type(odd) { color: red; }<ul> <li>item</li> <li>item</li> <li>item</li> <li>item</li> <li>item</li> <li>item</li> </ul>You dont necessarily have to add a class to apply this kind of formulaic styling, if your li appear one after another, you can simply style them based on whether they are odd or even using the following CSS:
li:nth-of-type(odd){}or
li:nth-of-type(even){}Example code:
li:nth-of-type(odd) { color: red; }<ul> <li>item</li> <li>item</li> <li>item</li> <li>item</li> <li>item</li> <li>item</li> </ul>
更多推荐
发布评论