我使用的是一个脚本(applications/view/pages/home.php),它具有AJAX请求,通过它获取并显示另一个脚本的内容(我们将称之为scheduler.php)。调度程序文件只是一个包含动态修改的html的文件,该文件基于从AJAX请求传递给它的$_GET参数。
我的问题是,这个动态内容来自数据库,而且由于scheduler.php是由AJAX调用的,所以它没有继承$this->db->能力。
我得到了错误:Fatal error: Using $this when not in object context。
我怎么才能解决这个问题?我是CodeIgniter和AJAX的新手。谢谢!
编辑: Scheduler.php代码:
<?php
$shift = $_GET['shift'];
?>
<table>
<tr>
<th><?php echo $d->format('l') . '<br>' . $d->format('F jS'); ?></th>
</tr>
<tr>
<td>
<?php
$this->db->select('id', 'event', 'time');
$query = $this->db->get('myTable', $shift, $shift-5);
foreach ($query->result() as $row) {
echo "<a href='/schedule/{$row['id']}'>{$row['event']} at {$row['time']}</a><br>";
}
</td>
</tr>
</table>发布于 2014-01-25 18:33:15
根据注释中的讨论,scheduler.php不是控制器或库。
所以你在CI项目之外打电话。只有通过CI index.php文件进行处理,才能使用CI DB函数。
因此,只需将scheduler.php作为控制器,如下所示:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Scheduler extends CI_Controller {
public function index($shift = "")
{
// add your stuff here to response to ajax request.
}
}
?>然后将ajax url更改为:domain.com/index.php/scheduler/index/{shift_value_here}
发布于 2014-01-25 17:50:04
您可以通过CodeIgniter访问$CI =& get_instance();“超级对象”
在此之后,将$this替换为$CI中的scheduler.php,您将可以访问框架库和函数等。
阅读文档中的利用图书馆中的CodeIgniter资源部分。
https://stackoverflow.com/questions/21353980
复制相似问题