navid3d_69
دوشنبه 09 اردیبهشت 1392, 21:36 عصر
سلام من با این کلاس می خوام سبد خرید درست کنم و برای هر سطح محصولات جدا
مثلا محصولات مربوط به pro1 جدا pro2 و... همه جدا
<?php
class Cart
{
///////////////////
// PROPERTIES
///////////////////
/*
* id of product
* @var int
*/
public $id;
/*
* num of product
* @var int
*/
public $num;
/*
* unit price of product (fee فی فروش)
* @var int
*/
public $unit;
/*
* adress of thumb image of product
* @var string
*/
public $thumb='';
/*
* the sum of whole the products price in cart
* @var int
*/
public $total;
/*
* errors stors in this property
* you can access to errors with getErrors method
* @array
* @access private
*/
private $errors=array();
public $type;
/*
* for storing values of item in array
* @array
* @access private
*/
private $arr=array();
///////////////////
// METHODS
///////////////////
/*
* if id is repetitive, upadate num or unit of product
* else add item (id, num, unit, totalItem, thumb) of product in cart
* @ return true if succeed
* @ else return false and you can use getErrors() for finding errors
*/
public function save ()
{
// for set id, num, ... in array
$this->toArray();
if($this->arr['id']==0 or $this->arr['num']==0)
{
$this->errors[]='id or num cannt be equal 0';
}
if(count($this->errors)==0)
{
$_SESSION['cart'][$this->type]=array(
'id' =>$this->arr['id'],
'num'=>$this->arr['num'],
'unit'=>$this->arr['unit'],
'totalItem'=>$this->arr['num']*$this->arr['unit'],
'thumb'=>$this->arr['thumb'],
);
$this->setTotal();
return true;
}
else
return false;
}
/*
* for remove a item from cart
* @params int $id
* @ return true if succeed
* @ else return false and you can use getErrors() for finding errors
*/
public function destroy ($id)
{
if(isset($_SESSION['cart'][$id]))
{
unset($_SESSION['cart'][$id]);
$this->setTotal();
return true;
}
else
{
$this->errors[]='there is no id that you want to remove in cart';
return false;
}
}
/*
* remove whole of items in cart
*/
public function destroyAll ()
{
unset($_SESSION['cart']);
}
/*
* getAll items in cart
* you can set one three kind in $dataType to export. array, xml, json
* @return items in cart
* @return array (0=>array(id=>array(num=>num,price=>price,totalItem=>totalItem,thumb=>thumb)),1=>...)
* @return json
* @return xml
*/
public function getAll ($dataType='array')
{
if(isset($_SESSION['cart']))
{
switch (strtolower ($dataType) )
{
case 'json':
return json_encode($_SESSION['cart']);
break;
case 'array':
default :
return $_SESSION['cart'];
break;
}
}
else
{
$this->errors='cart is empty';
return $this->getErrors();
}
}
/*
* get information(num, price, totalItem, thumb) about product in cart with id
* @params int $id
* @return array (num=>num,price=>price,totalItem=>totalItem,thumb=>thumb)
*/
public function getById($id)
{
if(isset($_SESSION['cart'][$id]))
return $_SESSION['cart'][$id];
else
{
$this->errors='undifinde id='.$id.' in cart';
return $this->getErrors();
}
}
/*
* set the value requires for items
* @param array $arr(id=>id,num=>num,price=>price,thumb=>thumb)
*/
public function set ($arr,$type)
{
$arr=array_merge($this->toArray(),$arr);
$this->type = $type;
$this->id=$arr['id'];
$this->num=$arr['num'];
$this->unit=$arr['unit'];
$this->thumb=$arr['thumb'];
}
/*
* if occur errors, with this method you can know about errors
* @return errors
*/
public function getErrors ()
{
return $this->errors;
}
/*
* set value in array
* @ return array of information about item
* @ access private
*/
private function toArray()
{
return $this->arr=array
(
'id'=>(int) $this->id,
'num'=>(int) $this->num,
'unit'=>(int) $this->unit,
'thumb'=> $this->thumb,
);
}
/*
* calculate the total of price products in cart
* you can access total by total property ($this->total)
* @ set total in total property
* @ access private
*/
private function setTotal ()
{
$this->total=0;
foreach($_SESSION['cart'] as $arr)
{
$this->total +=$arr['totalItem'];
}
}
}
حالا مشکلی که داره اینه که فقط یک بار سشن هارو درست می کنه
یعنی با این کد
<?php
session_start();
require 'Class/Cart.php';
$cart = new Cart();
$cart->destroyAll();
$cart->set(array(
'id' =>3,
'num' => 2,
'unit' => 3
),'pro1');
$cart->save();
$cart->set(array(
'id' =>1,
'num' => 2,
'unit' => 3
),'pro2');
$cart->set(array(
'id' =>2,
'num' => 2,
'unit' => 3
),'pro2');
$cart->save();
echo '<pre>';
print_r($cart->getAll());
echo '</pre>';
?>
این خروجی رو میده
Array
(
[pro1] => Array
(
[id] => 3
[num] => 2
[unit] => 3
[totalItem] => 6
[thumb] =>
)
[pro2] => Array
(
[id] => 2
[num] => 2
[unit] => 3
[totalItem] => 6
[thumb] =>
)
)
و مثلا مقدار pro2 باید 2تا آیدی ازش اضافه بشه ولی آیدی آخر که ست شده فقط اضافه میشه
من هر کاری کردم نشد حلش کنم لطفا کمک کنید ممنون
مثلا محصولات مربوط به pro1 جدا pro2 و... همه جدا
<?php
class Cart
{
///////////////////
// PROPERTIES
///////////////////
/*
* id of product
* @var int
*/
public $id;
/*
* num of product
* @var int
*/
public $num;
/*
* unit price of product (fee فی فروش)
* @var int
*/
public $unit;
/*
* adress of thumb image of product
* @var string
*/
public $thumb='';
/*
* the sum of whole the products price in cart
* @var int
*/
public $total;
/*
* errors stors in this property
* you can access to errors with getErrors method
* @array
* @access private
*/
private $errors=array();
public $type;
/*
* for storing values of item in array
* @array
* @access private
*/
private $arr=array();
///////////////////
// METHODS
///////////////////
/*
* if id is repetitive, upadate num or unit of product
* else add item (id, num, unit, totalItem, thumb) of product in cart
* @ return true if succeed
* @ else return false and you can use getErrors() for finding errors
*/
public function save ()
{
// for set id, num, ... in array
$this->toArray();
if($this->arr['id']==0 or $this->arr['num']==0)
{
$this->errors[]='id or num cannt be equal 0';
}
if(count($this->errors)==0)
{
$_SESSION['cart'][$this->type]=array(
'id' =>$this->arr['id'],
'num'=>$this->arr['num'],
'unit'=>$this->arr['unit'],
'totalItem'=>$this->arr['num']*$this->arr['unit'],
'thumb'=>$this->arr['thumb'],
);
$this->setTotal();
return true;
}
else
return false;
}
/*
* for remove a item from cart
* @params int $id
* @ return true if succeed
* @ else return false and you can use getErrors() for finding errors
*/
public function destroy ($id)
{
if(isset($_SESSION['cart'][$id]))
{
unset($_SESSION['cart'][$id]);
$this->setTotal();
return true;
}
else
{
$this->errors[]='there is no id that you want to remove in cart';
return false;
}
}
/*
* remove whole of items in cart
*/
public function destroyAll ()
{
unset($_SESSION['cart']);
}
/*
* getAll items in cart
* you can set one three kind in $dataType to export. array, xml, json
* @return items in cart
* @return array (0=>array(id=>array(num=>num,price=>price,totalItem=>totalItem,thumb=>thumb)),1=>...)
* @return json
* @return xml
*/
public function getAll ($dataType='array')
{
if(isset($_SESSION['cart']))
{
switch (strtolower ($dataType) )
{
case 'json':
return json_encode($_SESSION['cart']);
break;
case 'array':
default :
return $_SESSION['cart'];
break;
}
}
else
{
$this->errors='cart is empty';
return $this->getErrors();
}
}
/*
* get information(num, price, totalItem, thumb) about product in cart with id
* @params int $id
* @return array (num=>num,price=>price,totalItem=>totalItem,thumb=>thumb)
*/
public function getById($id)
{
if(isset($_SESSION['cart'][$id]))
return $_SESSION['cart'][$id];
else
{
$this->errors='undifinde id='.$id.' in cart';
return $this->getErrors();
}
}
/*
* set the value requires for items
* @param array $arr(id=>id,num=>num,price=>price,thumb=>thumb)
*/
public function set ($arr,$type)
{
$arr=array_merge($this->toArray(),$arr);
$this->type = $type;
$this->id=$arr['id'];
$this->num=$arr['num'];
$this->unit=$arr['unit'];
$this->thumb=$arr['thumb'];
}
/*
* if occur errors, with this method you can know about errors
* @return errors
*/
public function getErrors ()
{
return $this->errors;
}
/*
* set value in array
* @ return array of information about item
* @ access private
*/
private function toArray()
{
return $this->arr=array
(
'id'=>(int) $this->id,
'num'=>(int) $this->num,
'unit'=>(int) $this->unit,
'thumb'=> $this->thumb,
);
}
/*
* calculate the total of price products in cart
* you can access total by total property ($this->total)
* @ set total in total property
* @ access private
*/
private function setTotal ()
{
$this->total=0;
foreach($_SESSION['cart'] as $arr)
{
$this->total +=$arr['totalItem'];
}
}
}
حالا مشکلی که داره اینه که فقط یک بار سشن هارو درست می کنه
یعنی با این کد
<?php
session_start();
require 'Class/Cart.php';
$cart = new Cart();
$cart->destroyAll();
$cart->set(array(
'id' =>3,
'num' => 2,
'unit' => 3
),'pro1');
$cart->save();
$cart->set(array(
'id' =>1,
'num' => 2,
'unit' => 3
),'pro2');
$cart->set(array(
'id' =>2,
'num' => 2,
'unit' => 3
),'pro2');
$cart->save();
echo '<pre>';
print_r($cart->getAll());
echo '</pre>';
?>
این خروجی رو میده
Array
(
[pro1] => Array
(
[id] => 3
[num] => 2
[unit] => 3
[totalItem] => 6
[thumb] =>
)
[pro2] => Array
(
[id] => 2
[num] => 2
[unit] => 3
[totalItem] => 6
[thumb] =>
)
)
و مثلا مقدار pro2 باید 2تا آیدی ازش اضافه بشه ولی آیدی آخر که ست شده فقط اضافه میشه
من هر کاری کردم نشد حلش کنم لطفا کمک کنید ممنون