Skip to content

Instantly share code, notes, and snippets.

@asadaly111
Last active October 24, 2016 08:24
Show Gist options
  • Save asadaly111/82ffead047e0eb27b80c9597aa227be0 to your computer and use it in GitHub Desktop.
Save asadaly111/82ffead047e0eb27b80c9597aa227be0 to your computer and use it in GitHub Desktop.
Php Essentials File
<?php
/*Site Base url */
session_start();
define("BASEURL","http://localhost/Erp/");
// Finance
define("FINANCE","".BASEURL."module/finance/");
define("SCRIPT","".BASEURL."inc/scripts/");
// Payroll
define("PAYROLL","".BASEURL."module/payroll/");
define("ADMIN","".BASEURL."module/admin/");
//setting
define("SETTING","".BASEURL."module/setting/");
//Inventory
define("INVENTORY","".BASEURL."module/inventory/");
class dbmanager {
/*Database Details*/
public $host = "localhost";
public $user = "root";
public $pass = "";
public $db = "erp";
/*Variables*/
private $conn;
private $result;
private $query;
function __construct(){
$this->conn = $this->dbconn();
}
// Database Connection
function dbconn(){
$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->db);
if ($this->conn->connect_error){
die('Connection Problem : ' .$this->conn->connect_error);
}
else{
return $this->conn;
}
}
function run_query($query){
$this->query = $this->conn->query($query);
if(!$this->query){
echo "Invalid Query : ".$this->conn->error;
}
else{
return $this->query;
}
}
// Fetch Query Last Auto Generated Inserted ID
function last_id(){
return $this->conn->insert_id;
}
// Fetching Single Record
function fetch_single($query){
$this->result = $this->conn->query($query);
if ($this->conn->error) {
die('Invalid Query :'. $this->conn->error);
}
else{
if ($this->result->num_rows > 0) {
return $this->result->fetch_array();
}
}
}
// Fetching Multiple Records
function fetch_all( $query){
$this->result = $this->conn->query($query);
if ($this->conn->error) {
die ('Invalid Query: '. $this->conn->error);
}
else{
if ($this->result->num_rows > 0 ) {
while($row = $this->result->fetch_assoc()){
$data[] = $row;
}
return $data;
}
else{
return $this->conn->error;
}
}
}
// count number rows
function num_rows($result){
return mysqli_num_rows($result);
}
function escape_string($str){
return $this->conn->escape_string($str);
}
// Quries
public function insert($table, $inserts) {
$values = array_values($inserts);
$keys = array_keys($inserts);
return $this->run_query('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')');
}
// Delete 1 Where
public function delete($table, $where , $id) {
return $this->run_query('DELETE FROM `'.$table.'` WHERE `'.$where.'` = "'.$id.'" ');
}
// Delete 2 Where fix query not useable
public function delete_2_where($table, $where1 , $id1, $where2 , $id2 , $year) {
return $this->run_query('DELETE FROM `'.$table.'` WHERE `fin_year` = "'.$year.'" AND `'.$where1.'` = "'.$id1.'" AND `'.$where2.'` = "'.$id2.'" ');
}
public function select_all($table)
{
return $this->run_query('SELECT * FROM `'.$table.'`');
}
public function select_where_single($table , $key , $id)
{
return $this->fetch_single('SELECT * FROM `'.$table.'` WHERE `'.$key.'` = "'.$id.'" ');
}
public function select_2where_single($table , $key1 , $id1 , $key2 , $id3)
{
return $this->fetch_single('SELECT * FROM `'.$table.'` WHERE `'.$key1.'` = "'.$id1.'" AND `'.$key2.'` = "'.$id3.'" ');
}
// select all where key ==
public function select_where_all($table , $key , $id)
{
return $this->run_query('SELECT * FROM `'.$table.'` WHERE `'.$key.'` = "'.$id.'" ');
}
public function select_where_all_2($table , $wherekey1 , $value1 , $wherekey2 , $value2)
{
return $this->run_query('SELECT * FROM `'.$table.'` WHERE `'.$wherekey1.'` = "'.$value1.'" AND `'.$wherekey2.'` = "'.$value2.'" ');
}
// select query select table where id = id and id = id
public function select_wa_all($table , $wherekey1 , $value1 , $wherekey2 , $value2)
{
return $this->run_query(' SELECT * FROM `'.$table.'` WHERE `'.$wherekey1.'` = "'.$value1.'" AND `'.$wherekey2.'` = "'.$value2.'" ');
}
// single update query
public function update_single_where_key($table , $key , $value , $where , $id)
{
return $this->run_query('UPDATE `'.$table.'` SET `'.$key.'` = "'.$value.'" WHERE `'.$where.'` = "'.$id.'" ');
}
// SELECT COUNT(*) FROM `order` WHERE `where1` = 'value1' AND WHERE `where2` = 'value2'
public function count($table , $where1 , $value1 , $where2 , $value2)
{
return $this->fetch_single(' SELECT COUNT(*) FROM `'.$table.'` WHERE `'.$where1.'` = "'.$value1.'" AND `'.$where2.'` = "'.$value2.'" ');
}
// for company
public function companydsc()
{
$value = $this->select_where_single('company_reg' , 'id' , $_SESSION['compid']);
return $value['dsc'];
}
public function company_name()
{
$value = $this->select_where_single('company_reg' , 'id' , $_SESSION['compid']);
return $value['companyname'];
}
public function finance_authentication()
{
$check = $this->fetch_all(' SELECT * FROM `rights` WHERE `user_id` = "'.$_SESSION['user_id'].'" ');
if (!empty($check)) {
foreach ($check as $key) {
if ($key['module'] == 'finance') {
}else{
echo '<script type="text/javascript">document.location = "'.BASEURL.'";</script>';
}
}
}
}
public function payroll_authentication()
{
$check = $this->fetch_all(' SELECT * FROM `rights` WHERE `user_id` = "'.$_SESSION['user_id'].'" ');
if (!empty($check)) {
foreach ($check as $key) {
if ($key['module'] == 'hrm') {
}else{
echo '<script type="text/javascript">document.location = "'.BASEURL.'";</script>';
}
}
}
}
public function inventory_authentication()
{
$check = $this->fetch_all(' SELECT * FROM `rights` WHERE `user_id` = "'.$_SESSION['user_id'].'" ');
if (!empty($check)) {
foreach ($check as $key) {
if ($key['module'] == 'inventory') {
}else{
echo '<script type="text/javascript">document.location = "'.BASEURL.'";</script>';
}
}
}
}
public function update_all($table, $updateKaData, $where , $id){
$x=null;
foreach($updateKaData as $key=>$val)
{
$x.="`".$key."`='".$val."',";
}
$x=substr($x,0,-1);
$query='UPDATE `'.$table.'` SET '.$x.' WHERE `'.$where.'` = '.$id.' ';
$result=substr($query, 0,-1);
$this->run_query($result);
}
// New SQL Qurires
/*
* 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;
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++;
}
}
$sql = "UPDATE ".$table." SET ".$colvalSet.$whereSql;
$this->run_query($sql);
}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++;
}
}
$sql = "DELETE FROM ".$table.$whereSql;
$this->run_query($sql);
}
/*
* 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("condition",$conditions)){
$sql .= ' '.$conditions['condition'];
}
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'];
}
if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
switch($conditions['return_type']){
case 'count':
return mysqli_num_rows($this->run_query($sql));
break;
case 'single':
return $this->fetch_single($sql);
break;
default:
$data = '';
}
}else{
return $this->run_query($sql);
// var_dump($sql);
}
}
public function getCol($table , $conditions)
{
$sql = 'SELECT '.'`'.implode('`,`', $conditions['col']).'`';
$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("condition",$conditions)){
$sql .= ' '.$conditions['condition'];
}
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'];
}
if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
switch($conditions['return_type']){
case 'single':
return $this->fetch_single($sql);
//var_dump($sql);
break;
default:
$data = '';
}
}else{
return $this->run_query($sql);
//var_dump($sql);
}
}
// SELECT FROM `accvouchers` WHERE `type` = 'BPV';
public function countAll($table,$conditions = array())
{
if(array_key_exists("distinct",$conditions)){
$sql = 'SELECT COUNT(DISTINCT '.$conditions['distinct'].') AS count';
}else{
$sql = 'SELECT COUNT(*) AS count';
}
$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++;
}
}
return $this->fetch_single($sql);
//var_dump($sql);
}
} // End DB Manger Class
function company_authentication(){
if (!isset($_SESSION['user_id'])) {
echo '<script type="text/javascript">document.location = "'.BASEURL.'login.php";</script>';
}
}
function get_datentime($date){
$oDate = new DateTime($date);
return $sDate = $oDate->format("h:i:sa d, F Y");
}
function get_date($date){
$oDate = new DateTime($date);
return $sDate = $oDate->format("d, F Y");
}
function flash( $name = '', $message = '')
{
//We can only do something if the name isn't empty
if( !empty( $name ) )
{
//No message, create it
if( !empty( $message ) && empty( $_SESSION[$name] ) )
{
if( !empty( $_SESSION[$name] ) )
{
unset( $_SESSION[$name] );
}
$_SESSION[$name] = ''.$message.'';
}
//Message exists, display it
elseif( !empty( $_SESSION[$name] ) && empty( $message ) )
{
unset($_SESSION[$name]);
}
}
}
function redirect($url)
{
header('Location: ' . $url, true, 303);
die();
}
function pagination($linkcount='' , $currentpage = '' , $rowCount , $showperpage)
{
$numrows = $rowCount[0];
$rowsperpage = $showperpage;
$data['show'] = $showperpage;
$totalpages = ceil($numrows/$rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$data['offset'] = ($currentpage - 1) * $rowsperpage;
// range of num links to show
$range = $linkcount;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
$data['pagination'][] = "<a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
$data['pagination'][] = "<a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
$data['pagination'][] = " <b>$x</b> ";
// if not current page...
} else {
// make it a link
$data['pagination'][] = " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
$data['pagination'][] = " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'> ></a> ";
// echo forward link for lastpage
$data['pagination'][] = " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>&nbsp;&nbsp; >> </a> ";
} // end if
/****** end build pagination links ******/
return $data;
}
$obj = new dbmanager;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment