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=".$product."&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.
something like that.
See also: http_build_query