首先,创建一个这样的控制器
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;
我希望这将有所帮助。