Check quantity of an item

Forum
Last Post
Threads / Messages

Abronsyth

A Headache Embodied
Member
Joined
Aug 25, 2011
Messages
1,012
Points
36
Location
NY
Mysidian Dollar
73,285
I'm working on building very simple quests and I'm wondering if anyone knows how to ask the database to check if the quantity of a specific item a user has is, say 10 or more?

Edit: And then also remove the specified quantity of that item from the users inventory?

So say it's a quest and they need to turn in 10 rocks, first it verifies that they have 10 or more rocks, and then it removes 10 rocks from their inventory if they indeed have 10 or more.

Thank you,
-Abron
 
Last edited:
How about a function like this?

PHP:
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}!"); 
	}
}
 
OK I know I did something terribly unintelligent to get this error;
Fatal error: Uncaught exception 'Exception' with message 'Sorry, you only have Rock Cone, you need 10!' in /home/arieng/catisserie.net/view/completeq1view.php:48 Stack trace: #0 /home/arieng/catisserie.net/view/completeq1view.php(13): Completeq1View->takeItem('Rock Cone', 10) #1 /home/arieng/catisserie.net/classes/class_frontcontroller.php(100): Completeq1View->index() #2 /home/arieng/catisserie.net/index.php(74): FrontController->render() #3 /home/arieng/catisserie.net/index.php(78): IndexController::main() #4 {main} thrown in /home/arieng/catisserie.net/view/completeq1view.php on line 48

I'm sure this will make you cringe to look at but this is the file thus far;
PHP:
<?php 

class Completeq1View extends View{ 
     
    public function index(){ 
        $mysidia = Registry::get("mysidia"); 
        $document = $this->document; 
        $document->setTitle("Turn in 10 Rock Cones");

			// Allow user to complete quest if they have not yet.  
			if ($mysidia->user->quest1 = "no") {  				
						$document->add(new Comment("<center>Text Text Text<center>", FALSE));
					$this->takeItem("Rock Cone",10);
					}		
			else{
				$document->add(new Comment("The man furrows his brows at you, <b>sorry, no reward if you don't have any rock cones.</b>", FALSE));
				}
	}

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){ 
			$amount = 100;
			$mysidia->user->changecash($amount);					
				$document->add(new Comment("<center>You return to the scientit's camp and present the head scientist with 10 rock cones.<br>
				<b>Wow, thank you! Here are some kibs!</b><br>
				You've obtained {$amount} kibs!</center>", FALSE));
				// Update that they have done the quest 
				$mysidia->db->update("users", array("quest1" => "yes"), "username = '{$mysidia->user->username}'");
            // If the user has exactly $qty left, delete the whole row.
             $mysidia->db->delete("inventory", "itemname='{$item}' and owner='{$mysidia->user->username}'");
			$amount = 100;
			$mysidia->user->changecash($amount);					
				$document->add(new Comment("<center>You return to the scientit's camp and present the head scientist with 10 rock cones.<br>
				<b>Wow, thank you! Here are some kibs!</b><br>
				You've obtained {$amount} kibs!</center>", FALSE));
				// Update that they have done the quest 
				$mysidia->db->update("users", array("quest1" => "yes"), "username = '{$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}!"); 
    }
}	

    
}
?>

Edit;
OK, may be because I didn't actually define $qty at any point...not really sure how to, though @_@ I know that you can see if a user owns a certain item, but not sure how to define $qty to check how many of that item they own.
 
Last edited:
No, you did it right, with takeItem("Rock Cone",10); that 10 is defining $qty in the function. I wasn't entirely sure on how to construct the end of the function... rather than

PHP:
throw new Exception("Sorry, you only have {$owned} {$item}, you need {$qty}!");

Try... either:
PHP:
echo "Sorry, you only have {$owned} {$item}, you need {$qty}!";
or maybe just
PHP:
return false;

I'm not totally sure what to do on failure, so I just had it throw an Exception.
 
OK, thank you! So no longer getting the error, but the text doesn't want to actually appear on the page XD

Rather it's appearing right above all of the page, oops!
 
Yeah, I figured, that's what an echo does. You'll have to decide what you want to actually be doing as a result. It all depends on what you actually wanted to be doing with the function...?

I would suggest this:

PHP:
$takeItem = $this->takeItem("Rock Cone", 10);

if ($takeItem){
    $document->add(new Comment("Thank you for bringing me those Rock Cones!", FALSE));
} else {
    $document->add(new Comment("Hey, this isn't enough Rock Cones!", FALSE));
}

but you'll need to have the function return a true or false value (I've added one or the other here to all possible outcomes):
PHP:
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}'");
             return true;
         } 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}'"); 
            return true;
        }
    } else {
        return false;
    }
}

I would NOT modify the function above as you did - if you want to reward the user with something, you should do it in the first half of this post after the success/fail message. That way the code is reusable regardless of the reward.
 
Last edited:

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,280
Messages
33,130
Members
1,603
Latest member
Monako
BETA

Latest Threads

Latest Posts

Top