php循环输出标题,php-如何通过循环在同一标题下分组数据
我試圖從兩個聯接表中獲取數據,從一個表中我收集列表標題,從第二個聯接表中我收集列表數據.
型號代碼:
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 echo $searchingtyp['searchtype_name'];?>
請檢查下面的圖像,我已經顯示了此代碼的輸出,并且我還畫了我需要的輸出,謝謝,請幫助我.謝謝
解決方法:
只需存儲您的姓氏,并且僅在新名稱與其不同時才打印它.發生這種情況時,我們還將創建新列表,以便每個組具有不同的列表.
// Define our test variable
$lastName = null;
foreach ($searchingtype as $searchingtyp):
?>
if ($lastName != $searchingtyp['searchtype_name']):
// It's not the same name
if ($lastName != null):
// Close the previous list (except from the first iteration).
?>
endif;
?>
?>
<?php echo $searchingtyp['searchtype_name'];?>
// 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
after the loop.標簽:php,codeigniter,loops
來源: https://codeday.me/bug/20191012/1903106.html
總結
以上是生活随笔為你收集整理的php循环输出标题,php-如何通过循环在同一标题下分组数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 工厂静态类,静态工厂模式(Sta
- 下一篇: php ismethod,结合php类三