CodeIgniter基本インタフェースapi


1.全表クエリー
public function query()
{
  $sql = "    ";
  $query = $this->db->query($sql);
  $this->output->set_output(json_encode($query->result()));
}

2.パラメータ付きクエリー(getリクエスト)
public function queryByParam($param)
{
  $sql = "     where   ={$param}";
  $query = $this->db->query($sql);
  $this->output->set_output(json_encode($query->result()));
}

3.パラメータ付きクエリー(post要求)
public function detail()
{
  $jsonStr = $this->input->raw_input_stream;
  $jsonObj = json_decode($jsonStr); 
  $param = $jsonObj->id;
  $sql = "     from    where    = '{$param}' ";
  $query = $this->db->query($sql);
  $resData = $query->result();
  $this->output->set_output(json_encode($resData));
}

4.ページングクエリ
public function paginate()
{
  $jsonStr = $this->input->raw_input_stream;
  $jsonObj = json_decode($jsonStr); 
  $page = $jsonObj->page;    //$page=1
  $limit = $jsonObj->limit;  //$limit=2
  if($page < 1 )
  {
     $page = 1;
  }
  $tmp = ($page-1)*$limit;
  $sql = "     from    limit {$tmp},{$limit}";
  $query = $this->db->query($sql);
  $this->output->set_output(json_encode($query->result()));
}

5.保存
public function save()
{
  $jsonStr = $this->input->raw_input_stream;
  $jsonObj = json_decode($jsonStr);
  $params = array(
    'id' => $jsonObj->id,
    'name' => $jsonObj->title,
    'content' => $jsonObj->content
  );
  $this->db->insert('  ',$params);
  $this->output->set_output($this->db->affected_rows());
}