php - CodeIgniter $this->load->vars() 函数

标签 php codeigniter

我想知道 $this->load->vars() 在 CodeIgniter 中是如何工作的。文档对此相当模糊。

我有以下代码:

    $init = $this->init->set();

    $this->load->view('include/header', $init);
    $this->load->view('include/nav');

    $dates = $this->planner_model->create_date_list();
    $this->load->view('planner/dates_content', $dates);

    $detail = $this->planner_model->create_detail_list();
    $this->load->view('planner/detail_content', $detail);


    $this->load->view('include/footer');

但是,我的 detail_content View 中还需要 $dates 数组。我试图用 $this->load->vars() 加载它并希望它会附加到 $detail 数组,因为 CI 文档说明如下:

You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.

如果我执行 $detail['dates'] = $dates; 会有效吗?它会将 $dates 数组附加到 $detail['dates'] 吗?

提前致谢。

最佳答案

$this->load->vars() 非常适合这个目的。试试这个:

$init = $this->init->set();// Won't be passed to the next 2 views
$this->load->view('include/header', $init);
$this->load->view('include/nav');

$dates = $this->planner_model->create_date_list();
$this->load->vars($dates);
$this->load->view('planner/dates_content');

$detail = $this->planner_model->create_detail_list();
$this->load->vars($detail);
$this->load->view('planner/detail_content');

我觉得奇怪的是,通常您将关联数组作为数据传递,例如 $data['my_var_name'] = $var_value,所以我假设您的模型调用返回的是已经结构化的数据使用您将在您的 View 中使用的变量名称(数组键),我觉得这很奇怪,但我对您的应用程序一无所知。

这是一个更“传统”的版本:

$data['dates'] = $this->planner_model->create_date_list();
$this->load->view('planner/dates_content', $data);

$data['detail'] = $this->planner_model->create_detail_list();
// receives both dates and detail
$this->load->view('planner/detail_content', $data);

关于php - CodeIgniter $this->load->vars() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5770419/

相关文章:

mysql - Codeigniter 加载页面非常慢

forms - CI - 忘记密码表单验证以检查数据库中是否存在电子邮件地址

php - 如何测试地理位置标识符功能?

php - 具有多个用户位置的 Google map Android 应用

php - 将搜索结果分配给变量 - PHP Fulltext Search Boolean

mysql - Codeigniter mysql 哪里不等于查询

Codeigniter 3 与 redis 的 session - AUTH 密码

php - 如何集中重复使用但每次使用不同分区的复杂窗口查询

带有端口号的数组中的php mysql数据库连接

http - PHP_SELF vs PATH_INFO vs SCRIPT_NAME vs REQUEST_URI