Question

I've thought of how to make this easier/cleaner/better for the hosting (I guess my method not server-friendly, although I'm not sure).

My purpose: reduce (if possible) the "if" part. That part checks which dropbdown boxes have been changed from "empty" (it's actually "all" but it could be changed to empty if it's better for something), and applies it to show only the users that meet those conditions (home university, host university, location and/or nationality. This is the only easy/basic way I've been able to create.

I have this function:

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

I'd like to know if there is a better way of doing all the if's. I'm sure there is. Thank you :)

Was it helpful?

Solution

You could just say what you mean:

if(  ($selectedhomeuni == "all" || $selectedhomeuni == $homeuni)
  && ($selectedhostuni == "all" || $selectedhostuni == $hostuni)
  && ($selectedlocation == "all" || $selectedlocation == $location)
  && ($selectednationality == "all" || $selectednationality == $nationality)
  )
{
  return $html;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top