2017-05-09 2 views
0

내 URL에 4 개의 매개 변수가 있습니다. 주어진 url에서 url 매개 변수를 검색합니다. 모든 매개 변수를 사용하여 디렉토리 경로를 변경하고 다른 이미지를 얻습니다. 이 같은PHP에서 복수 URL 매개 변수를 검색하는 방법

내 샘플 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 
} 

경로 D = 독일어, E = 영어, C = 체코 어, P = 가격, N = noprices

+3

__All__이 코드는'$ files = glob ('pathto ......');로 재 작성 될 수 있습니다. 당신은'pathto'가 url 매개 변수에 의존하는 방법을 설명해야합니다. –

+0

이 내 코드에 대한 경로를 추가했습니다. –

답변

0
당신은/단축 바로 확인하고 함께 다른 if 문을 많이 제거 할 수

:

$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 대 1로 번역합니다. 다른 price 옵션이 어떻게 지정되었는지 확실하지 않았습니다 ...

관련 문제