使用代码Igniter控制器和视图创建foreach循环 [英] Creating foreach loops using Code Igniter controller and view
问题描述
这是我发现自己几次的情况,我只想一次性清除它。
最好的只是为了告诉你我需要什么
code> function my_controller()
{
$ id = $ this-> uri-> segment(3);
$ this-> db-> from('cue_sheets');
$ this-> db-> where('id',$ id);
$ data ['get_cue_sheets'] = $ this-> db-> get();
$ this-> db-> from('clips');
$ this-> db-> where('sheet_id','CUE SHEET ID GOES IN HERE ???');
$ data ['get_clips'] = $ this-> db-> get();
$ this-> load-> view('show_sheets_and_clips',$ data);
}
我的视图 >
<?php if($ get_cue_sheets-> result_array()){?>
<?php foreach($ get_cue_sheets-> result_array()as $ sheetRow):?>
< h1><?php echo $ sheetRow ['sheet_name']; ?>< / h1>
< br />
<?php if($ get_clips-> result_array()){?>
< ul>
<?php foreach($ get_clips-> result_array()as $ clipRow):?>
< li><?php echo $ clipRow ['clip_name']; ?>< / li>
<?php endforeach; >
< / ul>
<?php} else {echo'No Clips Found'; }?>
<?php endforeach; >
<?php}?>
所以基本上我想显示一些表,然后在每个表中
我希望这对于那里的人有意义。
Tim
Code Igniter论坛上的用户想出了解决方案下面。原始主题是此处。
分别查找每张纸张的剪辑效率不高。使用JOIN查询同时获取这两个列表。
//获取数据
$ this-> ; db-> from('cue_sheets');
$ this-> db-> where('cue_sheets.id',$ id);
$ this-> db-> join('clips','clips.sheet_id = cue_sheets.id','left');
$ rawdata = $ this-> db-> get() - > result_array();
//将数据准备为多维数组
$ data = array();
foreach($ rawdata as $ row)
{
//如果这是新工作表的第一个剪辑,为它创建一个新条目
if(!isset data [$ row ['id']])
{
$ data [$ row ['id']] = $ row;
$ data [$ row ['id']] ['clips'] = array();
}
//将当前剪辑添加到工作表
$ data [$ row ['id']] ['clips'] [] = $ row;
}
现在在你的视图中,你可以循环遍历表,通过剪辑:
foreach($ data as $ sheet){
// make header etc.
if(sizeof($ sheet ['clips']))
{
foreach($ sheet ['clips'] as $ clip)
{
//显示剪辑
}
} else {
//显示'无剪辑'
}
}
再次感谢,
Tim
This is a situation I have found myself in a few times and I just want clear it up once and for all.
Best just to show you what I need to do in some example code.
My Controller
function my_controller()
{
$id = $this->uri->segment(3);
$this->db->from('cue_sheets');
$this->db->where('id', $id);
$data['get_cue_sheets'] = $this->db->get();
$this->db->from('clips');
$this->db->where('sheet_id', ' CUE SHEET ID GOES IN HERE ??? ');
$data['get_clips'] = $this->db->get();
$this->load->view('show_sheets_and_clips', $data);
}
My View
<?php if($get_cue_sheets->result_array()) { ?>
<?php foreach($get_cue_sheets->result_array() as $sheetRow): ?>
<h1><?php echo $sheetRow['sheet_name']; ?></h1>
<br/>
<?php if($get_clips->result_array()) { ?>
<ul>
<?php foreach($get_clips->result_array() as $clipRow): ?>
<li><?php echo $clipRow['clip_name']; ?></li>
<?php endforeach; ?>
</ul>
<?php } else { echo 'No Clips Found'; } ?>
<?php endforeach; ?>
<?php } ?>
So basically I am trying to display a number of sheets and then within each of those sheets the clips that belong to that sheet.
I hope this makes sense to someone out there.
Thanks,
Tim
A user on the Code Igniter Forums came up with the solution below. The original thread is here.
It’s not efficient to look up the clips for every sheet separately. Use a JOIN query to get both lists at the same time.
// get the data
$this->db->from('cue_sheets');
$this->db->where('cue_sheets.id', $id);
$this->db->join('clips', 'clips.sheet_id = cue_sheets.id', 'left');
$rawdata = $this->db->get()->result_array();
// prepare the data into a multidimensional array
$data = array();
foreach($rawdata as $row)
{
// if this is the first clip of a new sheet, make a new entry for it
if (!isset($data[$row['id']]))
{
$data[$row['id']] = $row;
$data[$row['id']]['clips'] = array();
}
// add the current clip to the sheet
$data[$row['id']]['clips'][] = $row;
}
Now in your view you can loop through the sheets, and within that, loop through the clips:
foreach($data as $sheet) {
// make header etc.
if (sizeof($sheet['clips']))
{
foreach($sheet['clips'] as $clip)
{
// show clip
}
} else {
// show 'no clips'
}
}
Thanks again,
Tim
这篇关于使用代码Igniter控制器和视图创建foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!