PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Mar 06, 2007 4:33 am
why is the array named in this way?
Code: Select all
<?php
error_reporting(E_ALL);
require ('../../../functions/database.php');
class exportPLU
{
function generateTransactionFile ($sessionID) {
$query = "SELECT *
FROM `cart`
WHERE `sessionID` = '{$sessionID}'";
echo $query;
$result = mysql_query ($query) or die (mysql_error());
while ($a = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$array['sessionID'] = $a['sessionID'];
$array['cartContents'] = $a['cartContents'];
}
$array['cartContents'] = unserialize($array['cartContents']);
return $array;
}
function writeTransactionFile () {
//
}
}
?>
<?
$exportPLU = new exportPLU ();
$gen = $exportPLU->generateTransactionFile('7597b6196e18bd634eae35400b1cb779');
print_r ($gen);
?>
output
Code: Select all
Array
(
[sessionID] => 7597b6196e18bd634eae35400b1cb779
[cartContents] => __PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => cart
[cart] => Array
(
[contents] => Array
(
[0] => Array
(
[productStockCode] => 72490
[productColour] => Navy
[productOnlinePrice] => 29.9900
[productBrandName] => 80s Casuals
[productCategoryName] => Tee
[productFullName] => 80s Casuals Trimm Tab Tee
[productQuantity] => 3
[productSize] => Lge
[productBarcode] => 72490040
)
[1] => Array
(
[productStockCode] => 71815
[productColour] => Brown
[productOnlinePrice] => 119.9900
[productBrandName] => Michiko Koshino Yen Jeans
[productCategoryName] => Knit
[productFullName] => Michiko Koshino Yen Jeans Cracked Knit
[productQuantity] => 1
[productSize] => Med
[productBarcode] => 71815030
)
[2] => Array
(
[productStockCode] => 72583
[productColour] => Denim
[productOnlinePrice] => 94.9900
[productBrandName] => Lacoste
[productCategoryName] => Jeans
[productFullName] => Lacoste Something Jeans
[productQuantity] => 1
[productSize] => 32
[productBarcode] => 72583030
)
)
[totalPrice] => 244.97
)
)
)
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Mar 06, 2007 4:44 am
php did not know the class
cart when you called unserialize. You have to define the class before unserializing the data.
e.g.
Code: Select all
<?php
$s = 'O:3:"foo":1:{s:3:"bar";i:3;}';
echo '#1: '; print_r(unserialize($s));
if (true) {
class foo {
var $bar;
}
}
echo '#2: '; print_r(unserialize($s));
?>prints
Code: Select all
#1: __PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => foo
[bar] => 3
)
#2: foo Object
(
[bar] => 3
)
Last edited by
volka on Tue Mar 06, 2007 4:49 am, edited 2 times in total.
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Mar 06, 2007 4:49 am
when i attempt to instantiate it it says non-existant class
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Mar 06, 2007 4:51 am
Same reason
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Mar 06, 2007 4:58 am
so what would you recommend i do?
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Mar 06, 2007 5:01 am
declaring/defining the class (once) before using unserialize().
Code: Select all
$o = unserialize($data); // <- no
class xyz {
var ...
function ...
....
}
$o = unserialize($data); // <- yes
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Mar 06, 2007 5:13 am
sorry, its just not happening for me today, keeps givnig me the same error message?
Can you point me in the right direction at least
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Mar 06, 2007 5:15 am
Where's the class
cart defined? That's some code starting with
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Mar 06, 2007 5:18 am
Code: Select all
function generateTransactionFile ($sessionID) {
$query = "SELECT *
FROM `cart`
WHERE `sessionID` = '{$sessionID}'";
echo $query;
$result = mysql_query ($query) or die (mysql_error());
while ($a = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$array['sessionID'] = $a['sessionID'];
$cartContents = new cart ();
#$array['cartContents'] = unserialise ($cartContents']);
}
return $array;
}
not working, obviously as im not sure what to reference it as, the cartContents is jiust an object saved into a DB
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Mar 06, 2007 5:20 am
Hm, did I ask for code with new cart(); in it? NO!
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Mar 06, 2007 5:26 am
Code: Select all
<?php
#require('../../functions/database.php');
error_reporting (E_ALL);
class cart
{
function cart () {
@session_start ();
}
function add ($productStockCode, $size, $quantity) {
$this->cart['contents'][] = $this->getItemDetails ($productStockCode, $size, $quantity);
$this->update ();
}
function remove ($productStockCode) {
for ($i = 0; $i < count ($this->cart['contents']); $i++) {
if ($this->cart['contents'][$i]['productStockCode'] == $productStockCode) {
unset ($this->cart['contents'][$i]);
}
}
$this->cart['contents'] = array_values($this->cart['contents']);
$this->update ();
}
function update () {
$totalPrice = 0;
for ($i = 0; $i < count ($this->cart['contents']); $i++) {
$totalPrice = ($totalPrice + $this->cart['contents'][$i]['productOnlinePrice']);
}
$this->cart['totalPrice'] = $totalPrice;
}
function saveCart () {
$sessionID = session_id ();
$savedCart = serialize($this);
$currentTime = time ();
//check to see if there is a basket
if (isset($this->cart['contents'])) {
// check to see if this sessionID is already in the database
$query = "SELECT *
FROM `cart`
WHERE `sessionID` = '{$sessionID}'
LIMIT 1";
$result = mysql_query ($query) or die (mysql_error());
if (mysql_num_rows($result) == 1) {
// its already there, an update is required
$query = "UPDATE `cart`
SET `cartContents` = '{$savedCart}',
`lastActive` = '{$currentTime}'
WHERE `sessionID` = '{$sessionID}'
LIMIT 1";
} else {
// insert
$query = "INSERT INTO `cart`
(`sessionID` , `cartContents`, `lastActive`)
VALUES
('{$sessionID}', '{$savedCart}', '{$currentTime}')";
}
$result = mysql_query ($query) or die (mysql_error());
$this->cleanCartTable();
}
}
function loadCart () {
$sessionID = session_id ();
$currentTime = time ();
$query = "SELECT *
FROM `cart`
WHERE `sessionID` = '{$sessionID}'
LIMIT 1";
$result = mysql_query ($query) or die (mysql_error());
if (mysql_num_rows($result) == 1) {
// this person has items in there basket, we need to load it
while ($array = mysql_fetch_array($result, MYSQL_ASSOC)) {
$this = unserialize($array['cartContents']);
}
// update the last Active Time
$query = "UPDATE `cart`
SET `lastActive` = '{$currentTime}'
WHERE `sessionID` = '{$sessionID}'
LIMIT 1";
$result = mysql_query ($query) or die (mysql_error());
}
}
function getBarcode ($productStockCode, $size) {
$query = "SELECT * FROM `stock_levels` WHERE productStockCode = '{$productStockCode}'";
$result = mysql_query ($query) or die ('Error: '.mysql_error().'');
while ($array = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$a['size'] = $array['size'];
$a['quantity'] = $array['quantity'];
}
$sizeArray = explode(",", $a['size']);
$quanArray = explode(",", $a['quantity']);
for ($i = 0; $i < count ($sizeArray); $i++) {
if ($sizeArray[$i] == $size) {
$sizeCode = ($i + 2);
return ''.$productStockCode.'0'.$sizeCode.'0';
}
}
}
function cleanCartTable () {
// delete automatically after last active more than 60 minutes
$deleteTime = (time() - (60 * 60));
$query = "DELETE FROM `cart`
WHERE `lastActive` < '{$deleteTime}'";
$result = mysql_query ($query) or die (mysql_error());
}
function show () {
$orderTotal = 0;
$itemTotal = 0;
echo '<pre>';
for ($i = 0; $i < count ($this->cart['contents']); $i++) {
$itemTotal = ($this->cart['contents'][$i]['productOnlinePrice'] * $this->cart['contents'][$i]['quantity']);
$orderTotal = ($orderTotal + $itemTotal);
echo ''.$this->cart['contents'][$i]['productFullName'].' x '.$this->cart['contents'][$i]['quantity'].'';
echo ' - £'.number_format($itemTotal, 2).'<br>';
}
echo 'Total: '.$orderTotal.'<br><br>';
echo '</pre>';
}
function getItemDetails ($productStockCode, $size, $quantity) {
$query = 'SELECT *
FROM `products`
WHERE `productStockCode` = '.$productStockCode.'
LIMIT 1';
$result = mysql_query ($query) or die (mysql_error());
if (mysql_num_rows($result) >= 1) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($row as $k=>$v) {
$productDetails[$k] = $v;
}
}
// now get name for category and brand
$productDetails['productBrandName'] = $this->getProductBrand ($productDetails['productBrandID']);
$productDetails['productCategoryName'] = $this->getProductCategory ($productDetails['productCategoryID']);
$productDetails['productFullName'] = $this->getFullItemName($productDetails);
$productDetails['productQuantity'] = $quantity;
$productDetails['productSize'] = $size;
$productDetails['productBarcode'] = $this->getBarcode ($productStockCode, $productDetails['productSize']);
}
unset($productDetails['productID']);
unset($productDetails['productTemp']);
unset($productDetails['productOnline']);
unset($productDetails['productDescription']);
unset($productDetails['productOriginalPrice']);
unset($productDetails['productName']);
unset($productDetails['productBrandID']);
unset($productDetails['productCategoryID']);
unset($productDetails['productDateAdded']);
// we now need to build the full barcode number for RS
return $productDetails;
}
function getProductBrand ($brandID) {
$query = 'SELECT `brandName`
FROM `brands`
WHERE `brandID` = '.$brandID.'
LIMIT 1';
$result = mysql_query ($query) or die (mysql_error());
while ($array = mysql_fetch_array($result, MYSQL_ASSOC)) {
return $array['brandName'];
}
}
function getProductCategory ($categoryID) {
$query = 'SELECT `categoryProductName`
FROM `categories`
WHERE `categoryID` = '.$categoryID.'
LIMIT 1';
$result = mysql_query ($query) or die (mysql_error());
while ($array = mysql_fetch_array($result, MYSQL_ASSOC)) {
return $array['categoryProductName'];
}
}
function getFullItemName ($productDetails) {
return ''.$productDetails['productBrandName'].' '.$productDetails['productName'].' '.$productDetails['productCategoryName'].'';
}
function getCurrentCarts () {
#$this->cleanCartTable();
$query = "SELECT *
FROM `cart`";
$result = mysql_query ($query) or die (mysql_error());
if (mysql_num_rows($result) >= 1) {
while ($array = mysql_fetch_array($result, MYSQL_ASSOC)) {
if (!isset($i)) {
$i = 1;
}
$a[$i]['sessionID'] = $array['sessionID'];
$a[$i]['cartContents'] = unserialize($array['cartContents']);
$a[$i]['lastActive'] = $array['lastActive'];
$i++;
}
return $a;
} else {
return FALSE;
}
}
}
function getMicrotime () {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
#<!---------------------Usage Example WITHOUT cart ---------------------------!>
#$cart = new cart ();
#$cart->add (72490, 'Lge', 3); // add 1 x productCode 72597
#$cart->add (71815, 'Med', 1); // add 1 x productCode 72400
#$cart->add (72583, '32', 1); // add 1 x productCode 72400
#$cart-> saveCart();
#print_r ($cart);
?>
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Mar 06, 2007 5:30 am
And this file must be included/required/whatever before you can unserialize an object of this class.
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Mar 06, 2007 7:53 am
ok got that working, now i have the array
Code: Select all
["sessionID"]=>
string(32) "7597b6196e18bd634eae35400b1cb779"
["cartContents"]=>
object(cart)(1) {
["cart"]=>
array(2) {
["contents"]=>
array(3) {
[0]=>
array(9) {
["productStockCode"]=>
string(5) "72490"
["productColour"]=>
string(4) "Navy"
["productOnlinePrice"]=>
string(7) "29.9900"
["productBrandName"]=>
string(11) "80s Casuals"
["productCategoryName"]=>
string(3) "Tee"
["productFullName"]=>
string(25) "80s Casuals Trimm Tab Tee"
["productQuantity"]=>
int(3)
["productSize"]=>
string(3) "Lge"
["productBarcode"]=>
string(8) "72490040"
}
[1]=>
array(9) {
["productStockCode"]=>
string(5) "71815"
["productColour"]=>
string(5) "Brown"
["productOnlinePrice"]=>
string(8) "119.9900"
["productBrandName"]=>
string(25) "Michiko Koshino Yen Jeans"
["productCategoryName"]=>
string(4) "Knit"
["productFullName"]=>
string(38) "Michiko Koshino Yen Jeans Cracked Knit"
["productQuantity"]=>
int(1)
["productSize"]=>
string(3) "Med"
["productBarcode"]=>
string(8) "71815030"
}
[2]=>
array(9) {
["productStockCode"]=>
string(5) "72583"
["productColour"]=>
string(5) "Denim"
["productOnlinePrice"]=>
string(7) "94.9900"
["productBrandName"]=>
string(7) "Lacoste"
["productCategoryName"]=>
string(5) "Jeans"
["productFullName"]=>
string(23) "Lacoste Something Jeans"
["productQuantity"]=>
int(1)
["productSize"]=>
string(2) "32"
["productBarcode"]=>
string(8) "72583030"
}
}
["totalPrice"]=>
float(244.97)
}
}
}
how would i actually go about accessing the array element "["productStockCode"]" because no matter what i do i keep getting null when i try and assign the [cart] array to another variable
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Mar 06, 2007 7:57 am
You might want to add a new method to class cart granting access to $this->cart['contents']
malcolmboston
DevNet Resident
Posts: 1826 Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK
Post
by malcolmboston » Tue Mar 06, 2007 8:07 am
im sorry, what do you mean?