Jump to content

multiple variables in one mySQL field


ldsmike88

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/10287-multiple-variables-in-one-mysql-field/
Share on other sites

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
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...

[code]
<?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);
?>            
[/code]
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.