使用CodeIgniter是一个糟糕的做法,在循环中加载视图 [英] Using CodeIgniter is it bad practice to load a view in a loop

查看:87
本文介绍了使用CodeIgniter是一个糟糕的做法,在循环中加载视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用CodeIgniter,我想知道如果我把这样的代码放在一个循环中,它会减慢速度。

I just got started with CodeIgniter and am wondering will it slow things down if I put code like this in a loop.

    $data['title'] = 'the title';
    $data['content'] = 'blah blah blah';
    $this->load->view('result', $data);



我不完全确定CodeIgniter如何处理事物,或PHP本身。例如,如果我这样做。

I'm not entirely sure how CodeIgniter handles things, or PHP itself for that matter. For example if I did this. Would the file be read on each iteration?

    $data['title'] = 'the title';
    $data['content'] = 'blah blah blah';
    include 'result.php';

也可以在循环中加载控件或者我缺少一些基本的东西,一个循环?感谢。

Also is it ok to load controls in a loop or am I missing something fundamental by putting a control in a loop? Thanks.

其他信息。
我有用户的配置文件的搜索结果...,我正在想一个苗条的版本的配置文件视图显示在搜索结果页上。

Additional info. I have search results for users' profiles... and I was thinking of making a slimmed down version of the profile view to display on the search results page. Would this be bad practice to use a view for that?

推荐答案

我不建议从你的视图调用你的模型。这不是最好的做法,当试图保持MVC框架标准。从您的控制器调用模型,并将用户数组作为$ data的一部分传递到视图。现在,您可以访问$ users数组作为视图中的一个变量。类似于你所拥有的,但是这使得模型能够访问控制器。

I would not recommend calling your model from your view. This is not best practice when trying to keep to MVC framework standards. Call the model from your controller and pass the "users" array into the view as part of $data. Now you access the $users array just as a variable in the view. Similar to what you had, but this gets the access to the model back into the controller.

控制器

$data['title'] = 'the title';
$data['content'] = 'blah blah blah';
$data['users'] = $this->user_model->get_users();
$this->load->view('result', $data);

查看

<?php foreach ($users as $user) {
    echo '<p>' . $user->first_name . '</p>';
    echo '<p>' . $user->last_name . '</p>';
}?>

这篇关于使用CodeIgniter是一个糟糕的做法,在循环中加载视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆