5

我的 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    

我怎样才能使这项工作?

提前致谢,

4

2 回答 2

11

像这样试试

$data['navbar'] = $this->navbardata;
$this->load->view('buyers/navbar', $data);

在你看来尝试像

<?php echo 'in nav ';var_dump($navbar); exit; ?>
于 2013-06-04T04:29:50.677 回答
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;
    }
}

现在在视图文件夹中创建您的 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;

我希望这将有所帮助。

于 2019-11-29T10:56:50.893 回答