gpt4 book ai didi

php - Codeigniter:将数据从 Controller 传递到 View

转载 作者:IT王子 更新时间:2023-10-29 01:11:20 25 4
gpt4 key购买 nike

我想将 $data 从名为 poll 的 Controller 传递到 results_view 但是我收到一个 undefined variable 错误。

<?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);

}
}

Result_view.php

<html>
<?php echo $data; ?>
</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

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

关于php - Codeigniter:将数据从 Controller 传递到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9446700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com