Monkuar Posted September 6, 2013 Share Posted September 6, 2013 (edited) Okay, so my monsters table has a column called "droparray" for example: 7,2,5 7,2,5 are the id's of the items they could POSSIBLE get when they kill the monster [intuitive eh?] I need a way to just select a random number from those 3 numbers (or any amount) AND a way to let's say if 7 was a unique item, have it a less % chance. Any ideas? Edited September 6, 2013 by Monkuar Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted September 6, 2013 Share Posted September 6, 2013 Sounds like you need to normalize your data to me. Quote Link to comment Share on other sites More sharing options...
Monkuar Posted September 6, 2013 Author Share Posted September 6, 2013 (edited) Sounds like you need to normalize your data to me. haha nice pun But seriously, my items and stuff are already in a row.. I'm just going to use the WHERE IN clause in my query, so I'm just generating these random id's to select what items they will get after the monster has died. These id's will be used in a where in CLAUSE (in mysql) to grab the item info/etc and insert their "NORMALIZED" data to their inventory/etc I'm going to have each mob have different loot, let's say Mob 2 will have a chance to get the items with the id of 2,3,5 so I just need to select a random of these to use in the where IN clause to grab all the data and insert it. (makes it very good for RNG based) Edited September 6, 2013 by Monkuar Quote Link to comment Share on other sites More sharing options...
Monkuar Posted September 6, 2013 Author Share Posted September 6, 2013 (edited) $monsterdata['droparray'] = "5,2,1"; $item_id_loot = array($monsterdata['droparray']); echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].' This DOESN't work.... but this does: $item_id_loot = array(5,2,1); echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].' Why? Wat the.. Is it because it's set to Varchar in column? (mysql) Lawl, another php bug zz will report Edited September 6, 2013 by Monkuar Quote Link to comment Share on other sites More sharing options...
akphidelt2007 Posted September 6, 2013 Share Posted September 6, 2013 $monsterdata['droparray'] = "5,2,1"; $item_id_loot = array($monsterdata['droparray']); echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].' This DOESN't work.... but this does: $item_id_loot = array(5,2,1); echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].' Why? Wat the.. Is it because it's set to Varchar in column? (mysql) Lawl, another php bug zz will report It's not a bug... $monsterdata['droparray'] = "5,2,1"; $item_id_loot = array($monsterdata['droparray']); //this would result in Array([0]=>"5,2,1") Quote Link to comment Share on other sites More sharing options...
Monkuar Posted September 6, 2013 Author Share Posted September 6, 2013 No it doesn't, because it just reads it as text. It spits out "5,2,1" instead of 1 rand of each. "5,2,1" is the same as array(5,2,1) but php thinks if you put in a variable array($temparray), $temparray = "5,2,1" it think's it's 1 word, it's not. Quote Link to comment Share on other sites More sharing options...
Philip Posted September 6, 2013 Share Posted September 6, 2013 haha nice pun He means database normalization. You shouldn't be storing comma separated values for things you want to query against. Quote Link to comment Share on other sites More sharing options...
akphidelt2007 Posted September 6, 2013 Share Posted September 6, 2013 No it doesn't, because it just reads it as text. It spits out "5,2,1" instead of 1 rand of each. "5,2,1" is the same as array(5,2,1) but php thinks if you put in a variable array($temparray), $temparray = "5,2,1" it think's it's 1 word, it's not. "5,2,1" is one string. And PHP reads it correctly. You are acting as if commas are some deliminator when you put a string in an array. Like I said the array will be Array([0]=>"5,2,1"). Which is correct and not a bug. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted September 6, 2013 Share Posted September 6, 2013 He means database normalization. You shouldn't be storing comma separated values for things you want to query against. Yup. A column of CSVs in a row is probably the #1 sign of doing it wrong. Here's a link to a good introduction to normalization: http://mikehillyer.com/articles/an-introduction-to-database-normalization/ Quote Link to comment Share on other sites More sharing options...
Irate Posted September 7, 2013 Share Posted September 7, 2013 $monsterdata['droparray'] = "5,2,1"; $item_id_loot = array($monsterdata['droparray']); echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].'This DOESN't work.... but this does: $item_id_loot = array(5,2,1); echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].' Why? Wat the.. Is it because it's set to Varchar in column? (mysql) Lawl, another php bug zz will report Revise the first code. String literals are never parsed as arrays. Use explode() to get an array out of a CSV-list. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.