-- ----------------------------
-- Records of article_type
-- ----------------------------
INSERT INTO article_type VALUES ('1', '生活');
INSERT INTO article_type VALUES ('2', '科技');
=======CI============
model: article_type.php
<?php
class article_type extends CI_Model{
function article_type(){
parent::__construct();
}
function getAllArticleType(){
$this->db->select('_id,_name');
$query = $this->db->get('article_type');
$data=array();
$rearr=array();
if($query->num_rows()>0){
foreach($query->result() as $row){
$data['id']=$row->_id;
$data['name']=$row->_name;
array_push($rearr,$data);
}
}
return $rearr;
}
}
?>
controller:article.php
<?php
class article extends CI_Controller{
function article(){
parent::__construct();
$this->load->helper("url");
}
function data_article_type() {
$this->load->model('article_type');
$data = $this->article_type->getAllArticleType();
echo json_encode($data);
}
}
?>
到这步 访问 http://10.6.0.117/index.php/article/data_article_type
能够得到ci框架返回的json字符串: [{"id":"1","name":"\u751f\u6d3b"},{"id":"2","name":"\u79d1\u6280"}]