使用 CodeIgniter 将变量从控制器传递到视图

问题描述 投票:0回答:2

我正在尝试用几个小函数构建我的控制器,但我正在努力处理以下代码:

楼宇控制器:

public function index(){
   $data['title'] = 'my title part 1';
   $data['title'] .= 'my title part 2';
   $data = $this->function1();
   $data = $this->function2();
   $data['end'] .= 'end of this block';
   $data['main_content'] = 'buildingView';
   $this->load->view('templates/default',$data); 
}

public function1(){
        $data['block1'] = 'test1';
        $data['blockA'] = 'testA';
        $data['blockB'] = 'testB';
        $data['blockC'] = 'testC';
        return $data;
}

public function2(){
        $data['block2'] = 'test2';
        $data['block3'] = 'test3';
        $data['block4'] = 'test4';
        $data['block5'] = 'test5';
        return $data;
}

建筑景观

echo $title;
echo $block1;
echo $block2;
echo $end;

问题在于 function2 中的

$data
变量覆盖了 function1 中的变量。 如何传递 Index 和两个函数中生成的数据?

我已经尝试了下面的代码,但它也不起作用:

$data[] = $this->function1();
$data[] = $this->function2();

编辑:我编辑了函数 1 和 2,因为它们实际上返回一个由多个变量组成的数组。

php codeigniter controller merging-data
2个回答
1
投票

您正在覆盖 $data 变量。因此,根据您的情况,使用

array_merge()
并将其保留在
$data
变量中。就像下面这样

public function index(){
   $data1 = $this->function1();
   $data2 = $this->function2();
   $data = array_merge($data1,$data2);
   $data['title'] = 'my title part 1';
   $data['title2'] .= 'my title part 2';
   $data['end'] .= 'end of this block';
   $data['main_content'] = 'buildingView';
   $this->load->view('templates/default',$data); 
}

public function1(){
        $data['block1'] = 'test1';
        $data['blockA'] = 'testA';
        $data['blockB'] = 'testB';
        $data['blockC'] = 'testC';
        return $data;
}

public function2(){
        $data['block2'] = 'test2';
        $data['block3'] = 'test3';
        $data['block4'] = 'test4';
        $data['block5'] = 'test5';
        return $data;
}

1
投票
public function index()
{
  $data['title'] = 'my title part 1';
  $data['title'] .= 'my title part 2';
  $data['block1'] = $this->function1();
  $data['block2'] = $this->function2();
  $data['end'] .= 'end of this block';
  $data['main_content'] = 'buildingView';
  $this->load->view('templates/default',$data); 
}

您的函数的结果将在视图中使用变量

$block1
$block2
进行访问。

这些定义更适合演示目的

public function function1()
{
    return 'test1';
}

public function function2()
{
    return 'test2';
}

使用问题代码中定义的辅助函数

public function function1(){
    $data['block1'] = 'test1';
    return $data;
}

并将返回值分配给一个 var,然后将其传递给视图

$data['func1'] = $this->function1();
$this->load->view('templates/default',$data);     

在视图中执行此操作以显示结果

echo $func1['block1'];

将数组传递给视图是非常常见且完全可以接受的。

公共函数 function2(){ $data['block1'] = 'test2'; $data['block2'] = 'test3'; $data['block3'] = 'test4'; $data['block4'] = 'test5'; 返回$数据; }

传递到视图

$data['func2'] = $this->function2();
$this->load->view('templates/default',$data); 

在视图中访问。这次使用迭代器来显示每个项目 在数组中。

foreach($func2 as $item)
{
   echo $item."<br>";
}

或者一次购买一件商品

echo $func2['block1']."<br>";
echo $func2['block2']."<br>";
echo $func2['block3']."<br>";
echo $func2['block4']."<br>";

您也可以将对象实例传递给视图。此版本的

function2()
返回一个对象实例。

  public function function2()
  {
    $data = new stdClass();
    $data->block1 = 'test2';
    $data->block2 = 'test3';
    $data->block3 = 'test4';
    $data->block4 = 'test5';
    return $data;
  }

将函数返回值分配给传递给视图的数组

$data['obj2'] = $this->function2(); 
$this->load->view('templates/default',$data);    

在视图中使用它

echo $obj2->block1;
echo $obj2->block2;
...

迭代器 (

foreach
) 也适用于对象实例。

© www.soinside.com 2019 - 2024. All rights reserved.