Between Method

Between is a method of the DB object in the model class to be used as a Query Builder. The purpose of this method is to filter data that is compatible with the BETWEEN command in SQL.

Basic Usage

Here is the basic usage of between method is from SENE_Model class.

$this->db->between(string $column_name, string $value_from, string $value_to): $this->db

Parameters

Where method has 2 required parameters that is column name and value, another parameters are optional. Here is the completed parameters can be used by where methods.

$column_name

The $column_name value is required for filtering data from table. This parameter value is automatically escaped.

$value_from

The $value_from can contain about start value for filtering data by between method.

$value_to

The $value_to can contain about end value for filtering data by between method.

Example

Here is the basic example that used in a model class.

<?php
class Blog_Model extends SENE_Model{
  var $tbl = 'd_order';
  var $tbl_as = 'dor';
  public function __construct(){
    parent::__construct();
    $this->db->from($this->tbl,$this->tbl_as);
  }

  /**
  * Get blog post by creation date (not date time)
  * @param  string  $sdate  		String date format YYYY-MM-DD
  * @param  string  $edate  		String date format YYYY-MM-DD
  * @return array           		Result array of object
  */
  public function getByDateRange($sdate,$edate){
    if (strlen($sdate)==10 && strlen($edate)==10) {
      $this->db->between("$this->tbl_as.cdate", "'$sdate'", "'$edate 23:59:59'");
    }elseif(strlen($sdate)==10 && strlen($edate)!=10){
      $this->db->where_as("$this->tbl_as.cdate", "'$sdate'", "AND", '<=');
    }elseif(strlen($sdate)!=10 && strlen($edate)==10){
      $this->db->where_as("$this->tbl_as.cdate", "'$edate'", "AND", '>=');
    }
    return $this->db->get();
  }

  /**
  * Get blog post by publish date time
  * @param  string  $sdate  		String date format YYYY-MM-DD
  * @param  string  $edate  		String date format YYYY-MM-DD
  * @return array           		Result array of object
  */
  public function getByDateTimeRange($sdate,$edate){
    if (strlen($sdate)==10 && strlen($edate)==10) {
      $this->db->between("$this->tbl_as.pubdt", "('$sdate 00:00:00')", "('$edate 23:59:59')");
    }elseif(strlen($sdate)==10 && strlen($edate)!=10){
      $this->db->where_as("$this->tbl_as.pubdt", "('$sdate 00:00:00')", "AND", '<=');
    }elseif(strlen($sdate)!=10 && strlen($edate)==10){
      $this->db->where_as("$this->tbl_as.pubdt", "('$edate 23:59:59')", "AND", '>=');
    }
    return $this->db->get();
  }
}

Tips

This method can be chained with other Query Builder methods, because it returns the same object value (return object) as $this->db in the class Model.