-=[ Mr. Bumblebee ]=-
_Indonesia_

Path : /var/www/html/inc.bk/class/
File Upload :
Current File : /var/www/html/inc.bk/class/class.query.php

<?php

class query
{
	var $db_connect ;
	var $rs ;
	var $total = array (
							'pagesize'	=> 10 ,
							'nowpage'	=> 1 ,
							'blockpage'	=> 5 ,
							'totalpage'	=> 0 ,
							'firstpage'	=> '' ,
							'prevpage'	=> '' ,
							'pageloop'	=> '' ,
							'nextpage'	=> '' ,
							'lastpage'	=> ''
						) ;
	var $fetch_type ;
	var $errorview = 'Y' ;
	var $button = array () ;
	var $design = array () ;
	var $execute_query ;

	function conn ( $server , $user_id , $password , $database_name )
	{
		$this->db_connect = new database ;
		$this->db_connect->info ( $server , $user_id , $password , $database_name) ;
	}
	function chkerror()
	{
		if ( mysql_affected_rows() == -1 && $this->errorview == 'Y' )
		{
			//fc_error( mysql_error() , '' ) ;
			echo mysql_error() ;
			echo '<br><br> query : ' . $this->execute_query ;
			exit ;
		}
	}
	function paging ( $link , $total = array() , $name = 'page' )
	{
		foreach ( $this->total as $key=>$val ) $this->total[$key] = $total[$key] ;
		if ( empty($this->total['nowpage']) || $this->total['nowpage'] < 1 || !is_numeric($this->total['nowpage'])) $this->total['nowpage'] = 1 ;
		if ( empty($design['left'])) $design['left'] = '' ;
		if ( empty($design['center'])) $design['center'] = ' กค ' ;
		if ( empty($design['right'])) $design['right'] = '' ;
		if ( empty($design['select'])) $design['select'] = 'font-weight:bold;' ;
		$total['firstpage'] = '' ;
		$total['prevpage'] = '' ;
		$total['pageloop'] = '' ;
		$total['nextpage'] = '' ;
		$total['lastpage'] = '' ;
		if ( $this->total['nowpage'] > $total['totalpage'] && !empty($total['totalpage'])) $this->total['nowpage'] = $total['totalpage'] ;
		$total['firstpage'] = ( $this->total['nowpage'] < $this->total['blockpage'] )?$this->button['firstpage_n']:'<a href="' . $link . '&' . $name . '=1">' . $this->button['firstpage'] . '</a>' ;
		$page_first_number = floor( ( $this->total['nowpage']-1 ) / $this->total['blockpage'] )* $this->total['blockpage'] + 1 ;
		$total['prevpage'] = ( $page_first_number <= 1 )?$this->button['prevpage_n'] . '&nbsp;':'<a href="' . $link . '&' . $name . '=' . ($page_first_number - $this->total['blockpage']) . '">' . $this->button['prevpage'] . '</a>&nbsp;' ;
		$tmp_limit = 1 ;
		$tmp_count = $page_first_number ;
		while ( $tmp_limit <= $this->total['blockpage'] && $tmp_count <= $this->total['totalpage'] )
		{
			if ( !empty($total['pageloop'])) $total['pageloop'].= $design['center'] ;
			$total['pageloop'].= ( $tmp_count == $this->total['nowpage'] )?' <span style="' . $design['select'] . '">' . $tmp_count . '</span> ':' <a href="' . $link . '&' . $name . '=' . $tmp_count . '">' . $tmp_count . '</a> ' ;
			$tmp_count++; $tmp_limit++ ;
		}
		$total['pageloop'] = ( !empty($total['pageloop']))?$design['left'] . $total['pageloop'] . $design['right']:$total['pageloop'] = ' - ' ;
		if ( $this->total['totalpage'] >= $tmp_count) $total['pageloop'].= '..' ;
		if ( $this->total['nowpage'] > $this->total['blockpage'] ) $total['pageloop'] = '..' . $total['pageloop'] ;
		$next_page_first_number = $page_first_number + $total['blockpage'] ;
		$total['nextpage'] = ( $next_page_first_number > $total['totalpage'] )?'&nbsp;' . $this->button['nextpage_n']:'&nbsp;<a href="' . $link . '&' . $name . '=' . $next_page_first_number . '">' . $this->button['nextpage'] . '</a>' ;
		$total['lastpage'] = ( $this->total['nowpage'] == $total['totalpage'] || empty($total['totalpage']) || $next_page_first_number > $total['totalpage'])?$this->button['lastpage_n']:'<a href="' . $link . '&' . $name . '=' . $total['totalpage'] . '">' . $this->button['lastpage'] . '</a>' ;
		return $total ;
	}
	function fetch ( $execute )
	{
		if ( is_resource( $execute )) {
			switch ( $this->fetch_type )
			{
				case 'assoc' :		$this->rs = mysql_fetch_assoc( $execute ) ; break ;
				case 'num' :		$this->rs = mysql_fetch_row( $execute) ;	break ;
				default :			$this->rs = mysql_fetch_array( $execute ) ; break ;
			}
			return $this->rs ;
		}
	}
	function qry_fetch ( $query , $columns , $target = '' )
	{
		$query = " SELECT $columns $query " ;
		$execute = mysql_query($query) ;
		if ( $this->errorview == 'Y' ) {
			$this->execute_query = $query ;
			$this->chkerror() ;
		}
		$rs = $this->fetch($execute) ;
		return empty($target)?$rs:$rs[$target] ;
	}
	function qry_one ( $query , $columns , $error = 'Y' )
	{
		$query = " SELECT $columns $query " ;
		$execute = mysql_query($query) ;
		if ( $this->errorview == 'Y' ) {
			$this->execute_query = $query ;
			$this->chkerror() ;
		}
		return $execute ;
	}
	function qry_insert ( $data , $table )
	{
		$columns = "" ;
		$values = "" ;
		foreach ( $data as $key=>$val )
		{
			if ( empty($key)) continue ;
			if ( !empty($columns)) $columns.= "," ;
			if ( !empty($values)) $values.= "," ;
			$columns.= $key ;
			if ( strtoupper($val) == 'NULL' ) {
				$values.= " null " ;
			} else {
				$values.= " '$val' " ;
			}
		}
		$query = " INSERT INTO $table ( $columns ) VALUES ( $values ) " ;
		mysql_query( $query ) ;
		if ( $this->errorview == 'Y' ) {
			$this->execute_query = $query ;
			$this->chkerror() ;
		}
		$auto_number = mysql_query(" SELECT LAST_INSERT_ID() ") ;
		$auto_rs = mysql_fetch_row($auto_number) ;
		return $auto_rs[0] ;
	}
	function qry_update ( $data , $table , $con )
	{
		$set_values = '' ;
		if ( is_array($data))
		{
			foreach ( $data as $key=>$val )
			{
				if ( empty($key)) continue ;
				if ( !empty($set_values)) $set_values.= ", " ;
				if ( strtoupper($val) == 'NULL' ) {
					$set_values.= " $key = null " ;
				} else {
					$set_values.= " $key = '$val' " ;
				}
			}
		}
		else $set_values = $data ;
		$query = " UPDATE $table SET {$set_values} WHERE $con " ;
		mysql_query($query) ;
		if ( $this->errorview == 'Y' ) {
			$this->execute_query = $query ;
			$this->chkerror() ;
		}
	}
	function qry_delete ( $table , $con )
	{
		$query = " DELETE FROM $table WHERE $con " ;
		mysql_query($query) ;
		if ( $this->errorview == 'Y' ) {
			$this->execute_query = $query ;
			$this->chkerror() ;
		}
	}
	function qry_list ( $query , $columns , $total , $count_key = ' * ' )
	{
		if ( !empty($total['pagesize'])) $this->total['pagesize'] = $total['pagesize'] ;
		$this->total['nowpage'] = ( !empty($total['nowpage']))?$total['nowpage']:1 ;
		if ( !empty($total['blockpage'])) $this->total['blockpage'] = $total['blockpage'] ;
		#$this->total['totalcount'] = $this->qry_fetch ( $query , " COUNT({$count_key}) AS cntnum " , "cntnum" ) ;
		$this->total['execute'] = mysql_query( " SELECT SQL_CALC_FOUND_ROWS {$columns} {$query} LIMIT " . (($this->total['nowpage']-1)*$this->total['pagesize']) . ", " . $this->total['pagesize'] ) ;
		if ( $this->errorview == 'Y' ) {
			$this->execute_query = $query ;
			$this->chkerror() ;
		}
		$tmp_totalcount = mysql_fetch_row(mysql_query( "SELECT FOUND_ROWS() "));
		$this->total['totalcount'] = $tmp_totalcount[0] ;
		if ( empty($this->total['totalcount'])) $this->total['totalcount'] = 0 ;
		$this->total['totalpage'] = ceil($this->total['totalcount']/$this->total['pagesize']) ;
		if ( $this->total['nowpage'] > $this->total['totalpage']) $this->total['nowpage'] = $this->total['totalpage'] ;
		$this->total['recno'] = $this->total['totalcount'] - (( $this->total['nowpage']-1)*$this->total['pagesize']) ;
		$total['totalcount'] = $this->total['totalcount'] ;
		$total['totalpage'] = $this->total['totalpage'] ;
		$total['recno'] = $this->total['recno'] ;
		$total['execute'] = $this->total['execute'] ;
		return $total ;
	}
	function qry_alllist ( $query , $columns , $total , $count_key = ' * ' )
	{
		$this->total['execute'] = mysql_query( " SELECT SQL_CALC_FOUND_ROWS {$columns} {$query} ") ;
		if ( $this->errorview == 'Y' ) {
			$this->execute_query = $query ;
			$this->chkerror() ;
		}
		$total['execute'] = $this->total['execute'] ;
		return $total ;
	}

	function qry_query ( $query ) {
		mysql_query ( $query ) ;
		if ( $this->errorview == 'Y' ) {
			$this->execute_query = $query ;
			$this->chkerror() ;
		}
	}
}

?>

Copyright © 2017 || Recoded By Mr.Bumblebee