ldsmike88 Posted May 23, 2006 Share Posted May 23, 2006 I have a table in a mySQL database for the items I sell on my site. I want to make a list of options, or add ons for each item. I figured the best way to do this would be to include the add on's item number in a field in the item-being-added-to's row. But what if I want 3 or 5 add on's, I don't want to have that many new fields in my table. Is there a way to put the item numbers in one field and separate them with a comma or a colon and have php break them apart into each individual item number? Thanks in advance!Mike Quote Link to comment https://forums.phpfreaks.com/topic/10287-multiple-variables-in-one-mysql-field/ Share on other sites More sharing options...
appeland Posted May 23, 2006 Share Posted May 23, 2006 Hi Mike,you can use explode() for that -- no reason to explain it in detail here because the lads at php.net have done so quite nicely I think:[a href=\"http://www.php.net/manual/en/function.explode.php\" target=\"_blank\"]http://www.php.net/manual/en/function.explode.php[/a]Regards,Andi Quote Link to comment https://forums.phpfreaks.com/topic/10287-multiple-variables-in-one-mysql-field/#findComment-38345 Share on other sites More sharing options...
ldsmike88 Posted May 23, 2006 Author Share Posted May 23, 2006 Now what if I want to sort them alphabetically and get rid of duplicates? Quote Link to comment https://forums.phpfreaks.com/topic/10287-multiple-variables-in-one-mysql-field/#findComment-38399 Share on other sites More sharing options...
.josh Posted May 23, 2006 Share Posted May 23, 2006 you would use the sort function to sort sort ($array);[a href=\"http://www.php.net/sort\" target=\"_blank\"]http://www.php.net/sort[/a]as far as getting rid of duplicates.... well i'm not sure if php has abuilt in function for doing that or not, but this will work...[code]<?phpfunction remove_dupes($list) { $x = 0; $count = count($list); while ($list[$x]) { $temp = $list[$x]; for ($y=($x+1);$y<=$count;$y++) { if ($temp == $list[$y]) { unset($list[$y]); } //end if } //end for $list = array_values($list); $count = count($list); $x++; } //end while return $list;} //end remove_dupes//example array$fruit = array ('apple','banana','orange','grape','kiwi','apple','orange');$fruit = remove_dupes($fruit);?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/10287-multiple-variables-in-one-mysql-field/#findComment-38401 Share on other sites More sharing options...
mikejarrett Posted May 23, 2006 Share Posted May 23, 2006 if $exparray is the array that you've exploded into, you could:foreach ($exparray as $key=>$value){ if (empty($svar[$value])) $svar[$value]=1; else $svar[$value]++;}ksort($svar);At that point, the $svar would be an array with the keys that are the sorted, non-duped values, a the values would have the number of occurances. Quote Link to comment https://forums.phpfreaks.com/topic/10287-multiple-variables-in-one-mysql-field/#findComment-38403 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.