dt56449492 2017-05-09 18:55
浏览 85

如何在php中检索多个url参数

I have 4 parameter in my URL. I retrieve the url parameter from my url that is given. With every parameter I'm changing the path to a directory and get different images.

My sample url look like this:

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

The code is working but it's a spagheti code.

Is there a solution to make is less DRY ? How do I retrieve multiple url parameter ?

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
}

The path d=german, e=english, c=czech, p=prices, n=noprices

  • 写回答

2条回答 默认 最新

  • dtkwt62022 2017-05-09 21:03
    关注

    You could shorten/remove many if else statements with just doing the checks:

      $lang_code = $language[0];
    

    There you have your first letter, you can do the same with every GET parameter. So you can use that as in:

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

    You can do the same for everything else.

    P.s.: don't forget to sanitze any user input i.e.:

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog