Simple Class for Search in Database with Date


class SearchDb
public $tablename;
public $fieldname;
public $post;

protected $fromdate;
protected $todate;
protected $filterby;

public function __construct($tablename,$fieldname,$post) {
$this->tablename = $tablename;
$this->post = $post;
$this->fieldname = $fieldname;
$this->fromdate = isset($this->post[‘fromdate’])?$this->post[‘fromdate’]:””;
$this->todate = isset($this->post[‘todate’])?$this->post[‘todate’]:””;
$this->filterby = isset($this->post[‘filterby’])?$this->post[‘filterby’]:””;
$this->table = “”


public function makesearh()
if($this->fromdate!=”” and $this->todate!=”” and $this->filterby!=””) {
return $this->SearchByDateAndFilter();
elseif($this->fromdate!=”” and $this->todate!=””){
return $this->onlySearchByDate();
return $this->onlySearchByFilter();
throw new Exception(‘Invalid search terms.’);

private function onlySearchByFilter(){
return R::getAll(“select *from “.$this->table.”where “.$this->fieldname.”=?”,array($this->filterby));

private function onlySearchByDate(){
return R::getAll(“select *from “.$this->table.”where createdate=>=? createdate=<?”,array($this->fromdate,$this->todate));
private function SearchByDateAndFilter(){
return R::getAll(“select *from “.$this->table.”where createdate=>=? and createdate=<? and “.$this->fieldname.”=? “,array($this->fromdate,$this->todate,$this->filterby));

$class = new SearchDb(‘admin’,’name’,$post);
$class ->getSearchResult();


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s