<?
/*
array GetItems ( [ int user id ] )
User id can be specified as first and only argument, default value is actually logged user.
Returns an array to be used this way : $array[item_id][property] = value.
*/
function GetItems($uid = $pun_user['id'])
{
//Use punbb database layer class
global $db;
//Secures function arguments
$uid = intval($uid);
//Item selection SQL Query string
$sql = "SELECT `ip`.`property`, `ipl`.`value`, `ui`.`id` AS `id_item`
FROM `user_items` AS `ui`
LEFT JOIN `item_properties_list` AS ipl ON `ipl`.`id_item` = `ui`.`id_item`
LEFT JOIN `item_properties` AS `ip` ON `ip`.`id` = `ipl`.`id_property`
WHERE `ui`.`id_user` = '".$uid."'";
//Execution of query string, or explain error
$res_items = $db->query($sql) or error('Unable to fetch item list for user : '.$uid.' <br /><br />SQL Query String :<br />'.$sql, __FILE__, __LINE__, $db->error());
//Fetch the result of SQL query, makes the array to return
while ($fetched_data=$db->fetch_assoc($res_items))
$items[$fetched_data['id_item']][$fetched_data['property']] = $fetched_data['value'];
//Returns item array and ends function.
return $items;
}
/*
bool AddItem ( int item id [, int quantity [, int user id ] ] )
Item id must be specified as first argument.
Quantity to add may be specified as second argument, default is one.
User id may be specified as third argument, if not, actually logged user is used.
Returns the number of inserted items.
*/
function AddItem ( $iid, $quantity_to_add = 1, $uid = $pun_user['id'] )
{
//Use punbb database layer class
global $db;
//Secures function arguments
$uid = intval($uid);
$iid = intval($iid);
$quantity_to_add = intval($quantity_to_add);
//Quantity of items selection SQL string
$sql = "SELECT `quantity` FROM `user_items`
WHERE `id_user` = '".$uid."'
AND `id_item` = '".$iid."'";
//Executes the query on the database
$res_quantity = $db->query($sql) or error('Unable to fetch quantity of item '.$iid.' of user : '.$uid.' <br /><br />SQL Query String :<br />'.$sql, __FILE__, __LINE__, $db->error());
//If a row was found in user_items : if such a user had such an item
if ($db->num_rows($res_quantity))
{
//Calculates updated quantity
$actual_quantity = $db->fetch_assoc($res_quantity);
$quantity = $quantity_to_add + $actual_quantity['quantity'];
//Quantity of such an item for such a user SQL query string
$sql = "UPDATE `user_items` SET
`quantity` = '".$quantity."'
WHERE `id_user` = '".$uid."'
AND `id_items` = '".$iid."'
LIMIT 1";
}
else
//Insert of such an item for such a user SQL query string
$sql = "INSERT INTO `user_items` SET
`ìd_item` = '".$iid."'
`id_user` = '".$uid."'
`quantity` = '".$quantity_to_add."'";
//Execution of SQL query string
$res_item_add = $db->query($sql) or error('Unable to insert/increment quantity of item '.$iid.' to user : '.$uid.' <br /><br />SQL Query String :<br />'.$sql, __FILE__, __LINE__, $db->error());
//Returns true and ends function.
return true;
}