如何使用PHP为每个循环中的元素添加不同的类

时间:2017-03-25 06:58:03

标签: php class foreach

我需要为循环中的div元素分配不同的类名。

<?php
    $sql="select * from category order by id desc";
    $catdata=$dbobj->db_get_data($sql);
    foreach ($catdata as $v) {
?>
      <div class="col-sm-3">
       <a href="javascript:void(0)" class="item-exhibitation maploc"><?php echo $v['name'] ?></a><!-- activemap1 -->
      </div>

 <?php } ?>

这里我需要保持类名称的动态,所有内容都在下面给出:

$classarr = array("item-exhibitation maploc","item-parking maploc","item-offices maploc","item-storage maploc");

以上4个是我的类,这些将动态分配。在第4次迭代之后,它将取自类名数组中的第一个元素。如何实现这一目标?

4 个答案:

答案 0 :(得分:1)

这是您正在寻找的输出吗?或者我理解错了。

<?php
$counter=0;
$classarr = array("item-exhibitation maploc", "item-parking maploc", "item-offices maploc", "item-storage maploc");
$sql = "select * from category order by id desc";
$catdata = $dbobj->db_get_data($sql);
foreach ($catdata as $v)
{        
    ?>
    <div class="col-sm-3">
        <a href="javascript:void(0)" class="<?php echo $classarr[$counter];?><?php echo $v['name'] ?></a><!-- activemap1 -->
    </div>
    <?php
    $counter++;
    if ($counter == 4) $counter = 0;
}
?>

答案 1 :(得分:0)

    <?php
$classarr = array("item-exhibitation maploc", "item-parking maploc", "item-offices maploc", "item-storage maploc");
$sql = "select * from category order by id desc";
$catdata = $dbobj->db_get_data($sql);
$i = 0;
foreach ($catdata as $v) {
    $class_name = '';
    ?>
    <div class="col-sm-3">
        <?php
        if (array_key_exists($i, $classarr)) {
            $class_name = $classarr[$i];
        } else {
            $class_name = $classarr[0];
            $i=0;
        }
        ?>
        <a href="javascript:void(0)" class="<?= $class_name; ?>"><?php echo $v['name'] ?></a>
    </div>
    <?php
    $i++;
}
?>

答案 2 :(得分:0)

您需要使用索引设置基于模数的类值,如下所示:

<?php
    $sql="select * from category order by id desc";
    $catdata=$dbobj->db_get_data($sql);
    $itemCount = count($classarr); //We need the count, but we calculate it once
    $index = $itemCount - 1; //We start it from last, so we will go to first at the first iteration
     foreach ($catdata as $v) {

     ?>
 <div class="col-sm-3">
 <a href="javascript:void(0)" class="<?php echo $classarr[$index = ($index + 1) % $itemCount]; ?>"><?php echo $v['name'] ?></a><!-- activemap1 -->
 </div>
 <?php
     }
 ?>

答案 3 :(得分:0)

根据foreach的php.net文档:

  

有两种语法:

foreach (array_expression as $value)
    statement

foreach (array_expression as $key => $value)
    statement
     

1

使用第二种语法将索引自动分配给 $ key 。然后使用 modulo arithmetic operator计算$index值的余数除以类名数组的长度(即$index % count($classarr))。

0 % 4 => 0
1 % 4 => 1
2 % 4 => 2
3 % 4 => 3
4 % 4 => 0
5 % 4 => 1
...

这样,$ classarr的索引将是顺序和重复的,导致类名被重复。

<?php
$sql="select * from category order by id desc";
$catdata=$dbobj->db_get_data($sql);
$classarr = array("item-exhibitation maploc", "item-parking maploc", "item-offices maploc", "item-storage maploc");
 foreach ($catdata as $index => $v) {
    $class = $classarr[$index % count($classarr)];
 ?>
<div class="col-sm-3">
    <a href="javascript:void(0)" class="<?php echo $class;?>"><?php echo $v['name'] ?></a><!-- activemap1 -->
</div>
<?php
 }
?>

请参阅this PHPfiddle中的演示。输出可以在下面的代码段中看到:

#Legend {
  float: right;
}

.item-exhibitation {
  color: #ff0000;
}

.item-parking {
  color: #00f;
}

.item-offices {
  color: #0f0;
}

.item-storage {
  color: #888;
}
<div id="Legend">
  <h3>Legend</h3>
  <div class="item-exhibitation">.item-exhibitation</div>
  <div class="item-parking">.item-parking</div>
  <div class="item-offices">.item-offices</div>
  <div class="item-storage">.item-storage</div>
</div>
<h3>Items</h3>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-exhibitation maploc">Exposition Center</a>
  <!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-parking maploc">Parking Lot A</a>
  <!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-offices maploc">Insurance Agency A</a>
  <!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-storage maploc">Personal Storage A</a>
  <!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-exhibitation maploc">Convention Center</a>
  <!-- activemap1 -->
</div>
<div class="col-sm-3"><a href="javascript:void(0)" class="item-parking maploc">Parking Lot B</a>
  <!-- activemap1 -->
</div>

1 <子> http://php.net/foreach