doukan4039 2014-10-19 19:58
浏览 44
已采纳

在MySQL内部的id迭代更改中打印新的<div>,同时从MySQL结果循环

I have this MySQL table:

+------------+-------------+
| contact_id | _company_id |
+------------+-------------+
|          1 |           1 |
|          2 |           1 |
|          3 |           1 |
|          4 |           2 |
|          5 |           2 |
+------------+-------------+

Im trying to print a new <div> containing all rows with the same _company_id

So far I have this code:

$previous = '';

while ($result = $stmt->fetch()) {

    $current = $_company_id;

    //This should be executed every first iteration and every time $_company_id changes
    if ($current != $previous) { $html .= '<div id="company-' . $_company_id . '" class="tab-pane fade">'; }

    //Iterate all contact_id here with the same _company_id

    if ($current != $previous) { $html .= '</div>'; }

    $previous = $current;

}

My desired output is:

<div id="company-1" class="tab-pane fade">123</div>
<div id="company-2" class="tab-pane fade">45</div>

Right now the output is:

<div id="company-1" class="tab-pane fade">1</div>
23
<div id="company-2" class="tab-pane fade">4</div>
5

What should I change?

  • 写回答

2条回答 默认 最新

  • doumang20060820 2014-10-19 20:07
    关注
    $previous = null;
    
    while ($result = $stmt->fetch()) {
    
        $current = $_company_id;
    
        //This should be executed every first iteration and every time $_company_id changes
        if ($current !== $previous) {
          if($previous !== null)
            $html.='</div>';
          $html .= '<div id="company-' . $_company_id . '" class="tab-pane fade">';
        }
    
        //Iterate all contact_id here with the same _company_id
    
        $previous = $current;
    
    }
    
    if($previous!==null)
      $html.='</div>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛