I'm over halfway done making my online text-based role-playing game, and I've come to the crafting portion. At first I thought it'd be no problem. Compare resources on the ground to the required resources that make the desired item.
But, I've never had to compare so specifically before.
I've looked into doing this both in SQL and in PHP, SQL being preferred as I'm guessing it's probably quicker.
I've looked into array_diff, and array_intersect, I've done joins, left joins, right joins, all within difference re-evaluated contexts; but I'm just not getting what I need.
Here's what I've got in the database; right now I'm just trying to echo out what cooked food you can make ONLY if you have ALL the required resources on the ground to make it. I've gotten it to echo, but instead of only echoing if all the required resources are present, it echos all food that uses a resource at least once that if it is on the ground.
Table: edible_resources
Columns: edible_resource_id, edible_resource_name, hunger_points, degeneration_id
Table: edible_ground
Columns: id, resource, amount, location
Table: req_crafting_edible
Columns: req_crafting_edible_id, edible_resource_id, req_resource_amount, created_item_id
Table: items
Columns: item_id, item_name, degeneration_id, is_grounded, is_stackable, can_equip, can_edit
This is one thing I've tried, though I've gone through many; this one uses in_array.
//get all craftable items, and all edible resources on the ground
//iterate through each craftable item, gather it's required resources into an array
//to do that we can use a while loop for all the rows in the edible crafting table, then use a select statement
//with each craftable id which will give us multiple rows for each required item based on each created_item_id
//then put those rows into an array and use in array to see if those resource ids are on both the ground and in required resources
$get_ground = mysql_query("SELECT resource FROM edible_ground WHERE location = 2020");
$ground = mysql_fetch_array($get_ground);
$get_req = mysql_query("SELECT * FROM req_crafting_edible ORDER BY created_item_id");
while($required = mysql_fetch_array($get_req)){
$get_each = mysql_query("SELECT edible_resource_id FROM req_crafting_edible WHERE created_item_id = $required[created_item_id]");
while($each = mysql_fetch_array($get_each)){
//now we have the required resources specific to each craftable item, so we put them in an array
if (in_array($each['edible_resource_id'], $each) && in_array($ground['resource'], $ground)){
$get_make = mysql_query("SELECT item_name FROM items JOIN req_crafting_edible ON items.item_id = req_crafting_edible.created_item_id
WHERE edible_resource_id = $each[edible_resource_id]");
while($can_make = mysql_fetch_array($get_make)){
echo $can_make['item_name'].'<br>';
}
}else{
echo 'There are no matches';
}
}
}
You can kind of see what I'm going for here, but.. I think my logic only looks good on paper. When I use it there's a gigantic list of those items and their names being repeated (sort of). I suspect placement of my while loops (it gets confusing), and the items that are being listed shouldnt be. Some items are listed that don't even use the items on the ground, like mushroom soup. They aren't even repeating in a set way. The repeats show different instances of some foods almost every time. Which. Again. Probably dumb placement of the while loops.
Part of the output:
Quote
Mushroom Soup
Charred Mushroom
Gumbo
Fish Soup
Seafood Soup
Charred Fish
Grilled Fish
Baked Fish
Sashimi
Sushi
Udon
Fish Soup
Seafood Soup
Charred Fish
Grilled Fish
Baked Fish
Sashimi
Sushi
Udon
Seafood Soup
Charred Scallops
Grilled Scallops
Boiled Scallops
Baked Scallops
Udon
Seafood Soup
Charred Shrimp
Grilled Shrimp
Boiled Shrimp
Baked Shrimp
Udon
Charred Mushroom
Gumbo
Fish Soup
Seafood Soup
Charred Fish
Grilled Fish
Baked Fish
Sashimi
Sushi
Udon
Fish Soup
Seafood Soup
Charred Fish
Grilled Fish
Baked Fish
Sashimi
Sushi
Udon
Seafood Soup
Charred Scallops
Grilled Scallops
Boiled Scallops
Baked Scallops
Udon
Seafood Soup
Charred Shrimp
Grilled Shrimp
Boiled Shrimp
Baked Shrimp
Udon
Can anyone help me out here pretty please
I'd be grateful for any giant brains out there.
(:

New Topic/Question
Reply



MultiQuote








|