Array duplicate values!

Hi guys,

I have this select box that’s echoing out selected values. But the old values still remain, so annoying users can choose the same value twice if they want to (and make me go crazy).
I’m trying to avoid value duplication by incorporating array_unique. Now I learned how to use this function on it’s on but incorporating it into my echoing select box is something I’m having trouble with.

Can someone help me incorporate that please?


<?php
$offer = $_POST['inputoffer'];
?>

            <select name="inputoffer[]" size=3 multiple>
<?php

                      
foreach( $offer as $c ) {
            

            $selected = in_array($c, $_POST['inputoffer'])

                ? ' selected="selected"'

                : '';

            echo '<option value="'.$a.'" '.$selected.'>'.$a.'</option>';

        }

?>
            <option value="hot chocolate">hot chocolate</option>
            <option value="food">food</option>
            <option value="cooked meals">cooked meals</option>
            <option value="couch">couch</option>
            <option value="bed">bed</option>
            <option value="room">room</option>
            <option value="apartment">apartment</option>
            </select>

This:

$selected = in_array($c, $_POST['inputoffer'])

makes no sense right now, because you get all your $c’s from $_POST[‘inputoffer’] so they will always exist in that array.

Instead of hard coding the options in your select box, put them in another array, loop through that array (instead of looping through $_POST[‘inputoffer’]), and use the in_array function to see if the option is present in the $_POST[‘inputoffer’] array.

I’m sorry Guido but I tried doing what you said and didn’t manage. I’m sure your way is correct but I lack the knowledge needed to decipher your advice (for example, I don’t know how to create an array of my options without using $_POST[‘inputoffer’]). So I just let them echo out what they want but prohibit the introduction of double values into database like this.

Thanks anyway for trying to help me.


if( isset($_POST['inputoffer']) ) {

    $result = array_unique($offer);
    $str = implode(' + ', $result); //// I want it as a string in database on purpose
}
mysql_query ("INSERT INTO database(`offer`) VALUES('".$str."')")


<?php
  // put all your options in an array
  $options = 
    array("hot chocolate",  
          "food",
          "cooked meals",
          "couch",
          "bed",
          "room",
          "appartment"
    );
?>            
<select name="inputoffer[]" size=3 multiple>
<?php
  // loop through the options array                      
  foreach ($options as $option ) {
     // if the option exists in $_POST['inputoffer'] it must be selected
     $selected = in_array($option, $_POST['inputoffer']) ? 'selected="selected"' : '';
     echo '<option value="'.$a.'" '.$selected.'>'.$a.'</option>';
  }
?>

You don’t have to advertise my incapability to code php on your postings if you don’t want to Guido :slight_smile: just kidding

Thanks a lot for your help. It works great and now I don’t have to use array_unique that took me an hour to learn.
So to see if I understand this correctly. The code is selecting only those options that exist within the $_POST[‘inputoffer’], but how does keep on echoing out the values not selected.
Is it because the boolean translated as such:

If option is not within the posted inputoffer than $selected = ’ ', which actually means what… an empty value?

It echoes out all the options defined in the $options array.
And only those options that are also present in the $_POST[‘inputoffer’] array will have ‘selected = “selected”’, because like you say,

If option is not within the posted inputoffer than $selected = ’ ', which actually means what… an empty value?

which means indead: empty.