2014-01-25 86 views
1

我使用的腳本(applications/view/pages/home.php)由此獲得並顯示另一個腳本的內容其中有一個AJAX請求使用的數據庫連接(我們把它叫做scheduler.php)。調度程序文件只是一個包含基於從AJAX請求傳遞給它的$ _GET參數動態修改的html的文件。笨 - 用AJAX請求

我的問題是,這個動態內容來自數據庫,並且由於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> 
+0

您scheduler.php可能不是一個控制器。 –

+0

你可以添加你的scheduler.php代碼嗎? –

+0

這不是一個控制器,不。它應該是?再次,我是CodeIgniter的新手(我的經驗僅僅基於用戶指南,大約一週的工作)。 – Mephoros

回答

1

按在評論中討論,scheduler.php沒有控制器或庫。

所以,你所呼叫的CI項目之外。您不能使用CI DB功能,直到您通過CI index.php文件處理。

因此,只要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網址:domain.com/index.php/scheduler/index/{shift_value_here}

+0

這工作完美!謝謝。肯定覺得我現在更瞭解CodeIgniter。 – Mephoros