请指导如何在codeigniter中编写此代码

时间:2015-11-06 05:33:42

标签: php mysql codeigniter

我有一个名为Questions的表2000 questions,我使用MySql数据库和Codeigniter,我想要的是select 10 PERCENT questions of total 2000 with a query in笨

1 个答案:

答案 0 :(得分:1)

在控制器中

public function FunctionName()
{
    $result = $this>model_name->get_question();
    print_r($result);
}

在模型中

public function get_question()
{
    #get all question count
    $query= $this->db->query("SELECT * FROM question");
    $result = $query->result_array();
    $count = count($result);  # returns 2000

    $limit = ($count/100)*10;  # returns 200

    # select only limited question which set with $limit
    $query= $this->db->query("SELECT * FROM question LIMIT $limit");
    $result = $query->result_array();
    return $result
}