显示每个foreach索引的不同值(Display different value for each foreach index)

编程入门 行业动态 更新时间:2024-10-25 03:32:40
显示每个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> 
  
 

更多推荐

本文发布于:2023-07-29 15:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317596.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   foreach   index   Display

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!