2017-07-12 24 views
1

我想從兩個連接的表中獲取數據,從一個表中收集列表標題,並從第二個連接表中收集列表數據。如何通過循環在同一標題下對數據進行分組

代碼型號:

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> 

請Ç heck下面的圖片我已經顯示了這段代碼的輸出,並且我也繪製了我需要的輸出謝謝,請幫助我。感謝

enter image description here

回答

0

只儲存塔姓氏且僅當新的名稱來自它不同於打印。當發生這種情況時,我們也會創建新的列表,以便每個組有不同的列表。

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

是的,先生現在航向的輸出不重複,但我怎樣才能使具有相同的標題一個清單羣組的數據,因爲我已經證明在圖中@ @馬格納斯·埃裏克森 – Bangash

+0

非常感謝你,先生,你很棒,你的答案像魅力一樣工作,你已經節省了我的時間。 @Magnus Eriksson – Bangash

+0

不用擔心。如果可以的話,隨時也可以上傳。 :-) –

0

快速和骯髒的:如果標題不同於以前使用

檢查。

if($title != $lasttitle){ 
echo 'title foo'; 
} 

在循環的結束:

$lasttitle = title; 
相關問題