在 Codeigniter 中使用 LIMIT 和 OFFSET 进行选择

问题描述 投票:0回答:3

我有一个在 Codeigniter 中开发的网站,在我的模型中我有这样的功能:

public function nationList($limit = null, $start = null)
{
    if ($this->session->userdata('language') == "it") {
        $this->db->select('nation.id, nation.name_it as name');
    }
    if ($this->session->userdata('language') == "en") {
        $this->db->select('nation.id, nation.name_en as name');
    }
    $this->db->from('nation');
    $this->db->order_by("name", "asc");
    $this->db->limit($limit, $start);
    $query = $this->db->get();
    $nation = array();
    foreach ($query->result() as $row) {
        array_push($nation, $row);
    }
    return $nation;     
}

如果进入我的控制器,我无限制地调用该函数,并且 start 不会返回如下结果:

$data["nationlist"] = $this->Nation_model->nationList();

相反,如果我设定限制并开始工作! 如果 limit 和 start 为 null,为什么不返回结果?如果 limit 和 start 为空,我不想创建第二个函数或控件。 当 limit 和 start 为空而没有控件或第二个函数以使代码变得有用且更高效时,我该如何解决这个问题?

php sql codeigniter
3个回答
29
投票

试试这个...

function nationList($limit=null, $start=null) {
    if ($this->session->userdata('language') == "it") {
        $this->db->select('nation.id, nation.name_it as name');
    }

    if ($this->session->userdata('language') == "en") {
        $this->db->select('nation.id, nation.name_en as name');
    }

    $this->db->from('nation');
    $this->db->order_by("name", "asc");

    if ($limit != '' && $start != '') {
       $this->db->limit($limit, $start);
    }
    $query  = $this->db->get();

    $nation = array();
    foreach ($query->result() as $row) {
        array_push($nation, $row);
    }

    return $nation;     
}

14
投票

对于更多访客:

// Executes: SELECT * FROM mytable LIMIT 10 OFFSET 20
// get([$table = ''[, $limit = NULL[, $offset = NULL]]])
$query = $this->db->get('mytable', 10, 20);

// get_where sample, 
$query = $this->db->get_where('mytable', array('id' => $id), 10, 20);

// Produces: LIMIT 10
$this->db->limit(10);  

// Produces: LIMIT 10 OFFSET 20
// limit($value[, $offset = 0])
$this->db->limit(10, 20);

0
投票

我不知道你在 2013 年使用的 CI 版本是什么,但我使用的是 CI3,并且我刚刚测试了传递给

null
的两个
limit()
参数,并且在测试中没有
LIMIT
OFFSET
渲染的查询(我使用
get_compiled_select()
检查)。

这意味着——假设您已经正确发布了您的编码尝试——您不需要更改任何内容(或者至少旧问题不再是 CI 问题)。

如果这是我的项目,这就是我将如何编写方法来返回对象的索引数组或空数组(如果结果集中没有符合条件的行)。

function nationList($limit = null, $start = null) {
    // assuming the language value is sanitized/validated/whitelisted
    return $this->db
        ->select('nation.id, nation.name_' . $this->session->userdata('language') . ' AS name')
        ->from('nation')
        ->order_by("name")
        ->limit($limit, $start)
        ->get()
        ->result();
}

这些改进删除了不必要的语法、条件和冗余循环。

作为参考,这里是 CI 核心代码:

/**
 * LIMIT
 *
 * @param   int $value  LIMIT value
 * @param   int $offset OFFSET value
 * @return  CI_DB_query_builder
 */
public function limit($value, $offset = 0)
{
    is_null($value) OR $this->qb_limit = (int) $value;
    empty($offset) OR $this->qb_offset = (int) $offset;

    return $this;
}

因此

$this->qb_limit
$this->qb_offset
类对象不会更新,因为
null
在馈送到
true
is_null()
时计算为
empty()

© www.soinside.com 2019 - 2024. All rights reserved.