ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-如何通过循环在同一标题下分组数据

2019-10-12 21:34:14  阅读:242  来源: 互联网

标签:php codeigniter loops


我试图从两个联接表中获取数据,从一个表中我收集列表标题,从第二个联接表中我收集列表数据.

型号代码:

function view_searching_type_items()
{
  $this->db->select("searching_type.searchtype_name, search_type_item.st_item_id_pk, search_type_item.searchtype_itemname");
  $this->db->from('searching_type');
  $this->db->join('search_type_item', 'search_type_item.st_id_fk = searching_type.st_id_pk');
  $query = $this->db->get();
  return $query->result_array(); 
}

控制器:

$url1 = 'http://localhost/pakistanmenu/index.php/Restaurant/ViewSearchingTypeItems';
    $curl_hand = curl_init($url1);
    curl_setopt($curl_hand, CURLOPT_TIMEOUT, 5);
    curl_setopt($curl_hand, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($curl_hand, CURLOPT_RETURNTRANSFER, true);
    $data1 = curl_exec($curl_hand);// json_decode($data)
    curl_close($curl_hand);
    $result['searchingtype'] = (array) json_decode($data1,true);

    $this->load->view('header');
    $this->load->view('index',$result);
    $this->load->view('footer');

查看代码:

<?php foreach ($searchingtype as $searchingtyp):?>

 <ul class="list-group checked-list-box">
    <h1 class="page-header leftsidebar-heading"><?php echo $searchingtyp['searchtype_name'];?></h1>

     <li class="list-group-item" data-style="button" style="cursor: pointer;">
         <span class="state-icon glyphicon glyphicon-unchecked">

         </span>

         <?php echo $searchingtyp['searchtype_itemname'];?>

         <input type="checkbox" class="hidden">

     </li>

请检查下面的图像,我已经显示了此代码的输出,并且我还画了我需要的输出,谢谢,请帮助我.谢谢

enter image description here

解决方法:

只需存储您的姓氏,并且仅在新名称与其不同时才打印它.发生这种情况时,我们还将创建新列表,以便每个组具有不同的列表.

<?php 
// Define our test variable
$lastName = null;

foreach ($searchingtype as $searchingtyp):
?>

    <?php 
    if ($lastName != $searchingtyp['searchtype_name']): 
        // It's not the same name
        if ($lastName != null):
            // Close the previous list (except from the first iteration).
        ?>
            </ul>
        <?php
        endif;
        ?>

     ?>
        <ul class="list-group checked-list-box">
            <li><h1 class="page-header leftsidebar-heading"><?php echo $searchingtyp['searchtype_name'];?></h1></li>
    <?php 
        // Store the current name so we can check again in the next iteration
        $lastName = $searchingtyp['searchtype_name'];
    endif;
    ?>

// The rest of your code.. just move the </ul> after the loop.

<?php endforeach ?>

</ul>

标签:php,codeigniter,loops
来源: https://codeday.me/bug/20191012/1903106.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有