[php] Cooking system (solved)

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

[php] Cooking system (solved)

Post 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.
Last edited by Perry on Mon Jun 20, 2011 12:25 pm, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [php] Cooking system

Post 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.
The indelible lord of tl;dr
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Re: [php] Cooking system

Post 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?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [php] Cooking system

Post by Jackolantern »

Yep, that looks good!

However, what is that random ID being used for? It is not supposed to be unique, right?
The indelible lord of tl;dr
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Re: [php] Cooking system

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [php] Cooking system

Post 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 :)
The indelible lord of tl;dr
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Re: [php] Cooking system

Post by Perry »

Yeah it seems to work very good. Thanks again for the help :D
Post Reply

Return to “Beginner Help and Support”