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?

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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