Advice on a item calculator

C++, C#, Java, PHP, ect...
Post Reply
dave3460
Posts: 117
Joined: Wed Jul 01, 2009 2:13 pm

Advice on a item calculator

Post by dave3460 »

Could anyone give me any advice on the best way to do a Calculator section .
what i am trying to do is when you have a large order to do for say a clan/guild and you need different amounts .
like
5 breast plates needs 6 bars to make a plate
4 Gloves needs 4 bars to make a set of gloves
3 swords needs 5 bars to make a sword and also 1 peive of wood for handle
all made from different metals like
bronze that uses 2 raw ores to make a bar
silver that uses 4 raw ores to make a bar
gold that uses 8 raw ores and 2 coal to make a bar

would it just be wise to do it all via php (if do the code for the page would be huge)from all the variables
or do it via php sql using the database to hold the infomation on what is need to make each item.
after that i have confused myself as to how to pull info out as i want it to add up to let me know the total amount of resource needed.

thanks for you help
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Advice on a item calculator

Post by Chris »

Yo, I wipped you up some code, read it, should make some sense.

Code: Select all

Table `items`
+----+--------------+-------+
| id | name         | price |
+----+--------------+-------+
| 1  | Breast plate | 6000  |
| 2  | Gloves       | 9000  |
| 3  | Sword        | 3099  |
| 4  | Bar          | 500   |
| 5  | Handle       | 200   |
+----+--------------+-------+

Table `make_items`
+----+---------+----------+------------+
| id | item_id | requires | of_item_id |
+----+---------+----------+------------+
| 1  | 1       | 6        | 4          |
| 2  | 3       | 5,1      | 4,5        |
| 3  | 2       | 4        | 4          |
+----+---------+----------+------------+
then..

make a few functions.. blah blah..

Code: Select all

function getItemPrice($id)
{
    $query = mysql_query("SELECT `price` FROM `items` WHERE `id` = '$id'");
    $array = mysql_fetch_assoc($query);
    return $array['price'];
}

function costToMake($id)
{
    $query = mysql_query("SELECT * FROM `make_items` WHERE `item_id` = '$id'");
    $array = mysql_fetch_assoc($query);
    
    $itemIds = explode(",", $array['of_item_id']);
    
    $cost = 0;
    
    foreach( explode(",", $array['requires']) as $key => $amount )
    {
        $cost = $cost + ( getItemPrice( $itemIds[$key] ) * $amount );
    }
    
    return $cost;
}

// Cost for Sword, should be 2700
echo costToMake(3);
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Coding”