crudoperation.php

$searchKeyword, 'email' => $searchKeyword, 'phone' => $searchKeyword ); } // Get count of the users $con = array( 'like_or' => $searchArr, 'return_type' => 'count' ); $rowCount = $db->getRows('users_custom', $con); // Initialize pagination class $pagConfig = array( 'baseURL' => 'custom-crud'.$searchStr, 'totalRows' => $rowCount, 'perPage' => $perPageLimit ); $pagination = new Pagination($pagConfig); // Get users from database $con = array( 'like_or' => $searchArr, 'start' => $offset, 'limit' => $perPageLimit, 'order_by' => 'id DESC', ); $users = $db->getRows('users_custom', $con); ?>

New User
Name Email Phone Action
No user(s) found......
createLinks(); ?>

DB.class.php

db)){ // Connect to the database $conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName); if($conn->connect_error){ die("Failed to connect with MySQL: " . $conn->connect_error); }else{ $this->db = $conn; } } } /* * Returns rows from the database based on the conditions * @param string name of the table * @param array select, where, order_by, limit and return_type conditions */ public function getRows($table, $conditions = array()){ $sql = 'SELECT '; $sql .= array_key_exists("select", $conditions)?$conditions['select']:'*'; $sql .= ' FROM '.$table; if(array_key_exists("where", $conditions)){ $sql .= ' WHERE '; $i = 0; foreach($conditions['where'] as $key => $value){ $pre = ($i > 0)?' AND ':''; $sql .= $pre.$key." = '".$value."'"; $i++; } } if(array_key_exists("like", $conditions) && !empty($conditions['like'])){ $sql .= (strpos($sql, 'WHERE') !== false)?' AND ':' WHERE '; $i = 0; $likeSQL = ''; foreach($conditions['like'] as $key => $value){ $pre = ($i > 0)?' AND ':''; $likeSQL .= $pre.$key." LIKE '%".$value."%'"; $i++; } $sql .= '('.$likeSQL.')'; } if(array_key_exists("like_or", $conditions) && !empty($conditions['like_or'])){ $sql .= (strpos($sql, 'WHERE') !== false)?' AND ':' WHERE '; $i = 0; $likeSQL = ''; foreach($conditions['like_or'] as $key => $value){ $pre = ($i > 0)?' OR ':''; $likeSQL .= $pre.$key." LIKE '%".$value."%'"; $i++; } $sql .= '('.$likeSQL.')'; } if(array_key_exists("order_by", $conditions)){ $sql .= ' ORDER BY '.$conditions['order_by']; } if(array_key_exists("start", $conditions) && array_key_exists("limit", $conditions)){ $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit']; }elseif(!array_key_exists("start", $conditions) && array_key_exists("limit", $conditions)){ $sql .= ' LIMIT '.$conditions['limit']; } $result = $this->db->query($sql); if(array_key_exists("return_type", $conditions) && $conditions['return_type'] != 'all'){ switch($conditions['return_type']){ case 'count': $data = $result->num_rows; break; case 'single': $data = $result->fetch_assoc(); break; default: $data = ''; } }else{ if($result->num_rows > 0){ while($row = $result->fetch_assoc()){ $data[] = $row; } } } return !empty($data)?$data:false; } /* * Insert data into the database * @param string name of the table * @param array the data for inserting into the table */ public function insert($table, $data){ if(!empty($data) && is_array($data)){ $columns = ''; $values = ''; $i = 0; if(!array_key_exists('created', $data)){ $data['created'] = date("Y-m-d H:i:s"); } if(!array_key_exists('modified', $data)){ $data['modified'] = date("Y-m-d H:i:s"); } foreach($data as $key=>$val){ $pre = ($i > 0)?', ':''; $columns .= $pre.$key; $values .= $pre."'".$val."'"; $i++; } $query = "INSERT INTO ".$table." (".$columns.") VALUES (".$values.")"; $insert = $this->db->query($query); return $insert?$this->db->insert_id:false; }else{ return false; } } /* * Update data into the database * @param string name of the table * @param array the data for updating into the table * @param array where condition on updating data */ public function update($table, $data, $conditions){ if(!empty($data) && is_array($data)){ $colvalSet = ''; $whereSql = ''; $i = 0; if(!array_key_exists('modified',$data)){ $data['modified'] = date("Y-m-d H:i:s"); } foreach($data as $key=>$val){ $pre = ($i > 0)?', ':''; $colvalSet .= $pre.$key."='".$val."'"; $i++; } if(!empty($conditions)&& is_array($conditions)){ $whereSql .= ' WHERE '; $i = 0; foreach($conditions as $key => $value){ $pre = ($i > 0)?' AND ':''; $whereSql .= $pre.$key." = '".$value."'"; $i++; } } $query = "UPDATE ".$table." SET ".$colvalSet.$whereSql; $update = $this->db->query($query); return $update?$this->db->affected_rows:false; }else{ return false; } } /* * Delete data from the database * @param string name of the table * @param array where condition on deleting data */ public function delete($table, $conditions){ $whereSql = ''; if(!empty($conditions) && is_array($conditions)){ $whereSql .= ' WHERE '; $i = 0; foreach($conditions as $key => $value){ $pre = ($i > 0)?' AND ':''; $whereSql .= $pre.$key." = '".$value."'"; $i++; } } $query = "DELETE FROM ".$table.$whereSql; $delete = $this->db->query($query); return $delete?true:false; } }

Pagination.class.php/h2>

'; protected $fullTagClose = ''; protected $firstTagOpen = ''; protected $firstTagClose = ' '; protected $lastTagOpen = ' '; protected $lastTagClose = ''; protected $curTagOpen = ' '; protected $curTagClose = ''; protected $nextTagOpen = ' '; protected $nextTagClose = ' '; protected $prevTagOpen = ' '; protected $prevTagClose = ''; protected $numTagOpen = ' '; protected $numTagClose = ''; protected $showCount = true; protected $currentOffset= 0; protected $queryStringSegment = 'page'; function __construct($params = array()){ if (count($params) > 0){ $this->initialize($params); } } function initialize($params = array()){ if (count($params) > 0){ foreach ($params as $key => $val){ if (isset($this->$key)){ $this->$key = $val; } } } } /** * Generate the pagination links */ function createLinks(){ // If total number of rows is zero, do not need to continue if ($this->totalRows == 0 OR $this->perPage == 0){ return ''; } // Calculate the total number of pages $numPages = ceil($this->totalRows / $this->perPage); // Is there only one page? will not need to continue if ($numPages == 1){ if ($this->showCount){ $info = 'Showing : ' . $this->totalRows; return $info; }else{ return ''; } } // Determine query string $query_string_sep = (strpos($this->baseURL, '?') === FALSE) ? '?page=' : '&page='; $this->baseURL = $this->baseURL.$query_string_sep; // Determine the current page $this->currentPage = isset($_GET[$this->queryStringSegment])?$_GET[$this->queryStringSegment]:0; if (!is_numeric($this->currentPage) || $this->currentPage == 0){ $this->currentPage = 1; } // Links content string variable $output = ''; // Showing links notification if ($this->showCount){ $currentOffset = ($this->currentPage > 1)?($this->currentPage - 1)*$this->perPage:$this->currentPage; $info = 'Showing ' . $currentOffset . ' to ' ; if( ($currentOffset + $this->perPage) <= $this->totalRows ) $info .= $this->currentPage * $this->perPage; else $info .= $this->totalRows; $info .= ' of ' . $this->totalRows . ' | '; $output .= $info; } $this->numLinks = (int)$this->numLinks; // Is the page number beyond the result range? the last page will show if($this->currentPage > $this->totalRows){ $this->currentPage = $numPages; } $uriPageNum = $this->currentPage; // Calculate the start and end numbers. $start = (($this->currentPage - $this->numLinks) > 0) ? $this->currentPage - ($this->numLinks - 1) : 1; $end = (($this->currentPage + $this->numLinks) < $numPages) ? $this->currentPage + $this->numLinks : $numPages; // Render the "First" link if($this->currentPage > $this->numLinks){ $firstPageURL = str_replace($query_string_sep,'',$this->baseURL); $output .= $this->firstTagOpen.''.$this->firstLink.''.$this->firstTagClose; } // Render the "previous" link if($this->currentPage != 1){ $i = ($uriPageNum - 1); if($i == 0) $i = ''; $output .= $this->prevTagOpen.''.$this->prevLink.''.$this->prevTagClose; } // Write the digit links for($loop = $start -1; $loop <= $end; $loop++){ $i = $loop; if($i >= 1){ if($this->currentPage == $loop){ $output .= $this->curTagOpen.$loop.$this->curTagClose; }else{ $output .= $this->numTagOpen.''.$loop.''.$this->numTagClose; } } } // Render the "next" link if($this->currentPage < $numPages){ $i = ($this->currentPage + 1); $output .= $this->nextTagOpen.''.$this->nextLink.''.$this->nextTagClose; } // Render the "Last" link if(($this->currentPage + $this->numLinks) < $numPages){ $i = $numPages; $output .= $this->lastTagOpen.''.$this->lastLink.''.$this->lastTagClose; } // Remove double slashes $output = preg_replace("#([^:])//+#", "\\1/", $output); // Add the wrapper HTML if exists $output = $this->fullTagOpen.$output.$this->fullTagClose; return $output; } }

userAction.php

$name, 'email' => $email, 'phone' => $phone ); // Store submitted data into session $sessData['postData'] = $userData; $sessData['postData']['id'] = $id; // ID query string $idStr = !empty($id)?'?id='.$id:''; // If the data is not empty if(!empty($name) && !empty($email) && !empty($phone)){ if(filter_var($email, FILTER_VALIDATE_EMAIL)){ if(!empty($id)){ // Update data $condition = array('id' => $id); $update = $db->update($tblName, $userData, $condition); if($update){ $sessData['postData'] = ''; $sessData['status']['type'] = 'success'; $sessData['status']['msg'] = 'User data has been updated successfully.'; }else{ $sessData['status']['type'] = 'error'; $sessData['status']['msg'] = 'Some problem occurred, please try again.'; // Set redirect url $redirectURL = 'addedit'.$idStr; } }else{ // Insert data $insert = $db->insert($tblName, $userData); if($insert){ $sessData['postData'] = ''; $sessData['status']['type'] = 'success'; $sessData['status']['msg'] = 'User data has been added successfully.'; }else{ $sessData['status']['type'] = 'error'; $sessData['status']['msg'] = 'Some problem occurred, please try again.'; // Set redirect url $redirectURL = 'addedit'; } } }else{ $sessData['status']['type'] = 'error'; $sessData['status']['msg'] = 'Please enter a valid email address.'; // Set redirect url $redirectURL = 'addedit'.$idStr; } }else{ $sessData['status']['type'] = 'error'; $sessData['status']['msg'] = 'All fields are mandatory, please fill all the fields.'; // Set redirect url $redirectURL = 'addedit'.$idStr; } // Store status into the session $_SESSION['sessData'] = $sessData; }elseif(($_REQUEST['action_type'] == 'delete') && !empty($_GET['id'])){ // Delete data $condition = array('id' => $_GET['id']); $delete = $db->delete($tblName, $condition); if($delete){ $sessData['status']['type'] = 'success'; $sessData['status']['msg'] = 'User data has been deleted successfully.'; }else{ $sessData['status']['type'] = 'error'; $sessData['status']['msg'] = 'Some problem occurred, please try again.'; } // Store status into the session $_SESSION['sessData'] = $sessData; } // Redirect the user header("Location: ".$redirectURL); exit();

/html>