用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2290|回复: 6
收起左侧

[HELP] 数据库连接

[复制链接]
发表于 2012-2-9 00:25:04 | 显示全部楼层 |阅读模式
本帖最后由 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') );模型怎么定义才能符合下面的数据格式  谢谢
发表于 2012-2-9 11:21:26 | 显示全部楼层
没太看懂你的需求,呵呵
 楼主| 发表于 2012-2-9 19:18:46 | 显示全部楼层
Hex 发表于 2012-2-9 11:21
没太看懂你的需求,呵呵

我是想要从数据库传入数据,和下面的一样 返回数组
$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'))),
                );不知道可不可以
发表于 2012-2-9 22:09:16 | 显示全部楼层
肯定是可以啊,你到底是哪里有问题呢?
 楼主| 发表于 2012-2-10 01:40:52 | 显示全部楼层
Hex 发表于 2012-2-9 22:09
肯定是可以啊,你到底是哪里有问题呢?

我的数据库里面定义id,name,email这几个变量然后传入模型,这几个变量会根据我选择的请求格式返回数据,我不太明白这里的模型怎么写
发表于 2012-2-10 09:16:38 | 显示全部楼层
nordwest 发表于 2012-2-10 01:40
我的数据库里面定义id,name,email这几个变量然后传入模型,这几个变量会根据我选择的请求格式返回数据, ...

你这句话实在是很有问题啊。。。
你是不是要返回以user_id不下标的数组,方便你取用户数据的时候直接$users[$user_id]这样?那你取出数据循环加工的时候就把数组下标写成你要的值。如果你用的是ar的话,得在result_array()方法后再循环一次了。
发表于 2012-2-10 11:55:51 | 显示全部楼层
nordwest 发表于 2012-2-10 01:40
我的数据库里面定义id,name,email这几个变量然后传入模型,这几个变量会根据我选择的请求格式返回数据, ...

数据库里怎么会有变量呢?是字段吧?

本版积分规则