1

これをホスティングにとってより簡単/クリーン/より良いものにする方法を考えました(確かではありませんが、私の方法はサーバーフレンドリーではないと思います)。

私の目的:(可能であれば)「if」部分を減らします。その部分は、どのドロップダウン ボックスが「空」から変更されたかをチェックし (実際には「すべて」ですが、何か良い場合は空に変更できます)、それらの条件 (ホーム大学、ホスト) を満たすユーザーのみを表示するように適用します。大学、場所、および/または国籍. これは、私が作成できた唯一の簡単で基本的な方法です.

私はこの機能を持っています:

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を実行するより良い方法があるかどうか知りたいです。きっとあるよ。ありがとうございました :)

4

1 に答える 1

2

あなたはただあなたが何を意味するかを言うことができます:

if(  ($selectedhomeuni == "all" || $selectedhomeuni == $homeuni)
  && ($selectedhostuni == "all" || $selectedhostuni == $hostuni)
  && ($selectedlocation == "all" || $selectedlocation == $location)
  && ($selectednationality == "all" || $selectednationality == $nationality)
  )
{
  return $html;
}
于 2013-08-08T14:10:14.880 に答える