Im trying to come up with some code to check whether or not a user owns a list of items. The list of items can be any length but will probably be no more than 5.
There are 2 tables involved here: qrequirements and inventory.
I’ve got as far as running a loop to check a user has each item, but I’m stuck on how to record the result of the ownership of each item and how this can be communicated to the user afterwards.
I might be going about all this the wrong way and maybe there’s a simpler way, I don’t know.
Any help would be great. Here’s my code so far:
<?php
// Get the list of required items
$result = mysql_query("select * from qrequirements where qid='2'");
// This might return E.G:
// item1 = '32'
// item2 = '24'
// item3 = '15'
// Loop through each item to check the user has it
while ($row = mysql_fetch_array($result)) {
$itemid = $row["itemid"];
$check_inv = mysql_query("select * from inventory where userid='$userid' AND item = $itemid");
$num_rows = mysql_num_rows($check_inv);
if($num_rows>=1){
// Something in here to confirm the item is owned by the user, while we check the others
}else{
echo "You do not have this item";
}
}
// Notify the user if they have all items required
?>
Determining the items that a user needs but does not have can be solved by a single query. The below query should accomplish that task.
If you also need to know which items the user does have then this similar query will accomplish that additional task.
Another useful query is one that would return the required items and an indicator of whether the user has that item. The item_count will be 0 if the user does not have the item and greater than 0 if the user does have the item.