简体   繁体   English

Codeigniter:将数据从控制器传递到视图

[英]Codeigniter: Passing data from controller to view

I want to pass $data from the controller named poll to the results_view however I am getting an undefined variable error.我想将$data从名为poll的控制器传递给results_view但是我收到了一个未定义的变量错误。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Poll extends CI_Controller {

    public function __construct()
       {
            parent::__construct();
            $this->load->database();
            $this->load->helper('form');
       }

    public function index()
    {

        $this->load->view('poll_view',$data);
    }

    public function vote()
    {
        echo "Voting Successfull";
        $this->db->insert('votes',$_POST);
    }

    public function results()
    {
        echo "These are the results";
        //$query = $this->db->get('votes');
        $data = "hello";
        $this->load->view('results_view', $data);

    }
}

Results_view.php结果_view.php

<html>
<?php echo $data; ?>
</html>

$data should be an array or an object: http://codeigniter.com/user_guide/general/views.html $data应该是一个数组或一个对象: http : //codeigniter.com/user_guide/general/views.html

$data = array(
    'title' => 'My Title',
    'heading' => 'My Heading',
    'message' => 'My Message'
);

$this->load->view('results_view', $data);

results_view.php结果视图.php

<html>
<?php 
//Access them like so
echo $title.$heading.$message; ?>
</html>

In simple terms,简单来说,

$data['a'] in controller becomes $a in your view.在您看来,控制器中的$data['a']变为$a ( $data won't exist in your view, only the index will become available) (您的视图中将不存在$data ,只有索引可用)

eg例如

Controller:    
$data['hello']='hellow world';

view:
echo $hello;

You just need to create a array, you using codeigniter right?你只需要创建一个数组,你使用 codeigniter 对吗?

Example on controller:控制器示例:

$data['hello'] = "Hello, world";
$this->load->view('results_view', $data);

In de page "results_view" you just have to:在页面“results_view”中,您只需:

<?php echo $hello;?>

Obs: You can create n datas, just pay attention in the name and make it a array. Obs:可以创建n个数据,注意名字,做成数组即可。

Obs²: To use the data use the key of the array with a echo. Obs²:要使用数据,请使用带有回声的数组的键。

The view wouldn't call the data 'data'视图不会将数据称为“数据”

The controller would include an associative index (not sure if that's correct nomenclature) for data eg 'stuff' looking thus $data['stuff']控制器将包含数据的关联索引(不确定这是否是正确的命名法),例如 'stuff' 看起来如此$data['stuff']

You'd echo in the view so: echo $stuff;你会echo的视图,以便: echo $stuff; not echo $data;echo $data;

I am av lowly code fiddler but do really like CodeIgniter so excuse me if i've got this arse about tit.我是一个卑微的代码提琴手,但我真的很喜欢 CodeIgniter,所以如果我对山雀有这种想法,请原谅我。

One more thing - surely your constructor function is a bit of a waste.还有一件事 - 当然你的构造函数有点浪费。 All that loading of libraries and helpers is done with the autoload file.所有库和帮助程序的加载都是通过自动加载文件完成的。

You can create property $data = [] ;您可以创建属性$data = [] ; inside CI_Controller(path: system/core/Controller.php) and store all data to show in view.在 CI_Controller(path: system/core/Controller.php) 中并存储所有数据以显示在视图中。 U can load common data like languages, menu, etc in CI_Controller.您可以在 CI_Controller 中加载常用数据,如语言、菜单等。 Also u can add special data for view in controller.您也可以在控制器中为视图添加特殊数据。 (example: $this->data['message'] = "Hello world"; ) Finally, u can pass $this->data to view when load view (example: $this->load->view('view_name',$this->data); ) (例如: $this->data['message'] = "Hello world"; ) 最后,你可以在加载视图时传递$this->data来查看 (例如: $this->load->view('view_name',$this->data); )

I hope this will help you我希望这能帮到您

you can do it this way你可以这样做

defined array in controller控制器中定义的数组

$data['hello'] = "hello";

and pass variable to view并传递变量以查看

echo $hello; 

If you pass如果你通过

$data = your code
$this->load->view('your-page', $data);

and get data on your view as并获取有关您的视图的数据

<?php echo $data;?>

It won't work because ci didn't understand this patern.这是行不通的,因为 ci 不理解这种模式。 If like to pass value form controller to view so you can try this -如果想传递值表单控制器来查看,那么你可以试试这个 -

controller -控制器 -

$data['any-name'] = your values;
$this->load->view('your-page', $data);

then in your view you can get this data by -那么在您看来,您可以通过以下方式获取此数据 -

<?php echo $any-name;?>

Hope this helps you.希望这对你有帮助。

In your controller you can pass在您的控制器中,您可以通过

$data['poll'] = "Your results";

In your view you can call在您看来,您可以调用

echo $poll; 

Ok so I finally solved it.好的,所以我终于解决了。 You should really have a model (it helps a lot)你真的应该有一个模型(它有很大帮助)

In your model do something like在你的模型中做类似的事情

Model模型

class poll_model extends CI_MODEL {

 function __construct() {
   $this-load->database(); 
 }

 function get_poll {
   $this->db->query("SELECT * FROM table");
   $row = $query->row();

   $obj = array(
    'id' => $row->id
  );
   return $obj;
 }
}

Now if you have more than an id say name of poll # you can add in array.现在,如果您有多个 id 说 poll # 的名称,您可以添加到数组中。 Now in your controller do现在在你的控制器中

class Poll extends CI_Controller {

public function __construct()
   {
        parent::__construct();
        $this->load->database();
        $this->load->helper('form');
        $this->load->model('poll_model');
   }

public function index()
{
    $data["a"] = $this->poll_model->get_poll();
    $this->load->view('poll_view',$data);
}

And finally in VIEW put最后在VIEW 中放入

<? echo $a["id"]; ?>

This is a big help.这是一个很大的帮助。 I figured it out by testing and it works for me.我通过测试弄清楚了,它对我有用。

In controller:在控制器中:

$data["result"] = $this->login_model->get_login(); // Get array value from DB..

$this->load->view('login-form',$data); // Pass the array to view 

In view:鉴于:

print_r($result);  // print the array in view file

I have seen all above answer so here is what I do when I have to load the data from the controller to my view.我已经看到了以上所有答案,所以当我必须将数据从控制器加载到我的视图时,我会这样做。 To Pass the Data To the view from the controller:将数据从控制器传递到视图:

public function your_controller(){

   // Your Necessary Code 
   // You have the $data, $data2, $data3 to post to the view.

   $this->load->view('your_view_directory or view_page',['data'=>$data, 'data2'=>$data2, 'data3'=>$data3... so on ]);

}

And At the View Side You can simply retrieve that data: To Display You can simply use echo , print , print_r .在视图端,您可以简单地检索该数据: 显示您可以简单地使用echoprintprint_r And if you want to loop over it, you can do that as well.如果你想循环它,你也可以这样做。

In controller:在控制器中:

public function product(){公共功能产品(){

$data = array("title" => "Books", "status"=>"Read","author":"arshad","company":"3esofttech",

"subject":"computer science"); “学科”:“计算机科学”);

Data From Model to controller从模型到控制器的数据

$this->load->model('bookModel');
$result = $this->bookModel->getMoreDetailsOfBook();

**Add *$result* from model to *$data* array**  
$data['tableRows'] = $result;

$data from Controller to View $data 从控制器到视图

$this->load->view('admin/head',$data);

And to access in view file views/user.php并在视图文件views/user.php 中访问

<?php  echo $data;
 foreach($tableRows as $row){ echo
 $row['startData']; } ?>

Instead of代替

$data = "hello";
$this->load->view('results_view', $data);

Do

$data['hello'] = 'hello';
$this->load->view('results_view', $data);

In your controller file and controller will send data having hello as string to results_view and in your view file you can simply access by在您的控制器文件中,控制器会将hello作为字符串的数据发送到 results_view,在您的视图文件中,您可以简单地访问

echo $hello;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM