Simple Class for Search in Database with Date

<?php

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();
}
elseif($this->filterby!=””){
return $this->onlySearchByFilter();
}
else{
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();

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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