缩小一堆“;如果“";elseif”;.(16种可能性)


Downsize bunch of "if", "elseif". (16 possibilities)

我已经考虑过如何让托管更容易/更干净/更好(我想我的方法对服务器不友好,尽管我不确定)。

我的目的是:减少(如果可能的话)"如果"部分。该部分检查哪些下拉框已从"空"更改(实际上是"全部",但如果对某些事情更好的话,可以将其更改为空),并将其应用于仅显示符合这些条件的用户(所在大学、主办大学、所在地和/或国籍)。这是我能够创建的唯一简单/基本的方法。

我有这个功能:

function get_user_listing($curauth) {
  global $post;
  $concat = wpu_concat_single();
  // These get the values from the plugin Cimy User Extra Fields:
  $homeuni=get_cimyFieldValue($curauth->ID,'homeuni');
  $hostuni=get_cimyFieldValue($curauth->ID,'hostuni');
  $location=get_cimyFieldValue($curauth->ID,'location');
  $nationality=get_cimyFieldValue($curauth->ID,'nationality');
  // These get the values from a dropdown form in the page:
  $selectedhomeuni = $_POST['homeuni'];
  $selectedhostuni = $_POST['hostuni'];  
  $selectedlocation = $_POST['location'];
  $selectednationality = $_POST['nationality'];
//This is the code that has to be run every time to display every user info:
include '/home/u548205287/public_html/wp-content/themes/Trim/profilescode.php';
// I set an initial page that runs the code with no conditions because with 
the form, the page would look empty until the form is submitted once:
if(is_page(806)) {return $html;} 
else{
if($selectedhomeuni == "all" && $selectedhostuni == "all" && $selectedlocation == "all" && $selectednationality == "all") {return $html;} // The possibilities with each dropdown start here. If "all" (the "empty" one) is selected, nothing changes and all are displayed.
elseif($selectedhomeuni != "all" && $selectedhostuni == "all" && $selectedlocation == "all" && $selectednationality == "all") {if($homeuni==$selectedhomeuni) {return $html;}}  // If any dropdown is selected, its value acts as a filter and only the users with that info are shown.
elseif($selectedhomeuni == "all" && $selectedhostuni != "all" && $selectedlocation == "all" && $selectednationality == "all") {if($hostuni==$selectedhostuni) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni == "all" && $selectedlocation != "all" && $selectednationality == "all") {if($location==$selectedlocation) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni == "all" && $selectedlocation == "all" && $selectednationality != "all") {if($nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni != "all" && $selectedlocation == "all" && $selectednationality == "all") {if($homeuni==$selectedhomeuni && $hostuni==$selectedhostuni) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni == "all" && $selectedlocation != "all" && $selectednationality == "all") {if($homeuni==$selectedhomeuni && $location==$selectedlocation) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni == "all" && $selectedlocation == "all" && $selectednationality != "all") {if($homeuni==$selectedhomeuni && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni != "all" && $selectedlocation != "all" && $selectednationality == "all") {if($homeuni==$selectedhomeuni && $hostuni==$selectedhostuni && $location==$selectedlocation) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni != "all" && $selectedlocation == "all" && $selectednationality != "all") {if($homeuni==$selectedhomeuni && $hostuni==$selectedhostuni && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni == "all" && $selectedlocation != "all" && $selectednationality != "all") {if($homeuni==$selectedhomeuni && $location==$selectedlocation && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni != "all" && $selectedhostuni != "all" && $selectedlocation != "all" && $selectednationality != "all") {if($homeuni==$selectedhomeuni && $hostuni==$selectedhostuni && $location==$selectedlocation && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni != "all" && $selectedlocation != "all" && $selectednationality == "all") {if($hostuni==$selectedhostuni && $location==$selectedlocation) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni != "all" && $selectedlocation == "all" && $selectednationality != "all") {if($hostuni==$selectedhostuni && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni != "all" && $selectedlocation != "all" && $selectednationality != "all") {if($hostuni==$selectedhostuni && $location==$selectedlocation && $nationality==$selectednationality) {return $html;}}
elseif($selectedhomeuni == "all" && $selectedhostuni == "all" && $selectedlocation != "all" && $selectednationality != "all") {if($location==$selectedlocation && $nationality==$selectednationality) {return $html;}}
}
} 

我想知道是否有更好的方法来做所有的if。我肯定有。谢谢:)

你可以说出你的意思:

if(  ($selectedhomeuni == "all" || $selectedhomeuni == $homeuni)
  && ($selectedhostuni == "all" || $selectedhostuni == $hostuni)
  && ($selectedlocation == "all" || $selectedlocation == $location)
  && ($selectednationality == "all" || $selectednationality == $nationality)
  )
{
  return $html;
}