Jump to content


Photo

multiple variables in one mySQL field


  • Please log in to reply
4 replies to this topic

#1 ldsmike88

ldsmike88
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts
  • LocationFlorida

Posted 23 May 2006 - 06:24 PM

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

#2 appeland

appeland
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 23 May 2006 - 07:10 PM

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


#3 ldsmike88

ldsmike88
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts
  • LocationFlorida

Posted 23 May 2006 - 09:58 PM

Now what if I want to sort them alphabetically and get rid of duplicates?

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 May 2006 - 10:35 PM

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 a
built in function for doing that or not, but this will work...

<?php
function 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);
?>            

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 mikejarrett

mikejarrett
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 May 2006 - 10:44 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users