Web Hosting Talk







View Full Version : Darn I am confused, very simple php coding please


lexington
08-14-2007, 08:42 PM
Hello, this should be very easy to do so if you could please help that would be great. I created a script but one part has me kinda confused. Instead of having to repeat a switch code over and over again, I rather use a variable for the switch name so that it could detect the mons_name from the DB and make a selection. I guess I would use an array for this, but I am not sure if that is the correct way of doing it. Here is what I have coded so far (I replaced the item name with (NORMAL ITEM *, and boss name with BOSS NAME HERE * in this post):

// normal item drops
if ($battle['mons_type'] == 1)
{
$newrand = rand(1,2);
switch($newrand)
{
case 1:
$stealitem = 'NORMAL ITEM 1';
break;
case 2:
$stealitem = 'NORMAL ITEM 2';
break;
}
}
// special boss item drops
if ($battle['mons_type'] == 2)
{
$boss_item = array(
'BOSS NAME HERE 1' => 'SPECIAL ITEM HERE 1',
'BOSS NAME HERE 2' => 'SPECIAL ITEM HERE 2',
'BOSS NAME HERE 3' => 'SPECIAL ITEM HERE 3'
);

// new boss entry
if ($battle['mons_name'] = 'BOSS NAME HERE 2')
{
$bossrand = rand(1,2);
switch($bossrand)
{
case 1:
$stealitem = $boss_item;
break;
case 2:
$stealitem = 'NORMAL ITEM HERE 1';
break;
}


}

I am not sure if I should use an array in this case or not. Basically I want the script to know if the mons name is the name of the mons, it will drop a specific item from my array. The script can already detect the mons_name, so how should I write this so that when it detects the mons name it can select the special item I have assigned? I hope this makes sense.

sasha
08-15-2007, 09:51 AM
You could try this

// new boss entry
if (rand(1,2) == 2 ) {
$stealitem = $boss_item[$battle['mons_name']];
}else{
$stealitem = 'NORMAL ITEM HERE 1';
}


... or ...

you could have that stuff in come kind of database rather then hard-coding items and monsters in the code.

table: item_drops
id
item (index, reference to items.id)
mons (index, reference to mons.id)

and the data would be something like
1 | 4 | 1 (which means: mons 1 can drop item 4)
2 | 5 | 1 (which means: mons 1 can drop item 5)
3 | 6 | 1 (which means: mons 1 can drop item 6)
4 | 9 | 2 (which means: mons 2 can drop item 9)
5 | 9 | 3 (which means: mons 3 can drop item 9)

table items
id
name
rarity (INT 1 , 2 ,3 ... the higher it is there is less chance of it being dropped)

table mons
id
name

Then you could do your something like this

select items.id , items.name
FROM item_drops
LEFT JOIN items ON item_drops.item = items.id
WHERE item_drops.mons = '{$battle['mons_id']}'
ORDER BY RAND() * items.rarity
LIMIT 1

"* items.rarity" part is added to allow even less chance for rare items to drop, but you might now want that.

lexington
08-15-2007, 10:00 AM
Hi Sasha how are you :) Thanks for the post. I meant to update this topic to say that I have decided to code it differently so everything is fine now. I lagged when trying to visit this post and forgot about it. Thanks for your time though :)