如何通过循环将数据分组到同一标题下 [英] How to group the data under same heading through looping

查看:69
本文介绍了如何通过循环将数据分组到同一标题下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get data from two joined tables,From one table I am collecting list heading and from second joined table i am collecting list data.

型号代码:

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>

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

Kindly check the below image i have shown the output of this code, and also i have draw what output i need thanks, Kindly help me. thanks

推荐答案

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

Just store tha last name and only print it if the new name differs from it. We're also gonna create new lists when that happens so you have different lists per group.

<?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>

这篇关于如何通过循环将数据分组到同一标题下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆