karimali831 Posted August 27, 2010 Share Posted August 27, 2010 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 https://forums.phpfreaks.com/topic/211852-can-i-shorten/ Share on other sites More sharing options...
Alex Posted August 27, 2010 Share Posted August 27, 2010 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 https://forums.phpfreaks.com/topic/211852-can-i-shorten/#findComment-1104254 Share on other sites More sharing options...
karimali831 Posted August 27, 2010 Author Share Posted August 27, 2010 Hmm.. they are duplicates if they are not the same length or are the same? Some maps however are the same length and some not: bloodgulch sidewinder thanks again. Link to comment https://forums.phpfreaks.com/topic/211852-can-i-shorten/#findComment-1104256 Share on other sites More sharing options...
Alex Posted August 27, 2010 Share Posted August 27, 2010 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 https://forums.phpfreaks.com/topic/211852-can-i-shorten/#findComment-1104259 Share on other sites More sharing options...
karimali831 Posted August 27, 2010 Author Share Posted August 27, 2010 thanks will give it ago. Link to comment https://forums.phpfreaks.com/topic/211852-can-i-shorten/#findComment-1104261 Share on other sites More sharing options...
litebearer Posted August 27, 2010 Share Posted August 27, 2010 Couldn't he save a step (or more) by simply using the result of array_unique()? Link to comment https://forums.phpfreaks.com/topic/211852-can-i-shorten/#findComment-1104354 Share on other sites More sharing options...
Alex Posted August 27, 2010 Share Posted August 27, 2010 Couldn't he save a step (or more) by simply using the result of array_unique()? Not according to what he wants, in the first post he said he wants all 10 maps to be unique. Link to comment https://forums.phpfreaks.com/topic/211852-can-i-shorten/#findComment-1104397 Share on other sites More sharing options...
karimali831 Posted August 27, 2010 Author Share Posted August 27, 2010 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 https://forums.phpfreaks.com/topic/211852-can-i-shorten/#findComment-1104482 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.