View Single Post
  #2  
Old 02-27-2016, 11:17 AM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 127,479
Kyttias is on a distinguished road
Default

How about a function like this?

PHP Code:
public function takeItem($item$qty){
    
$mysidia Registry::get("mysidia");
    
$owned $mysidia->db->select("inventory", array("quantity"), "itemname ='{$item}' and owner ='{$mysidia->user->username}'")->fetchColumn();
    if (
$owned >= $qty){ 
        
// If the user owns $qty amount or more...
        
if ($owned == $qty){ 
            
// If the user has exactly $qty left, delete the whole row.
             
$mysidia->db->delete("inventory""itemname='{$item}' and owner='{$mysidia->user->username}'");
         } else {
            
// Subtract $qty from user's inventory.
            
$owned_left $owned $qty;
            
$mysidia->db->update("inventory", array("quantity" => $owned_left), "itemname ='{$item}' and owner='{$mysidia->user->username}'");  
        }
    } else {
        throw new 
Exception("Sorry, you only have {$owned} {$item}, you need {$qty}!"); 
    }

__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it.
Reply With Quote