2017-05-09 7 views
0

私のURLに4つのパラメータがあります。私は与えられたURLからurlパラメータを取得します。すべてのパラメータで、私はディレクトリへのパスを変更し、別の画像を取得しています。このような複数のURLパラメータをPHPで取得する方法

私のサンプルURLの外観:

www.sample.com?cat=section1&language=de&prices=pl 

コードが機能していますが、それはspaghetiコードです。

DRYが少ないという解決策はありますか?複数のURLパラメータを取得するにはどうすればよいですか?

if(isset($_GET["cat"])) { 
switch ($cat) { 
case 'section1': 
if(isset($_GET["language"])) { 
     $language = htmlspecialchars($_GET["language"]); 
     if($language == "de") { 
      if(isset($_GET["prices"])) { 
       $prices = htmlspecialchars($_GET["prices"]); 
       if($prices == "pl"){ 
        $files=glob('pages/section1/dp/low/*.jpg'); 
       } 
       else { 
        $files=glob('pages/section1/dn/low/*.jpg'); 
       } 
      } 
      else { 

        $files=glob('pages/section1/dn/low/*.jpg'); 
      } 
     } 
     elseif ($language == "en") { 
      if(isset($_GET["prices"])) { 
       $prices = htmlspecialchars($_GET["prices"]); 
       if($prices == "pl"){ 
         $files=glob('pages/section1/ep/low/*.jpg'); 
       } 
       else { 
         $files=glob('pages/section1/en/low/*.jpg'); 
       } 
      } 
      else { 
        $files=glob('pages/section1/en/low/*.jpg'); 
      } 
     } 
     elseif ($language == "cz") { 
      if(isset($_GET["prices"])) { 
       $prices = htmlspecialchars($_GET["prices"]); 
       if($prices == "pl"){ 
         $files=glob('pages/section1/cp/low/*.jpg'); 
       } 
       else { 
         $files=glob('pages/section1/cn/low/*.jpg'); 
       } 
      } 
      else { 
        $files=glob('pages/section1/cn/low/*.jpg'); 
      } 
     } 
     else { 
       $files=glob('pages/section1/cn/low/*.jpg'); 
     } 
    } 
    else { 
      $files=glob('pages/section1/dn/low/*.jpg'); 
    } 
    break; 
case 'section2': 
    //the same like in section 1, path is .../section2/... 
    break; 
case section3: 
    //the same like in section 1, path is .../section3/... 
    break; 
default: 
    //the same like in section 1 
    break; 
} 
else { 
    //the same like in section 1 
} 

あなただけのチェックを行っているとelse文場合、多くを削除/短縮できたパスd =ドイツ語、E =英語、C =チェコ、P =価格、N =

+3

__All__このコードは、 '$ files = glob( 'pathto ......');'と書き直すことができます。 'pathto'がURLパラメータにどのように依存するのかを記述する必要があります。 –

+0

が自分のコードへのパスを追加しました。 –

答えて

0

をnoprices:

$lang_code = $language[0]; 

最初の手紙がありますので、すべてのGETパラメータで同じことができます。 だから、のようなことに使用することができます。

$files=glob('pages/section1/'.$lang_code.'p/low/*.jpg'); 

あなたが他のすべてのために同じことを行うことができます。

P.s:私はおそらくこのような何かをしたい任意のユーザ入力すなわち:

$language=mysqli_real_escape_string($conn, $_GET['language']); 
0

をsanitzeすることを忘れないでください:

<?php 
    $allowedCat = ['section1', 'section2']; 
    $allowedLanguage = ['pl', 'en', 'cz']; 
    $allowedPrice = ['pl', '??']; 

    $cat = (isset($_GET["cat"])) ? $_GET["cat"] : null; 
    $language = (isset($_GET["language"])) ? $_GET["language"] : null; 
    $prices = (isset($_GET["prices"])) ? $_GET["prices"] : null; 

    if (!in_array($cat, $allowedCat)) throw new \Exception('Invalid `cat`'); 
    if (!in_array($language, $allowedLanguage)) throw new \Exception('Invalid `language` option.'); 
    if (!in_array($prices, $allowedPrice)) throw new \Exception('Invalid `price` option.'); 

    $someVar1 = ($prices === 'pl') ? 'p' : 'n'; 
    $someVar2 = $language[0]; 

    $files = glob("pages/{$cat}/{$someVar1}{$someVar2}/low/*.jpg"); 

は、それは自己説明的であるべきだと思います。本当に1つを翻訳します。 priceオプションがどのように指定されたかについてはっきりしていません...

関連する問題