从具有偏移的数据库中选择

时间:2011-01-27 10:57:10

标签: php sql mysql codeigniter

 <?php
      switch ($page)
        {
        case 1:

            $query = $this->db->get_where('ao', array('banned' => '0'), 28, 0);
            break;
        case 2:
            $query = $this->db->get_where('ao', array('banned' => '0'), 56, 29);
            break;
        case 3:
            $query = $this->db->get_where('ao', array('banned' => '0'), 84, 57);
            break;
        case 4:
            $query = $this->db->get_where('ao', array('banned' => '0'), 112, 85);
            break;
        case 5:
            $query = $this->db->get_where('ao', array('banned' => '0'), 140, 113);
            break;
        case 6:
            $query = $this->db->get_where('ao', array('banned' => '0'), 168, 141);
            break;
        case 7:
            $query = $this->db->get_where('ao', array('banned' => '0'), 196, 169);
             break;
        case 8:
            $query = $this->db->get_where('ao', array('banned' => '0'), 224, 197);
             break;
        case 9:
            $query = $this->db->get_where('ao', array('banned' => '0'), 252, 225);
             break;
        case 10:
            $query = $this->db->get_where('ao', array('banned' => '0'), 280, 253);
            break;
        case 11:
            $query = $this->db->get_where('ao', array('banned' => '0'), 308, 281);
            break;
        case 12:
            $query = $this->db->get_where('ao', array('banned' => '0'), 336, 309);
            break;
        case 13:
            $query = $this->db->get_where('ao', array('banned' => '0'), 364, 337);
            break;
        case 14:
            $query = $this->db->get_where('ao', array('banned' => '0'), 392, 365);
            break;
        case 15:
            $query = $this->db->get_where('ao', array('banned' => '0'), 420, 393);
            break;                  
        default:
            $query = $this->db->get_where('ao', array('banned' => '0'), 28, 0);

}

&GT;

这就是我目前所做的..有更好的方法吗?每个案例都是一个页面,我从page / $ num

调用它

我如何从上面尝试实现的同一件事中获得28个最新条目?


Robert Pitt的更新

  • $page = 2; //page 2
  • $limit = 30; //per page
  • $offset = ($page - 1 * $offset); //30

然后你这样查询你的数据库:

SELECT * FROM table LIMIT $offset,$limit

2 个答案:

答案 0 :(得分:1)

您只能对无限页面使用两个查询:

<?php
    switch($page) {
       case 1:
          $query = $this->db->get_where('ao', array('banned' => '0'), 28, 0);
          break;
       default:
          $query = $this->db->get_where('ao', array('banned' => '0'), 28 * (int)$page, 28 * (int)$page - 27);
          break;
    }
?>

或者,您可以将页面大小存储在变量中并使用它,而不是对其进行硬编码:

<?php
    $ps = 28; // page size
    switch($page) {
       case 1:
          $query = $this->db->get_where('ao', array('banned' => '0'), $ps, 0);
          break;
       default:
          $query = $this->db->get_where('ao', array('banned' => '0'), $ps * (int)$page, $ps * (int)$page - ($ps - 1));
          break;
    }
?>

这样,您可以通过更改一个变量来更改页面大小,而不是在所有代码中执行查找/替换。

答案 1 :(得分:0)

也许这会有所帮助:

$sqltorun="SET @rownum = 0;
SET @startRow = ".$StartRow."; # your $page
SET @maxRows = ".$MaxRows."; # limit. how much rows per page
SELECT * FROM (SELECT @rownum:=@rownum+1 as rownum, t.* FROM (SELECT @rownum:=0) r, YOURTABLE t where banned=>0 order by SOMETHING OR DONT ORDERBY) t WHERE rownum BETWEEN @startRow and @startRow + @maxRows and rownum!=@startRow";