开发者

how to check for multi parameters in a url when printing the value or a link or selection

when i have a link like this which can be longer but i've put a short version for my question.

index.php?product=jeans&gender=male&lang=english

if the selection for the language looked like this

<select name="lang">
<option value="<?=$url;?>">english</option>
<option value="<?=$url;?>">spanish</option>
</select>

for the url variable i'll have something like this.

<?php

if(isset($_GET['lang']))
{
    $lang = $_GET['lang'];
    if(isset($_GET['product']) && !isset($_GET['gender']))
    {
        $product = $_GET['product'];
        $url = "index.php?product=".$product."&lang=".$lang;
    }
    elseif(isset($_GET['product']) && isset($_GET['gender']))
    {
        $product = $_GET['product'];
        $gender = $_GET['gender'];
        $url = "index.php?product=".$pr开发者_开发问答oduct."&gender=".$gender."&lang=".$lang;
    }
}
elseif(!isset($_GET['lang']))
{
    if(isset($_GET['product']) && !isset($_GET['gender']))
    {
        $product = $_GET['product'];
        $url = "index.php?product=".$product."&lang=english";
    }
    elseif(isset($_GET['product']) && isset($_GET['gender']))
    {
        $product = $_GET['product'];
        $gender = $_GET['gender'];
        $url = "index.php?product=".$product."&gender=".$gender."&lang=english";
    }
}

?>

as you can see it gets very messy. just to make sure the end-user is always on his/her correct page no matter what change is done in the param. what is the best way of doing this? im sick of checking soo much data just to make sure the end-user is on the correct page. how would you have had done this?

note: i havent written any functions to clean the gets i just wanted to write something clean so it is easy to read.


function defaultOrGet($param, $default = "") {
    return ( isset($_GET[$param]) ? $_GET[$param] : $default );
}


$lang = defaultOrGet('lang', 'english');
$product = defaultOrGet('product');
$gender = defaultOrGet('gender');

$url = 'index.php?'

if (!empty($product)) {
    $url .= 'product=' . $product . '&lang=' . $lang;

    if (!empty($gender)) {
        $url .= '&gender=' . $gender;           
    }
}
// else $url point to index.php

something like that.

See also: http_build_query

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜