spiderdog Posted June 13, 2007 Share Posted June 13, 2007 Is it possible to do this first store different things into an array thing1 thing2 thing3 thing4 second store numbers into a mysql row to record how much of each thing 0,1,1,5,2,8,3,10(data to store into th row) so that would mean 1 of thing1 5 of thing2 8 of thing3 and 10 of thing4 would it be possible for PHP to parse all of that and output how much of each? Quote Link to comment https://forums.phpfreaks.com/topic/55496-is-it-possible-to-do-this/ Share on other sites More sharing options...
trq Posted June 13, 2007 Share Posted June 13, 2007 would it be possible for PHP to parse all of that and output how much of each? Yes. Where exactly are you stuck? Quote Link to comment https://forums.phpfreaks.com/topic/55496-is-it-possible-to-do-this/#findComment-274235 Share on other sites More sharing options...
spiderdog Posted June 13, 2007 Author Share Posted June 13, 2007 I actually have not started on it but I wanted the 0,1,1,5,2,8,3,10 part to all be stored into one column so what php could parse it? could I just put all of the amount numbers into one array somehow? Quote Link to comment https://forums.phpfreaks.com/topic/55496-is-it-possible-to-do-this/#findComment-274238 Share on other sites More sharing options...
trq Posted June 13, 2007 Share Posted June 13, 2007 Your question is pretty vague. Why would you want to store things in an array and there related amounts in a database. How are you going to relate the two separate sources of data? Without really understanding what it is exactly your trying to do, I'm going to try an example. PHP has associative arrays. These are great for grouping keys / values of related data. eg; <?php $things = array('thing1' => 0,'thing2' => 1,'thing3' => 1,'thing4' => 5); foreach ($things as $k => $v) { echo "There are $v of $k<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/55496-is-it-possible-to-do-this/#findComment-274244 Share on other sites More sharing options...
Psycho Posted June 13, 2007 Share Posted June 13, 2007 Hmm.... Something like this: <?php $arr = (0,1,2,2,1,3,5,2,8,1,3,10,2); $countArr = array_count_values ($arr); print_r($countArr); // Output: // Array // ( // [0] => 1 // [1] => 3 // [5] => 1 // [2] => 4 // [8] => 1 // [3] => 2 // [10] => 1 // ) // To put in database foreach ($countArr as $value => $count) { $values[] = "('$value','$count')"; } $valueStr = "(" . implode(',', $values) . ")"; $query = "INSERT INTO table (value, count) VALUES " . $valueStr; $result - mysql_query($query); ?> How you would insert that into a database woud depend on the structure of your table. Quote Link to comment https://forums.phpfreaks.com/topic/55496-is-it-possible-to-do-this/#findComment-274247 Share on other sites More sharing options...
Caesar Posted June 13, 2007 Share Posted June 13, 2007 Yeah, I guess I'm wondering what he means... Array ( [item_1] => Array ( [0] => shoes [1] => 2 [2] => 12.99 ) [item_2] => Array ( [0] => pants [1] => 1 [2] => 42.99 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/55496-is-it-possible-to-do-this/#findComment-274255 Share on other sites More sharing options...
spiderdog Posted June 13, 2007 Author Share Posted June 13, 2007 It was supposed to be for a shop/inventory thing on a game I was working on maybe it would be better l to create a row if the item row doesn't exist for the person and update it if it does exist. my table layout for my inventory table is id(id for the row goes here) uid(member id goes here) itemname(the items name goes here) amount(the items amount goes here) Quote Link to comment https://forums.phpfreaks.com/topic/55496-is-it-possible-to-do-this/#findComment-274258 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.