Jump to content

compare comma seperated values to array index


Omzy

Recommended Posts

Basically I'm reading data from a MySQL database, and I want to compare this data to an array.

 

The array is called $subcats and has the following sample data:

 

$subcats=array(
'flowers'=>array('decorations', 'Flowers'),
'balloons'=>array('decorations', 'Balloons'),
'banners'=>array('decorations', 'Banners'),
'fruit-displays'=>array('decorations', 'Fruit Displays'),
'ice-sculptures'=>array('decorations', 'Ice Sculptures'),
);

 

The data I wish to compare against is in a field called 'tags', which contains a string of values separated by a comma, for example: flowers, balloons, banners

 

Now what I want to do is print out a SELECT MULTIPLE control which has the relevant options selected if they exist in that field.

 

Here is the code I have so far (simplified):

 

foreach($row as $key => $value)
{
  echo '<select size="5" name="'.$key.'">';
   foreach($subcats as $index1 => $value1)
   {
    echo '<option value="'.$index1.'"', $index1==$value ? ' selected="selected"' : null ,'>'.$value1[1].'</option>';
   }
   echo '</select>';
}

 

Now obviously the $index1==$value part is incorrect, and I'm a bit unsure what should be used to do the comparison. Can anyone help?

I thought you wanted to compare the $csv string to the indexes of the array. Try something like this:

 

$arr = explode(', ', $csv);
foreach($row as $key => $value)
{
  echo '<select size="5" name="'.$key.'">';
   $c = 0;
   foreach($subcats as $index1 => $value1)
   {
    echo '<option value="'.$index1.'"', $index1==$arr[$c] ? ' selected="selected"' : null ,'>'.$value1[1].'</option>';
    $c++;
   }
   $c = 0;
   echo '</select>';
}

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.