使用 Code Igniter 控制器和视图创建 foreach 循环

发布于 2024-09-05 20:14:00 字数 1317 浏览 7 评论 0原文

我曾多次遇到这种情况,我只想一劳永逸地解决它。

最好只是向您展示我需要在一些示例代码中做什么。

我的控制器

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

所以基本上我试图显示许多工作表,然后在每个工作表中显示属于该工作表的剪辑。

我希望这对那里的人来说是有意义的。

谢谢,

蒂姆

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虫児飞 2024-09-12 20:14:00

Code Igniter 论坛上的一位用户提出了以下解决方案。原始线程位于此处

单独查找每张纸的剪辑效率不高。使用 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'
  }
} 

再次感谢

蒂姆

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

梦回旧景 2024-09-12 20:14:00

您应该在模型中对两个表进行联接,并创建一个结果,稍后您将通过控制器将其移动到视图中。

请记住不要将查询放入控制器中。

也许是这样的:

function get_data($cue_sheets){
$this->db->select(clips.*);
$this->db->from('clips');
$this->db->join('cue_sheets', 'clips.idcuesheet = cue_sheets.idcuesheet' );
$this->db->where('cue_sheets.idcuesheet', $cue_sheets);
$query = $this->db->get();
}

或者类似的东西。

然后你把它带到控制器

$data['clips'] = $this->clips->get_data($cue_sheets);

,最后带到你的视图

foreach($clips as $clip){
echo $clip->clip_name;
}

希望它有帮助

You should do a join in your model for both tables, and create one result which you will move later to your view trough the controller.

Remember not to put queries in your controller.

maybe somthing like this:

function get_data($cue_sheets){
$this->db->select(clips.*);
$this->db->from('clips');
$this->db->join('cue_sheets', 'clips.idcuesheet = cue_sheets.idcuesheet' );
$this->db->where('cue_sheets.idcuesheet', $cue_sheets);
$query = $this->db->get();
}

Or something similar.

Then you bring it to the controller

$data['clips'] = $this->clips->get_data($cue_sheets);

and finally to your view

foreach($clips as $clip){
echo $clip->clip_name;
}

Hope it helps

娇纵 2024-09-12 20:14:00

据我所知,没有真正的方法可以将数据从视图传递回控制器。我自己从来没有觉得有必要这样做。您的控制器应该收集数据,然后将其传递给视图。如果您需要从数据库结果中获取其他数据,请先执行此操作,然后将其与原始数据一起传递到您的视图。

也许我不明白你的问题...

As far as I know, there's not really a way to pass data back to the controller from the view. I myself have not ever felt the need to. Your controller should be gathering the data and then passing it to the view. If you have additional data to get from your database results, do that first and then pass it to your view along with the original data.

Maybe I'm not understanding your question though...

陌伤浅笑 2024-09-12 20:14:00

我不确定我理解你,但我认为你想从第一个数据库调用给出的所有“表”中获取所有“剪辑”。也许以下可能有效:

$this->db->from('clips');
$this->db->where_in('sheet_id', array_map( function($sheet) { return $sheet['id'] }, $data['get_cue_sheets']->result_array() ) );
$data['get_clips'] = $this->db->get();

I'm not fully certain I understand you, but I think you want to get all "clips" from all "sheets" given by the first db call. Perhaps following might work:

$this->db->from('clips');
$this->db->where_in('sheet_id', array_map( function($sheet) { return $sheet['id'] }, $data['get_cue_sheets']->result_array() ) );
$data['get_clips'] = $this->db->get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文