简体   繁体   中英

Selecting from database with offsets

 <?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);

}

?>

This is what i'm currently doing.. Is there a better way to do this? Each case is a page and i'm calling that from page/$num

How would I get the 28 NEWEST entries from the same thing i'm trying to achieve above?


Update from Robert Pitt

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

Then you query your database like so:

SELECT * FROM table LIMIT $offset,$limit

You can use only two queries for unlimited pages:

<?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;
    }
?>

Optionally, you can store the page size in a variable and use that, instead of hardcoding it:

<?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;
    }
?>

This way, you can change the page size by changing one variable, instead of doing a find/replace in all of the code.

maybe this would help:

$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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM