我在我的视图中创建了一个函数,并尝试在php函数中进行查询,但我不知道我得到了并且在我的整个职业生涯中从未见过的意外错误都可以帮助我摆脱从这个错误?
Severity: Error --> Using $this when not in object context /application/views/includes/create_calendar.php 51
这是我的控制器
class Booking extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array("form", "url"));
$this->load->library('session');
$this->load->database();
}
public function get_calendar() {
$this->load->view('includes/create_calendar');
}
}
这是我尝试运行查询的文件views / includes / create_calendar.php
function getCalender($year = '',$month = '') {
$sql = $this->db->get('calendar');
if($day == $sql->row()->day_off) { $per_off = $sql->row()->per_off; }
echo "<p>".$day." Off</p>";
}
答案 0 :(得分:1)
在CodeIgniter用法中,如果需要使用&#34;超级对象&#34;中可用的其中一个类,则必须先获取超级对象的实例:
window.alert()
甚至可能无法加载此DB类。如果是这种情况,那么在使用db:
之前$CI =& get_instance();
$CI->db->get('calendar');
澄清一下,一旦你进入该功能,你就已经从变量范围中删除了$ this,就像这个简单的测试一样:
$CI->load->database();
您可以执行以下操作,但这非常麻烦:
class Test {
public $foo = 'bar';
public function index() {
function baz(){
return $this->foo;
}
echo baz();
}
}
$t = new Test;
$t->index();
你要做的最好的事情是找到另一种更清洁的方式来做你正在做的事情。您通常不希望在视图中调用数据库和函数。这违背了MVC的全部内容。