Page 1 of 1

[php] Cooking system (solved)

Posted: Mon Jun 20, 2011 12:36 am
by Perry
I have been building a cooking system and it is almost finished except I don't know how I would add the cooked item to the inventory.

My problem is that so far I have been adding generic items that are all the same, but when someone cooks something the health bonus can be different based on how it was cooked, what it was cooked with, etc.

I am not sure how to approach this. I made a table called crafted_foods and when a food is made I add all its stats to that table. My plan was to draw from that like I would when someone received an item from mining or harvesting and then insert it into the inventory. My problem is that with the items not having an id number until they are created, I do not know how to put them in the inventory.

I know that probably does not make sense so let me try and describe it with code.

When an item is successfully cooked I first run this:

Code: Select all

$SQL1 = "INSERT into crafted_food(owner, name, class, energy, health, value, randid) VALUES ('$player', 'cooked food', 'crafted_food', '$stataddenergy', '$stataddhp', '$value', '$randid1')";
           mysql_query($SQL1) or die("could not add to crafted foods"); 
crafted_food is an empty table that only has items someone has cooked in it. The id for this table is just an auto increment int, which is why the item will not have an id until it is created.

My plan for getting it into the inventory was to have this right after the above query:

Code: Select all

$SQL2 = "INSERT into inventory(id, name, class, item_id, randid) VALUES ('$player','$name','crafted_food', '$item_id', '$randid2')";
           mysql_query($SQL2) or die("could not add to inventory"); 
The problem is that I don't have an item_id to use to keep track of it with since all ids are created when the item is created.

Does this make sense? How can I make the items distinguishable if I do not have an id? Is there a better way to do this? Is it something simple I am not seeing?

Thanks for any help.

Re: [php] Cooking system

Posted: Mon Jun 20, 2011 1:34 am
by Jackolantern
PHP has you covered :) mysql_insert_id() will return you the ID of a newly created entry in the database. So you can create and insert the newly created food into the master food item database, run this function to get the ID, and then add it to the player's inventory with that ID.

That is a required function when working with databases for exactly this reason, and pretty much every language has some kind of form of it. My main tip is to be sure not to spread your query and this function very far apart, because unfortunately, this function just pulls the last ID inserted instead of allowing you to pass in a database resource handler or acting as an alternative mysql_query() function, which are two of the possible ways it works in some other languages. If you spread them too far apart, you may unknowingly run another query and grab the wrong ID.

Re: [php] Cooking system

Posted: Mon Jun 20, 2011 1:48 am
by Perry
I got it working. Thanks :D

The way I have it now is all of it is all together.

Code: Select all

$SQL1 = "INSERT into crafted_food(owner, name, class, energy, health, value, randid) VALUES ('$player', 'cooked food', 'crafted_food', '$stataddenergy', '$stataddhp', '$value', '$randid1')";
           mysql_query($SQL1) or die("could not add to crafted foods");
               $randid2 = rand(100,1000000);
               $food_id = mysql_insert_id();
           $SQL2 = "INSERT into inventory(id, name, class, item_id, randid) VALUES ('$player,'cooked food','crafted_food', '$food_id', '$randid2')";
           mysql_query($SQL2) or die("could not add to inventory"); 
Did you mean actually have them close together in the code like this?

Re: [php] Cooking system

Posted: Mon Jun 20, 2011 3:52 am
by Jackolantern
Yep, that looks good!

However, what is that random ID being used for? It is not supposed to be unique, right?

Re: [php] Cooking system

Posted: Mon Jun 20, 2011 4:40 am
by Perry
Yes it is updated every time an item is used/obtained to keep people from refreshing over and over to give themselves an item several times.

I saw it in halls tutorials and thought it worked pretty good. Actually I just got the idea from him. I am not sure how he did it, but what I do is whenever I put an item into a players inventory I give it that and change it every time the item is adjusted/moved. It seems to work pretty good, but if you know a better method I am always open to changing things.

Re: [php] Cooking system

Posted: Mon Jun 20, 2011 5:03 am
by Jackolantern
Ohhh, ok, yes, then that works :) I was making sure it wasn't a number that was needing to stay unique over a long period of time (which it definitely wouldn't over the course of years). However, to prevent refresh-spamming, it works well, because the number basically only has to be unique for 2 "rolls". Once for the first time the item is gained, and then again for the refresh. The odds are astronomical for getting the same number twice in a row like that :)

Re: [php] Cooking system

Posted: Mon Jun 20, 2011 12:24 pm
by Perry
Yeah it seems to work very good. Thanks again for the help :D