2012-06-12 42 views
0

原諒我的即將到來的問題,哈哈。 (在此非常新)codeigniter寧靜的網絡服務+數據庫查詢

我已閱讀​​。

我已經通過編輯autoload.php文件設置了自動連接到數據庫。

我正在創建自己的控制器,名爲dsapi.php(代碼如下),我想要ad_get()函數從我的簡單數據庫中檢索所有數據,以便我可以在我開發的android應用程序中使用它。

我看了一下 this guidelines page並試圖寫一些東西,但即時通訊很漂亮,正如你所看到的。我甚至接近我想要的嗎?

class dsapi extends REST_Controller { 

    function ad_get() 
    { 
     **$query = $this->db->query('SELECT index, title, detailed FROM ads'); 

     foreach ($query->result_array() as $row) 
     { 
      echo $row['index']; 
      echo $row['title']; 
      echo $row['detailed']; 
     } 

     $this->response($query, 200);** 
    } 

    function ad_put() 
    { 
     // create a new user and respond with a status/errors 
    } 

    function ad_post() 
    { 
     // update an existing user and respond with a status/errors 
    } 

    function ad_delete() 
    { 
     // delete a user and respond with a status/errors 
    } 
} 
+0

你有什麼想做的事:'echo'值或歸還?你的'ad_get()'這樣做:1.檢索數據,2.回顯數據,3.返回*查詢資源*(不是數據!)作爲響應。我認爲你需要[單獨命令和查詢](http://en.wikipedia.org/wiki/Command-query_separation) – scriptin

回答

0

video tutorial description of model 和代碼示例

class Blogmodel extends CI_Model 
{ 
var $title = ''; 
var $content = ''; 
var $date = ''; 

function __construct() 
{ 
    // Call the Model constructor 
    parent::__construct(); 
} 

function get_last_ten_entries() 
{ 
    $query = $this->db->get('entries', 10); 
    return $query->result(); 
} 

function insert_entry() 
{ 
    $this->title = $_POST['title']; // please read the below note 
    $this->content = $_POST['content']; 
    $this->date = time(); 

    $this->db->insert('entries', $this); 
} 

function update_entry() 
{ 
    $this->title = $_POST['title']; 
    $this->content = $_POST['content']; 
    $this->date = time(); 

    $this->db->update('entries', $this, array('id' => $_POST['id'])); 
}} 
+0

非常感謝你的朋友。當然,我多麼愚蠢,我錯過了一個Model:D。 – uaeHamed