数据库连接
本帖最后由 nordwest 于 2012-2-9 00:26 编辑class Example extends REST_Controller
{
function user_get()
{
if(!$this->get('id'))
//used to return GET variables from either a query string like this index.php/example_api/user?id=1 or can be set in the more CodeIgniter’esque way with index.php/example_api/user/id/1.
{
$this->response(NULL, 400);
}
// $user = $this->some_model->getSomething( $this->get('id') );
$users = array(
1 => array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com', 'fact' => 'Loves swimming'),
2 => array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com', 'fact' => 'Has a huge face'),
3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => 'Is a Scott!', array('hobbies' => array('fartings', 'bikes'))),
);
$user = @$users[$this->get('id')]; //
if($user)
{
$this->response($user, 200); // 200 being the HTTP response code
}
else
{
$this->response(array('error' => 'User could not be found'), 404);
}
}
请问这里的$user = $this->some_model->getSomething( $this->get('id') );模型怎么定义才能符合下面的数据格式谢谢
没太看懂你的需求,呵呵 Hex 发表于 2012-2-9 11:21 static/image/common/back.gif
没太看懂你的需求,呵呵
我是想要从数据库传入数据,和下面的一样 返回数组
$users = array(
1 => array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com', 'fact' => 'Loves swimming'),
2 => array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com', 'fact' => 'Has a huge face'),
3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => 'Is a Scott!', array('hobbies' => array('fartings', 'bikes'))),
);不知道可不可以 肯定是可以啊,你到底是哪里有问题呢? Hex 发表于 2012-2-9 22:09 static/image/common/back.gif
肯定是可以啊,你到底是哪里有问题呢?
我的数据库里面定义id,name,email这几个变量然后传入模型,这几个变量会根据我选择的请求格式返回数据,我不太明白这里的模型怎么写 nordwest 发表于 2012-2-10 01:40 static/image/common/back.gif
我的数据库里面定义id,name,email这几个变量然后传入模型,这几个变量会根据我选择的请求格式返回数据, ...
你这句话实在是很有问题啊。。。
你是不是要返回以user_id不下标的数组,方便你取用户数据的时候直接$users[$user_id]这样?那你取出数据循环加工的时候就把数组下标写成你要的值。如果你用的是ar的话,得在result_array()方法后再循环一次了。 nordwest 发表于 2012-2-10 01:40 static/image/common/back.gif
我的数据库里面定义id,name,email这几个变量然后传入模型,这几个变量会根据我选择的请求格式返回数据, ...
数据库里怎么会有变量呢?是字段吧?
页:
[1]