<?php
class mycontroller extends CI_Controller{
$Myvar=null;
function index(){
$myarry=array('VariableToView'=>$this->$Myvar);
$this->load->view('myview',$myarry)
}
}
function process(){
$this->$myvar="Thanks";
}
?>
答案 0 :(得分:0)
您需要process()
重新加载(redirect
)“mycontroller / index”才能显示消息。但由于服务器不“记住”任何先前的页面请求,因此在重定向期间$Myvar
的值将丢失。
要在服务器请求之间传递数据,您需要使用会话。
session
库需要在文件 application / config / config.php 中设置多个首选项 - 请参阅您正在使用的Codeigniter版本的文档,以了解必须使用的内容完成。
<?php
class Mycontroller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('session');
}
function index()
{
//get session data.
$Myvar = $this->session->userdata('message');
//If there is no session data named "message" then $Myvar will be "empty"
if(empty($Myvar))
{
$Myvar = ""; //use an empty string
}
else
{
//the session has a "message" in userdata,
//remove that data so it won't be used the next time the page is loaded
$this->session->unset_userdata('message');
}
$this->load->view('myview', array('VariableToView' => $Myvar));
}
function process()
{
//add userdata to session and redirect to mycontroller/index
$data = array("message" => "Thanks");
$this->session->set_userdata($data);
redirect("mycontroller"); //will load mycontroller/index
}
}