[英]Codeigniter sending data from controller to view
I'm trying to pass data from controller to view in Codeigniter.我正在尝试从 controller 传递数据以在 Codeigniter 中查看。
Report.php
(Controller) Report.php
(控制器)
<?php defined('BASEPATH') OR exit('No direct script access allowed');
header('Access-Control-Allow-Origin: *');
class Report extends Public_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view("templates/dashboard", $data);
}
}
dashboard.php
(View file located in view/templates
) dashboard.php
(查看文件位于view/templates
中)
<div class="row">
<?php
echo $title;
?>
</div>
Those codes have executed an error这些代码执行了错误
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: title
Filename: templates/dashboard.php
Line Number: 4
I need to load title in the dashboard.php我需要在仪表板中加载标题。php
I have tried lots of deferent things to fix this.我已经尝试了很多不同的方法来解决这个问题。 I have no idea why this keeps happening, but finally, I fixed my problem using ajax
我不知道为什么这种情况一直发生,但最后,我使用 ajax 解决了我的问题
$(document).ready(function(){
$.ajax({
url:'<?php echo base_url('admin/report/index'); ?>',
success:function(data){
$('#same').html(data);
},
error: function(){
alert('Error');
}
});
use this syntex to define title and other variables.使用这个语法来定义标题和其他变量。 thanks
谢谢
$this->data['title'] ='My Title';
$this->data['heading'] ='My Heading';
$this->data['message'] ='My Message';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.