2013-01-31 2 views
3

컨트롤러에서 뷰로 변수를 전달하려고합니다. 몇 가지 코드가 있지만 문제가 무엇인지 이해하기 위해 간단하게 만들었습니다. 여기 내 컨트롤러가 있습니다 :codeigniter 뷰의 변수 값

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

    class Welcome extends CI_Controller { 

     $p=2; 

     public function index() 
     { 
      $this->load->view('welcome_message',$p); 
     } 
    } 

?> 

변수 p가 뷰에 선언되었습니다.

ERROR

Parse error: syntax error, unexpected '$p' (T_VARIABLE), expecting function (T_FUNCTION) in C:\wamp\www\..\application\controllers\welcome.php on line 20 

문제점은 무엇입니까 : 나는 $ 피 값을 표시 할 때

<div id="container"> 
    <h1>Welcome to CodeIgniter!</h1> 
    <?php echo $p?> 
</div> 

, 나는 오류를 얻을?

감사합니다.

답변

0

당신은 당신의 컨트롤러의 생성자에서 $p를 선언해야합니다

class Welcome extends CI_Controller { 

    function __construct() { 
    parent::__construct(); 
     $this->p = 2; 
    } 

    public function index() 
    { 
     $data['p'] = $this->p; 
     $this->load->view('welcome_message',$data); 
    } 
} 

>

3

첫 번째 변수는 배열 (check out the docs)로 전달 될 필요가의?.

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

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

$ (P)는 기능의 범위에서 선언되고, 그래서 어느 한 내용;

public function index() { 
    $p = 2; 
    $this->load->view('welcome_message',array('p' => $p)); 
} 

또는

class Welcome extends CI_Controller { 

public $p=2; 

public function index() 
{ 
    $this->load->view('welcome_message',array('p' => $this->p)); 
} 
} 
+0

예. 이제 작동합니다. PHP는 오류가 심각도가 발생했습니다 : 메시지주의 : 정의되지 않은 속성을 : CI_Loader : $ arrPermisos 파일 이름 : 마케팅/campanas.php 줄 수 내 실제 코드에서 변경 때 나는 다른 오류가 : 6 컨트롤러 코드 :'public function index() \t {$ this-> load-> view ("marketing/campanas", $ this-> arrPermisos); }'보기 코드'

    \t \t \t \t \t \t <(arrPermisos [ "campanas_leer"{> \t \t \t \t \t \t
  • ? 클래스 = "현재"> \t \t \t \t \t \t \t \t \t
Campañas \t \t'무슨 WR의 그거야? – axmug

+0

@axmug는 $ this-> arrPermisos 해시 배열입니까? – Rooneyl

+0

예. 사용자 역할이 저장되는 배열입니다. – axmug