在CodeIgniter中如何在加载的视图之间传递数据

时间:2014-08-06 13:53:30

标签: php codeigniter

假设我们在同一个类方法中加载两个或多个视图,如下所示:

$this->load->view('header');
$this->load->view('body');
$this->load->view('footer');

您决定在 head 视图( $ cat_name )中创建一个变量,如下例所示:

<?php
foreach ($categories as $key => $value) {
    $selected = FALSE;
    if ($this->router->class == 'category' && $this->router->method == 'id' && $this->uri->segment(3) == $value->id) {
        $cat_name = $value->name;
        $selected = TRUE;
    }
 ?>
    <option value="<?= $value->id; ?>" <?= ($selected ? 'selected' : ''); ?>><?= $value->name; ?></option>
<?php } ?>

这需要一个循环来获取该变量。

我想将该变量( $ cat_name )传递给下一个视图而不重做循环,这只是一种浪费。

我想要实现的目标是最大限度地减少循环次数。

3 个答案:

答案 0 :(得分:1)

而不是加载控制器中的所有内容,在视图中加载它 创建新文件,假设模板

 $this->load->view('template',$variable);

并在您的模板中

//do the loop here

$this->load->view('header');
$this->load->view('body');
$this->load->view('footer')

答案 1 :(得分:-1)

你可以尝试这样:

创建一个类似的模型:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Custom_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    function printSelect(){
        // Do some logic and looping here
        $html = "<select><option>...</option></select>";
        return $html
    }
}

然后从您的视图中调用该模型,就像这样..

$这 - &GT; custom_model-&GT; printSelect();

请记住先加载模型。 $这 - &GT;负载&GT;模型(&#39;路径/到/你的/模型/文件夹/ custom_model&#39);

这样,每当您想要打印时,您只需从您的视图中调用该方法即可。

我希望这会有所帮助。

答案 2 :(得分:-2)

如果要在项目中保存MVC,则需要创建用于生成选择的模型。我知道,这看起来很奇怪,但在

之后它会帮助你很多次

<强>控制器

// load model
$this->load->model('myselect_model');
// get array with marked element
$select_data = $this->myselect_model->setSelected($categories, $this->router->class, $this->router->method, $this->uri->segment(3));
// get filled html
$my_html_select = $this->load->view('select_tpl',array('select'=>$select_data),TRUE);
// use it at any controller
$this->load->vars(array('my_select'=>$my_html_select));

// some views
$this->load->view('header');
$this->load->view('body');
$this->load->view('footer');

型号&#39; myselect_model&#39;

function setSelected($items, $uri_controller,$uri_method, $uri_value){
       // createing temp array for our list
       $tmp = array();
       foreach($items as $key=>$value){
             // appending item
             $tmp['options'][$key] = $value;
             if ($uri_controller == 'category' && $uri_method == 'id' && $uri_value == $value->id) 
             {
                   // saving selected for any reason to use after
                   $tmp['selected'] = array('name'=>$value->name, 'id'=>$value->id);
                   // marking this item as selected
                   $tmp['options'][$key]['selected'] = TRUE;
             }
       }
       // returning completed array
       return $tmp;
}

查看&#39; select_tpl&#39;

<?php if(!empty($select)){?>
     <select>
     <?php foreach($select['options'] as $option){?>
             <option value="<?=$option->id?>"<?=(isset($option['selected']) && $option['selected']==TRUE ? " selected=\"selected\"" : "")?>><?=$option->name?></option>
     <?php }
     </select>
<?php } ?>

<强>视图/报头

<body><?=$my_select?><i>template here</i>

<强>视图/体

<p>some html here and our select goes here-> <?=$my_select?></p>