2016-07-26 5 views
0

私はmycontrollerという名前のコントローラを持っていますが、index()はビューをロードし、ビューはそのアクションを他の関数呼び出しプロセス)。 フォームが送信されるときに、process()の変数が新しい値でコントローラーを再度更新して再ロードしたいとき。あなたがメッセージを表示するために、process()リロード(redirectへ) "mycontroller /インデックス" を持っている必要があります変数を関数から他の関数​​に呼び出す方法と、変数の新しい文字列をコントローラに再ロードする

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

。しかし、サーバーは前のページ要求を "記憶していない"ため、リダイレクト中に$Myvarの値が失われます。

サーバーリクエスト間でデータを渡すには、セッションを使用する必要があります。

sessionライブラリには、いくつかの設定がファイルに設定されている必要がありアプリケーション/設定/ 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 
    } 

} 
関連する問題