/src/BITM/SEIP145741/Hobbies/Hobbies.php
https://gitlab.com/Shweta_riya/practice · PHP · 239 lines · 150 code · 81 blank · 8 comment · 12 complexity · 23aab415e4312c52846d190d2fc66295 MD5 · raw file
- <?php
- namespace App\Hobbies;
- use App\Message\Message;
- use App\Model\database as db;
- use App\Utility\Utility;
- use PDO;
- //require_once("../../../../vendor/autoload.php");
- class Hobbies extends db{
- public $id;
- public $name;
- public $hobby;
- public function __construct()
- {
- parent::__construct();
- }
- public function setData($data = Null)
- {
- if (array_key_exists('id', $data)) {
- $this->id = $data['id'];
- }
- if (array_key_exists('name', $data)) {
- $this->name= $data['name'];
- }
- if (array_key_exists('hobby', $data)) {
- $this->hobby =implode(",", $data['hobby']);
- }
- }
- public function store(){
- $arrData=array($this->name,$this->hobby);
- $sql= "Insert INTO hobbies(name,hobby) VALUES (?,?)";
- $STH= $this->DBH->prepare($sql);
- $result= $STH->execute($arrData);
- if($result)
- Message::setMessage("Sucess!data has been inserted sucessfully");
- else
- Message::setMessage("Failure!data has not been inserted sucessfully");
- Utility::redirect('create.php');
- }// end of store method
- public function index($fetchMode='ASSOC'){
- $sql = "SELECT * from hobbies where is_deleted = 0 ";
- $STH = $this->DBH->query($sql);
- $fetchMode = strtoupper($fetchMode);
- if(substr_count($fetchMode,'OBJ') > 0)
- $STH->setFetchMode(PDO::FETCH_OBJ);
- else
- $STH->setFetchMode(PDO::FETCH_ASSOC);
- $arrAllData = $STH->fetchAll();
- return $arrAllData;
- }// end of index();
- public function view($fetchMode='ASSOC'){
- $sql='SELECT * from hobbies WHERE id='.$this->id;
- $STH = $this->DBH->query($sql);
- //echo $sql;
- $fetchMode = strtoupper($fetchMode);
- if(substr_count($fetchMode,'OBJ') > 0)
- $STH->setFetchMode(PDO::FETCH_OBJ);
- else
- $STH->setFetchMode(PDO::FETCH_ASSOC);
- $arrOneData = $STH->fetch();
- return $arrOneData;
- }// end of view();
- public function update(){
- $arrData=array($this->name,$this->hobby);
- //UPDATE `atomic_project_35`.`book_title` SET `book_title` = 'nan' WHERE `book_title`.`id` = 4;
- $sql="update hobbies SET name=? ,hobby=? WHERE id=".$this->id;
- $STH =$this->DBH->prepare($sql);
- $STH->execute($arrData);
- Utility::redirect('index.php');
- }//end of update();
- public function delete(){
- $sql="Delete from hobbies where id=".$this->id;
- $STH=$this->DBH->prepare($sql);
- $STH->execute();
- Utility::redirect('index.php');
- }//end of delete();
- public function trash(){
- $sql = "Update hobbies SET is_deleted=NOW()where id=".$this->id;
- $STH = $this->DBH->prepare($sql);
- $STH->execute();
- Utility::redirect('index.php');
- }// end of trash()
- public function indexPaginator($page=1,$itemsPerPage=3){
- $start = (($page-1) * $itemsPerPage);
- $sql = "SELECT * from hobbies WHERE is_deleted = '0' LIMIT $start,$itemsPerPage";
- $STH = $this->DBH->query($sql);
- $STH->setFetchMode(PDO::FETCH_OBJ);
- $arrSomeData = $STH->fetchAll();
- return $arrSomeData;
- }// end of index paginator
- public function trashedPaginator($page=0,$itemsPerPage=3){
- $start = (($page-1) * $itemsPerPage);
- $sql = "SELECT * from book_title WHERE is_deleted <> 'No' LIMIT $start,$itemsPerPage";
- $STH = $this->DBH->query($sql);
- $STH->setFetchMode(PDO::FETCH_OBJ);
- $arrSomeData = $STH->fetchAll();
- return $arrSomeData;
- }// end of trashedPaginator();
- public function search($requestArray){
- $sql = "";
- if( isset($requestArray['byHobby']) && isset($requestArray['byName']) ) $sql = "SELECT * FROM `hobbies` WHERE `is_deleted` ='0' AND (`hobbies` LIKE '%".$requestArray['search']."%' OR `name` LIKE '%".$requestArray['search']."%')";
- if(isset($requestArray['byHobby']) && !isset($requestArray['byName']) ) $sql = "SELECT * FROM `hobbies` WHERE `is_deleted` ='0' AND `hobbies` LIKE '%".$requestArray['search']."%'";
- if(!isset($requestArray['byHobby']) && isset($requestArray['byName']) ) $sql = "SELECT * FROM `hobbies` WHERE `is_deleted` ='0' AND `name` LIKE '%".$requestArray['search']."%'";
- $STH = $this->DBH->query($sql);
- $STH->setFetchMode(PDO::FETCH_OBJ);
- $allData = $STH->fetchAll();
- return $allData;
- }// end of search()
- public function getAllKeywords()
- {
- $_allKeywords = array();
- $WordsArr = array();
- $sql = "SELECT * FROM `hobbies` WHERE `is_deleted` ='0'";
- $STH = $this->DBH->query($sql);
- $STH->setFetchMode(PDO::FETCH_OBJ);
- // for each search field block start
- $allData= $STH->fetchAll();
- foreach ($allData as $oneData) {
- $_allKeywords[] = trim($oneData->hobby);
- }
- $STH = $this->DBH->query($sql);
- $STH->setFetchMode(PDO::FETCH_OBJ);
- $allData= $STH->fetchAll();
- foreach ($allData as $oneData) {
- $eachString= strip_tags($oneData->hobby);
- $eachString=trim( $eachString);
- $eachString= preg_replace( "/\r|\n/", " ", $eachString);
- $eachString= str_replace(" ","", $eachString);
- $WordsArr = explode(" ", $eachString);
- foreach ($WordsArr as $eachWord){
- $_allKeywords[] = trim($eachWord);
- }
- }
- // for each search field block end
- // for each search field block start
- $STH = $this->DBH->query($sql);
- $STH->setFetchMode(PDO::FETCH_OBJ);
- $allData= $STH->fetchAll();
- foreach ($allData as $oneData) {
- $_allKeywords[] = trim($oneData->name);
- }
- $STH = $this->DBH->query($sql);
- $STH->setFetchMode(PDO::FETCH_OBJ);
- $allData= $STH->fetchAll();
- foreach ($allData as $oneData) {
- $eachString= strip_tags($oneData->name);
- $eachString=trim( $eachString);
- $eachString= preg_replace( "/\r|\n/", " ", $eachString);
- $eachString= str_replace(" ","", $eachString);
- $WordsArr = explode(" ", $eachString);
- foreach ($WordsArr as $eachWord){
- $_allKeywords[] = trim($eachWord);
- }
- }
- // for each search field block end
- return array_unique($_allKeywords);
- }// get all keywords
- }
- //$objHobbies=new Hobbies();