如何在下面的代码中创建一组新的列而不是行?

时间:2016-03-17 13:07:28

标签: javascript php mysql conditional

我正在尝试为我的同事创建一个信息站点,他们可以添加他们拥有的所有信息,这样他们就不需要写下来了。变量$ cam_loc显示某些设备的位置。当我使用下面的代码而不是每个位置使用一个面板标题时,如果该位置有多于1个设备,则复制该位置。我想做一个条件,验证位置是否重复,如果是,而不是添加另一个小组标题,它应该添加另一列,在该位置有第二对信息。

这是代码:

<?php
$cam = $db->prepare('select * from camere');
$cam->execute();

while ($cams = $cam->fetch(PDO::FETCH_OBJ)) {?>
  <ul class="list" style="list-style: none;">                                                                               
    <div class="panel panel-primary">
      <div class="panel-heading">
        <li><h3 class="panel-title"><?php $cam_loc = $cams->locatie; ?><?php echo $cam_loc;?></h3> </li>
      </div>

      <div class="panel-body">
        <div class="row">
          <div class="col-md-2" align="center">
            <div class="list-group">
              <a class="list-group-item">Model: <strong><?php echo $cams->model; ?></strong></a>
              <a class="list-group-item">Ip Intern: <strong><?php echo $cams->ip_int; ?></strong></a>
              <a class="list-group-item">Ip Extern: <strong><?php echo $cams->ip_ext; ?></strong></a>
              <a class="list-group-item">Port: <strong><?php echo $cams->port; ?></strong></a>
              <a class="list-group-item">User: <strong><?php echo $cams->user; ?></strong></a>
              <a class="list-group-item">Pass: <strong><?php echo $cams->pass; ?></strong></a>
            </div>
          </div>
        </div>
      </div>
    </div>          
  </ul>
<?php } ?>          

1 个答案:

答案 0 :(得分:0)

首先,您需要在SQL语句中按标题对结果进行排序,将它们组合在一起,然后使用if语句确定当前记录是否与前一个记录具有相同的标题

<?php
$cam = $db->prepare('select * from camere');
$cam->execute();

$heading = "";

while ($cams = $cam->fetch(PDO::FETCH_OBJ)) {?>
  <ul class="list" style="list-style: none;">                                                                               
    <div class="panel panel-primary">

      <?php $cam_loc = $cams->locatie;

      if ($heading != $cam_loc) { ?>

      <div class="panel-heading">
        <li><h3 class="panel-title"><?php echo $cam_loc; ?></h3> </li>
      </div>

      <?php }
          //Update $heading ready for the next row.
          $heading = $cam_loc;
      } ?>

      <div class="panel-body">
        <div class="row">
          <div class="col-md-2" align="center">
            <div class="list-group">
              <a class="list-group-item">Model: <strong><?php echo $cams->model; ?></strong></a>
              <a class="list-group-item">Ip Intern: <strong><?php echo $cams->ip_int; ?></strong></a>
              <a class="list-group-item">Ip Extern: <strong><?php echo $cams->ip_ext; ?></strong></a>
              <a class="list-group-item">Port: <strong><?php echo $cams->port; ?></strong></a>
              <a class="list-group-item">User: <strong><?php echo $cams->user; ?></strong></a>
              <a class="list-group-item">Pass: <strong><?php echo $cams->pass; ?></strong></a>
            </div>
          </div>
        </div>
      </div>
    </div>          
  </ul>
<?php } ?>        

您可能需要在这些新代码行中稍微移动一些HTML标记,以便为您提供所需的布局,但是因为它在上面,您不会为每条记录获得新标题。