php-使用Code Igniter控制器和视图创建foreach循环
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-使用Code Igniter控制器和视图创建foreach循环,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1846字,纯文字阅读大概需要3分钟。
内容图文
这是我几次发现的情况,我只想一劳永逸.
最好只是在一些示例代码中向您展示我需要做什么.
我的控制器
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 } ?>
因此,基本上,我尝试显示一些工作表,然后在每个工作表中显示属于该工作表的剪辑.
我希望这对外面的人有意义.
谢谢,
蒂姆
解决方法:
Code Igniter论坛上的一位用户提出了以下解决方案.原始线程是here.
单独查找每张纸的剪辑效率不高.使用JOIN查询可同时获取两个列表.
// 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;
}
现在,在您的视图中,您可以循环浏览工作表,并在其中循环浏览剪辑:
foreach($data as $sheet) {
// make header etc.
if (sizeof($sheet['clips']))
{
foreach($sheet['clips'] as $clip)
{
// show clip
}
} else {
// show 'no clips'
}
}
再次感谢,
蒂姆
内容总结
以上是互联网集市为您收集整理的php-使用Code Igniter控制器和视图创建foreach循环全部内容,希望文章能够帮你解决php-使用Code Igniter控制器和视图创建foreach循环所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。