Jump to content

Can I shorten?


karimali831

Recommended Posts

Hi,

 

When a user POSTS 10 maps (dropdown), I don't want any two or more being the same.

From my knowledge, only way I know of doing this, is this very long messy way:

 

if($_POST['map1']==$_POST['map2'] || $_POST['map1']==$_POST['map3'] || $_POST['map1']==$_POST['map4'] || $_POST['map1']==$_POST['map5']) etc...

 

then I need to type if $_POST['map2']==$_POST['map1'] etc...

 

How can I put this in its simplest form so that there are no POST duplicates?

 

Thanks for any help.

Link to comment
Share on other sites

You can create an array containing all the values of $_POST['mapx'], perform array_unique on the array and compare the length of the original array to the one returned by array_unique. If they are the same length then there were no duplicates, otherwise there were.

 

$arr = array(
    $_POST['map1'],
    $_POST['map2'],
    ...
);

if(sizeof($arr) != sizeof(array_unique($arr))) {
    // There was duplicates
}

Link to comment
Share on other sites

The length's of the individual maps isn't being compared. The length of the array of maps is. For example say your array looks like this:

 

array(1, 2, 3, 4);

 

When you perform array_unique on it the result will still look like this:

 

array(1, 2, 3, 4);

 

Because there are no duplicates. But if we had an array like this:

 

array(1, 1, 2, 3);

 

The result from array_unique would look like this:

 

array(1, 2, 3);

 

The duplicates were removed and we can tell that there was a duplicate because the size of the array that we input is different from the size of the array that was output (meaning duplicates were stripped).

Link to comment
Share on other sites

That depends, (depending on the setting) - user can select from 1 map to 5  or 1 date to 5 (extending to 10 later)

The long bit:

 

Maps:

     if($ds['select_map']==1) {
       if($_POST['map1']==$_POST['map2'] || $_POST['map1']==$_POST['map3'] || $_POST['map1']==$_POST['map4'] || $_POST['map1']==$_POST['map5'])
        die('Map option 1 has been selected multiple times!');
        $select_map = "map1='".$_POST['map1']."',";
     }elseif($ds['select_map']==2) {
       if($_POST['map1']==$_POST['map2'] || $_POST['map1']==$_POST['map3'] || $_POST['map1']==$_POST['map4'] || $_POST['map1']==$_POST['map5'])
        die('Map option 1 has been selected multiple times!');
       if($_POST['map2']==$_POST['map1'] || $_POST['map2']==$_POST['map3'] || $_POST['map2']==$_POST['map4'] || $_POST['map2']==$_POST['map5'])
        die('Map option 2 has been selected multiple times!');
        $select_map = "map1='".$_POST['map1']."', map2='".$_POST['map2']."',";
     }elseif($ds['select_map']==3) {
       if($_POST['map1']==$_POST['map2'] || $_POST['map1']==$_POST['map3'] || $_POST['map1']==$_POST['map4'] || $_POST['map1']==$_POST['map5'])
        die('Map option 1 has been selected multiple times!');
       if($_POST['map2']==$_POST['map1'] || $_POST['map2']==$_POST['map3'] || $_POST['map2']==$_POST['map4'] || $_POST['map2']==$_POST['map5'])
        die('Map option 2 has been selected multiple times!');
       if($_POST['map3']==$_POST['map2'] || $_POST['map3']==$_POST['map1'] || $_POST['map3']==$_POST['map4'] || $_POST['map3']==$_POST['map5'])
        die('Map option 3 has been selected multiple times!');
        $select_map = "map1='".$_POST['map1']."', map2='".$_POST['map2']."', map3='".$_POST['map3']."',";
     }elseif($ds['select_map']==4) {
       if($_POST['map1']==$_POST['map2'] || $_POST['map1']==$_POST['map3'] || $_POST['map1']==$_POST['map4'] || $_POST['map1']==$_POST['map5'])
        die('Map option 1 has been selected multiple times!');
       if($_POST['map2']==$_POST['map1'] || $_POST['map2']==$_POST['map3'] || $_POST['map2']==$_POST['map4'] || $_POST['map2']==$_POST['map5'])
        die('Map option 2 has been selected multiple times!');
       if($_POST['map3']==$_POST['map2'] || $_POST['map3']==$_POST['map1'] || $_POST['map3']==$_POST['map4'] || $_POST['map3']==$_POST['map5'])
        die('Map option 3 has been selected multiple times!');
       if($_POST['map4']==$_POST['map2'] || $_POST['map4']==$_POST['map3'] || $_POST['map4']==$_POST['map1'] || $_POST['map4']==$_POST['map5'])
        die('Map option 4 has been selected multiple times!');
        $select_map = "map1='".$_POST['map1']."', map2='".$_POST['map2']."', map3='".$_POST['map3']."', map4='".$_POST['map4']."',";
     }elseif($ds['select_map']==5) {
       if($_POST['map1']==$_POST['map2'] || $_POST['map1']==$_POST['map3'] || $_POST['map1']==$_POST['map4'] || $_POST['map1']==$_POST['map5'])
        die('Map option 1 has been selected multiple times!');
       if($_POST['map2']==$_POST['map1'] || $_POST['map2']==$_POST['map3'] || $_POST['map2']==$_POST['map4'] || $_POST['map2']==$_POST['map5'])
        die('Map option 2 has been selected multiple times!');
       if($_POST['map3']==$_POST['map2'] || $_POST['map3']==$_POST['map1'] || $_POST['map3']==$_POST['map4'] || $_POST['map3']==$_POST['map5'])
        die('Map option 3 has been selected multiple times!');
       if($_POST['map4']==$_POST['map2'] || $_POST['map4']==$_POST['map3'] || $_POST['map4']==$_POST['map1'] || $_POST['map4']==$_POST['map5'])
        die('Map option 4 has been selected multiple times!');
       if($_POST['map5']==$_POST['map2'] || $_POST['map5']==$_POST['map3'] || $_POST['map5']==$_POST['map4'] || $_POST['map5']==$_POST['map1'])
        die('Map option 5 has been selected multiple times!');
        $select_map = "map1='".$_POST['map1']."', map2='".$_POST['map2']."', map3='".$_POST['map3']."', map4='".$_POST['map4']."', map5='".$_POST['map5']."',";
     }else
        $select_map = '';
        

 

Dates:

    if($ds['select_date']==1) {
       if($_POST['date1']==$_POST['date2'] || $_POST['date1']==$_POST['date3'] || $_POST['date1']==$_POST['date4'] || $_POST['date1']==$_POST['date5'])
        die('Date option 1 has been selected multiple times!');
        $select_date = "date1='".$_POST['date1']."',";
     }elseif($ds['select_date']==2) {
       if($_POST['date1']==$_POST['date2'] || $_POST['date1']==$_POST['date3'] || $_POST['date1']==$_POST['date4'] || $_POST['date1']==$_POST['date5'])
        die('Date option 1 has been selected multiple times!');
       if($_POST['date2']==$_POST['date1'] || $_POST['date2']==$_POST['date3'] || $_POST['date2']==$_POST['date4'] || $_POST['date2']==$_POST['date5'])
        die('Date option 2 has been selected multiple times!');
        $select_date = "date1='".$_POST['date1']."', date2='".$_POST['date2']."',";
     }elseif($ds['select_date']==3) {
       if($_POST['date1']==$_POST['date2'] || $_POST['date1']==$_POST['date3'] || $_POST['date1']==$_POST['date4'] || $_POST['date1']==$_POST['date5'])
        die('Date option 1 has been selected multiple times!');
       if($_POST['date2']==$_POST['date1'] || $_POST['date2']==$_POST['date3'] || $_POST['date2']==$_POST['date4'] || $_POST['date2']==$_POST['date5'])
        die('Date option 2 has been selected multiple times!');
       if($_POST['date3']==$_POST['date2'] || $_POST['date3']==$_POST['date1'] || $_POST['date3']==$_POST['date4'] || $_POST['date3']==$_POST['date5'])
        die('Date option 3 has been selected multiple times!');
        $select_date = "date1='".$_POST['date1']."', date2='".$_POST['date2']."', date3='".$_POST['date3']."',";
     }elseif($ds['select_date']==4) {
       if($_POST['date1']==$_POST['date2'] || $_POST['date1']==$_POST['date3'] || $_POST['date1']==$_POST['date4'] || $_POST['date1']==$_POST['date5'])
        die('Date option 1 has been selected multiple times!');
       if($_POST['date2']==$_POST['date1'] || $_POST['date2']==$_POST['date3'] || $_POST['date2']==$_POST['date4'] || $_POST['date2']==$_POST['date5'])
        die('Date option 2 has been selected multiple times!');
       if($_POST['date3']==$_POST['date2'] || $_POST['date3']==$_POST['date1'] || $_POST['date3']==$_POST['date4'] || $_POST['date3']==$_POST['date5'])
        die('Date option 3 has been selected multiple times!');
       if($_POST['date4']==$_POST['date2'] || $_POST['date4']==$_POST['date3'] || $_POST['date4']==$_POST['date1'] || $_POST['date4']==$_POST['date5'])
        die('Date option 4 has been selected multiple times!');
        $select_date = "date1='".$_POST['date1']."', date2='".$_POST['date2']."', date3='".$_POST['date3']."', date4='".$_POST['date4']."',";
     }elseif($ds['select_date']==5) {
       if($_POST['date1']==$_POST['date2'] || $_POST['date1']==$_POST['date3'] || $_POST['date1']==$_POST['date4'] || $_POST['date1']==$_POST['date5'])
        die('Date option 1 has been selected multiple times!');
       if($_POST['date2']==$_POST['date1'] || $_POST['date2']==$_POST['date3'] || $_POST['date2']==$_POST['date4'] || $_POST['date2']==$_POST['date5'])
        die('Date option 2 has been selected multiple times!');
       if($_POST['date3']==$_POST['date2'] || $_POST['date3']==$_POST['date1'] || $_POST['date3']==$_POST['date4'] || $_POST['date3']==$_POST['date5'])
        die('Date option 3 has been selected multiple times!');
       if($_POST['date4']==$_POST['date2'] || $_POST['date4']==$_POST['date3'] || $_POST['date4']==$_POST['date1'] || $_POST['date4']==$_POST['date5'])
        die('Date option 4 has been selected multiple times!');
       if($_POST['date5']==$_POST['date2'] || $_POST['date5']==$_POST['date3'] || $_POST['date5']==$_POST['date4'] || $_POST['date5']==$_POST['date1'])
        die('Date option 5 has been selected multiple times!');
        $select_date = "date1='".$_POST['date1']."', date2='".$_POST['date2']."', date3='".$_POST['date3']."', date4='".$_POST['date4']."', date5='".$_POST['date5']."',";
     }else
        $select_date = ',';

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.