如何遍历两个分别具有不同数据的“ div”?

时间:2019-05-29 02:46:39

标签: php html codeigniter

我正在尝试在我的Web应用程序中构建时间轴功能,我有两个不同的 div ,每个具有不同的数据,我正在尝试使用 foreach 循环,但不是按预期工作,可以请任何人帮助我实现这一目标,

这是我要执行厕所的两个 div

    <div class="row timeline-movement">
 <?php foreach ($events as $eve): ?>
    <div class="timeline-badge">
        <span class="timeline-balloon-date-day"><?php echo substr($eve->time,8, -8); ?></span>
        <span class="timeline-balloon-date-month " style="font-size: 20px;"><b><?php echo substr($eve->time,5, -12); ?></b></span>
    </div>

这是第一个 div

    <div class="col-sm-6  timeline-item">
        <div class="row">
            <div class="col-sm-11">
                <div class="timeline-panel credits">
                    <ul class="timeline-panel-ul">
                        <li><span class="importo"><?php echo $eve->title;?></span></li>
                        <li><span class="causale"><?php echo $eve->description;?></span> </li>
                        <li><p><small class="text-muted"><i class="glyphicon glyphicon-time"></i><?php echo $eve->time;?></small></p> </li>
                    </ul>
                </div>

            </div>
        </div>
    </div>

这是第二个 div


    <div class="col-sm-6  timeline-item">
        <div class="row">
            <div class="col-sm-11">
                <div class="timeline-panel debits">
                    <ul class="timeline-panel-ul">
                        <li><span class="importo"><?php echo $eve->title;?></span></li>
                        <li><span class="causale"><?php echo $eve->description;?></span> </li>
                        <li><p><small class="text-muted"><i class="glyphicon glyphicon-time"></i><?php echo $eve->time;?></small></p> </li>
                    </ul>
                </div>

            </div>
        </div>
    </div>
<?php endforeach ?>

这是从数据库获取价值的模式文件

 public function view_timeline($id){

    $this->db->select("*"); 
  $this->db->from('timeline');
    $this->db->where('projectID' ,$id);
    $this->db->order_by("time", "DESC");
  //$this->db->join('client', 'client.Client_id = projects.Client_id');
  $query = $this->db->get();

  return $query->result();
 }

这是我创建 $ event 变量以获取值的地方

    $data['events'] = $this->client_model->view_timeline($id);

我的预期输出将如下所示 https://bootsnipp.com/snippets/ODzE

1 个答案:

答案 0 :(得分:1)

  

以适当和合并的方式,您可以在此处使用oddeven公式来获得预期的输出结果

<?php 
$i=1;
foreach ($events as $eve){
    if($i%2){ 
      $div_class = "credits";
    }else{
      $div_class = "debits";
    }
?>
<div class="col-sm-6  timeline-item">
        <div class="row">
            <div class="col-sm-11">
                <div class="timeline-panel <?php echo $div_class; ?>">
                    <ul class="timeline-panel-ul">
                        <li><span class="importo"><?php echo $eve->title;?></span></li>
                        <li><span class="causale"><?php echo $eve->description;?></span> </li>
                        <li><p><small class="text-muted"><i class="glyphicon glyphicon-time"></i><?php echo $eve->time;?></small></p> </li>
                    </ul>
                </div>

            </div>
        </div>
 </div>
<?php $i++;} ?>