Вы находитесь на странице: 1из 9

Class database:

<?php
class database {
private $db_connection;
private $db_selection;
function __construct($root=false) {
// untuk menambahkan karakter ../
$root_dot="";
if($root==true){
$root_dot="../";
}else{
$root_dot="";
}
include ($root_dot. "config/database_config.php");
$this->db_connection=mysql_connect($db_server,$db_user,$db_password);
$this->db_selection=mysql_select_db($db_selected,$this->db_connection);
}
function db_queryresult($sql="") {
$result=array();
if($sql!=""){
$query=mysql_query($sql,$this->db_connection);
$rows=mysql_fetch_array($query);
if(!empty($rows)){
do{
$result[]=$rows;
}while($rows=mysql_fetch_array($query));
}
}
return $result;
}
function db_getonerow($sql="") {
$result=NULL;
if($sql!=""){
$query=mysql_query($sql,$this->db_connection);
$result=mysql_fetch_array($query);
}
return $result;
}
function db_insert($table="",$fielddata=NULL){
echo "masuk";
//variable untuk hasil output
$result=0;
//variable untuk field
$fields="";
//variable untuk filend value
$fieldsvalue="";
//check $table dan $fielddata ada isinya?
if($table!="" && $fielddata!=NULL){
//looping untuk memasukkan data field & field value
foreach($fielddata as $rows=>$rowsvalue){
$fields=$fields.$rows.",";
$fieldsvalue=$fieldsvalue.$rowsvalue.",";
}
// buang koma terakhir
$fields=substr($fields,0,strlen($fields)-1);
$fieldsvalue=substr($fieldsvalue,0,strlen($fieldsvalue)-1);

// buat sintak sql


$sql="insert into ".$table."(".$fields.") "."values(" .$fieldsvalue.")";
mysql_query($sql,$this->db_connection);
$result=mysql_insert_id();
}
return $result;
}
function db_update($table="",$fielddata=NULL, $wherestr=""){
$result=0;
$fieldset="";
if($table!="" && $fielddata !=NULL && $wherestr!=""){
foreach($fielddata as $field=>$fieldvalue){
$fieldset=$fieldset.$field."=".$fieldvalue.",";
}
$fieldset=substr($fieldset,0,strlen($fieldset)-1);
$sql="UPDATE ".$table." SET ".$fieldset;
$sql=$sql." WHERE ". $wherestr;
$result=mysql_query($sql,$this->db_connection);
}
return $result;
}
function db_rowcount($sql=""){
$result=0;
if($sql!=""){
$query=mysql_query($sql,$this->db_connection);
$result=mysql_num_rows($query);
}
return $result;
}
function db_execute($sql=""){
$result=0;
if($sql!=""){
$result=mysql_query($sql, $this->db_connection);
}
return $result;
}
?>

Class brand:
<?php
class brand extends database{
private $sql="SELECT * FROM ms_brand";
function __construct($root){
parent::__construct($root);
}
function createoptionbrand($brand_id=0){
$result=NULL;
$sql=$this->sql;
$query=parent::db_queryresult($sql);
$utility_obj=new utility();
if(!$utility_obj->isarrayempty($query)){
$selected=false;
$result="";
foreach($query as $rows){
if($rows["brand_id"]==$brand_id){
$selected=true;
}else{
$selected=false;
}
$result=$result.$utility_obj->createoption($rows["brand_id"],
$rows["brand_name"],$selected);
}

}
}
?>

}
return $result;

Class order:
<?php
class order extends database{
function __construct($root=true){
parent::__construct($root);
}
function saveorder($customer_id=0,$address="",$city="",$country="",$email="",
$phone="",$total_price=0){
if(isset($_SESSION["cart"])){
$data=array("customer_id"=>$customer_id,
"address"=>"'".$address."'",
"country"=>"'".$country."'",
"city"=>"'".$city."'",
"email"=>"'".$email."'",
"phone"=>"'".$phone."'",
"total_price"=>$total_price,
"order_date"=>"now()");
$order_id=parent::db_insert("tr_order",$data);
echo $order_id;
for($i=0;$i<=count($_SESSION["cart"])-1;$i++){
$sqldetail="SELECT price FROM ms_shoes WHERE shoes_id=".
$_SESSION["cart"][$i]["shoes_id"];
$shoesrow=parent::db_getonerow($sqldetail);
$subtotal=$shoesrow["price"]*$_SESSION["cart"][$i]["qty"];
$datadetail=array("order_id"=>$order_id,
"shoes_id"=>$_SESSION["cart"][$i]["shoes_id"],
"jumlah_barang"=>$_SESSION["cart"][$i]["qty"],
"harga"=>$shoesrow["price"],
"jumlah_harga"=>$subtotal);
parent::db_insert("tr_order_detail",$datadetail);
}
}

}
?>

Class shoes:

<?php
class shoes extends database{
private $_shoes_id=0;
private $_brand_id=0;
private $_brand_name="";
private $_shoes_name="";
private $_description="";
private $_price=0;
private $_size=0;
private $_stock=0;
private $_best_seller_flag=0;
private $_publish=0;
private $_create_date="";
private $sql="SELECT ms_shoes.*,ms_brand.brand_name FROM ms_shoes INNER JOIN
ms_brand ON ms_shoes.brand_id=ms_brand.brand_id ";
function __construct($shoes_id="",$root=false){
//memanggil method __construct dari induknya
parent::__construct($root);

if($shoes_id!=""){
$sqlc=$this->sql."WHERE ms_shoes.shoes_id=" . $shoes_id;
//memanggil method db_getonerow() dari induk
$query=parent::db_getonerow($sqlc);
if($query!=NULL){
$this->_shoes_id=$query["shoes_id"];
$this->_brand_id=$query["brand_id"];
$this->_brand_name=$query["brand_name"];
$this->_shoes_name=$query["shoes_name"];
$this->_description=$query["description"];
$this->_price=$query["price"];
$this->_size=$query["size"];
$this->_stock=$query["stock"];
$this->_best_seller_flag=$query["best_seller_flag"];
$this->_publish=$query["publish"];
$this->_create_date=$query["create_date"];
}
}

function shoes_id(){
return $this->_shoes_id;
}
function brand_id(){
return $this->_brand_id;
}
function shoes_name(){
return $this->_shoes_name;
}
function brand_name(){
return $this->_brand_name;
}
function description(){
return $this->_description;
}
function price(){
return $this->_price;
}
function size(){
return $this->_size;
}
function stock(){

return $this->_stock;
}
function best_seller_flag(){
return $this->_best_seller_flag;
}
function publish(){
return $this->_publish;
}
function create_date(){
return $this->_create_date;
}
function maskot_image($shoes_id=0){
$result="";
if($shoes_id>0){
$sql="SELECT image_name FROM ms_shoes_image WHERE maskot=1 AND shoes_id=".
$shoes_id;
$query=parent::db_getonerow($sql);
if($query!=NULL){
$result=$query["image_name"];
}
}
return $result;
}
function get_images($shoes_id=0){
$result=array();
$uob=new utility();
if($shoes_id>0){
$sql="SELECT image_name FROM ms_shoes_image WHERE maskot=0 AND shoes_id=".
$shoes_id;
$query=parent::db_queryresult($sql);
if(!$uob->isarrayempty($query)){
$result=$query;
}
}
return $result;
}
function get_all($brand_id=0,$exshoes_id=0){
$result=NULL;
$sql=$this->sql;
if($brand_id>0){
$sql=$sql."WHERE ms_shoes.brand_id=".$brand_id." ";
}
if($exshoes_id>0){
if(strpos($sql,"WHERE")>-1){
$sql=$sql. "AND ms_shoes.shoes_id<>".$exshoes_id;
}else{
$sql=$sql."WHERE ms_shoes.shoes_id<>".$exshoes_id;
}
}
$sql=$sql."ORDER BY shoes_id DESC";
$query=parent::db_queryresult($sql);
$utility_obj=new utility();
if(!$utility_obj->isarrayempty($query)){
$result=$query;
}
return $result;
}

}
?>
Class shoppingcart:

<?php
class shoppingcart{
function __construct(){
if(!isset($_SESSION["cart"])){
$_SESSION["cart"]=array();
}

function checkcart($shoes_id=0){
$result=false;
if($shoes_id>0){
for($i=0;$i<=count($_SESSION["cart"])-1;$i++){
if($_SESSION["cart"][$i]["shoes_id"]==$shoes_id){
$result=true;
break;
}
}
}
return $result;
}
function changeqty($shoes_id=0,$qty=0){
if($shoes_id>0&&$qty>0){
for($i=0;$i<=count($_SESSION["cart"])-1;$i++){
if($_SESSION["cart"][$i]["shoes_id"]==$shoes_id){
$_SESSION["cart"][$i]["qty"]=$_SESSION["cart"][$i]["qty"]+$qty;
}
}

}
function addtocart($shoes_id=0,$qty=0){
if($shoes_id>0&&$qty>0){
if($this->checkcart($shoes_id)==true){
$this->changeqty($shoes_id,$qty);
}else{
$_SESSION["cart"][]=array("shoes_id"=>$shoes_id,"qty"=>$qty);
}
}
}
function removecart($shoes_id=0){
if($shoes_id>0){
for($i=0;$i<=count($_SESSION["cart"])-1;$i++){
if($_SESSION["cart"][$i]["shoes_id"]==$shoes_id){
array_splice($_SESSION["cart"],$i,1);
break;
}
}
}
}
function editcart($shoes_id=0,$qty=0){
if($shoes_id>0 && $qty>0){
for($i=0;$i<=count($_SESSION["cart"])-1;$i++){
if($_SESSION["cart"][$i]["shoes_id"]==$shoes_id){
$_SESSION["cart"][$i]["qty"]=$qty;

}
}

break;

}
function clearcart(){
$_SESSION["cart"]=array();
}
function gettotalqty(){
$total=0;
for($i=0;$i<=count($_SESSION["cart"])-1;$i++){
$total=$total+$_SESSION["cart"][$i]["qty"];
}
return $total;
}
}

?>

Class utility:
<?php
class utility {
function isarrayempty($dataarray=NULL){
//hasil keluaran dari method isarrayempty()
$result=false;
//apakah $dataarray tidak sama dengan NULL
if($dataarray!=NULL){
//apakah array isinya lebih dari 0
if(count($dataarray)>0){
//apakah pada posisi index yang pertama ada
if(empty($dataarray[0])){
$result=true;
}else{
$result=false;
}
}
}
return $result;
}

function gotopage($page="",$baseurl=""){
if($page!=""){
echo "<script type='text/javascript' language='javascript'>";
echo "window.location.href='".$baseurl.$page."';";
echo "</script>";
}
}
function createoption($thevalue="",$thelabel="",$selected=false){
$selectedstr="";
if($selected==true){
$selectedstr="selected";
}
$theoptions="<option value='".$thevalue."' ".$selectedstr.">";
$theoptions=$theoptions.$thelabel."</option>";
return $theoptions;
}
}

?>