我在codeigniter构造函数中有以下内容:
$navbar= new stdClass();
$navbar->user_email = $this->user_email;
$navbar->vp = $this->vp;
当我尝试在索引函数中访问它时:
public function index() {
var_dump($this->navbar);
这有效。
我现在尝试将$ this-> navbar传递给视图:
$this->load->view('buyers/navbar', $this->navbardata);
在视图中我有
<?php echo 'in nav ';var_dump($this->navbar); exit; ?>
我明白了:
Message: Undefined property: MY_Loader::$navbar
我该如何做到这一点?
提前致谢,
答案 0 :(得分:11)
试试这个
$data['navbar'] = $this->navbardata;
$this->load->view('buyers/navbar', $data);
并在您的视图中尝试
<?php echo 'in nav ';var_dump($navbar); exit; ?>
答案 1 :(得分:0)
首先,创建一个像这样的控制器
class Test extends CI_Controller
{
public function index()
{
$data['topics'] = "Views Testing";
$data['title'] = "Welcome To My Blog";
$data['myData'] = array("name" => "John Doe", "email" => "johndoe2020@gmail.com", "designation" => "CodeIgniter Developer");
$data['myObject'] = new MyClass("John Doe", "johndoe@gmail.com");
$this->load->view("blogview", array($data, "MySelf" => new MyClass("Doe John", "doejohn460@gmail.com"), "developer" => "Life at CodeIgniter Academy"));
}
}
class MyClass
{
public $name;
public $email;
public function __construct($name, $email)
{
$this->name = $name;
$this->email = $email;
}
}
现在在views文件夹中创建blogview.php文件 并添加以下代码以访问从控制器传递的数据
echo "<h3>Variables :</h3> ";
echo var_dump($title);
echo $title;
echo var_dump($topics);
echo $topics;
echo "<h3>Array :</h3> ";
echo var_dump($myData);
print_r($myData);
echo "<br><br>";
echo $myData['name'];
echo "<br>";
echo $myData['email'];
echo "<br>";
echo $myData['designation'];
echo "<br> <br>";
foreach ($myData as $data)
{
echo $data."<br>";
}
echo "<h3>Object </h3>";
echo var_dump($myObject);
echo "<br>";
echo $myObject->name;
echo "<br>";
echo $myObject->email;
echo "<br><br>";
echo $MySelf->name;
echo "<br>";
echo $MySelf->email;
echo "<br><br>";
foreach($myObject as $Object)
{
echo $Object."<br>";
}
echo "<h3>Array key:Value Pair</h3>";
echo $developer;
我希望这会有所帮助。