Pregunta

I’ve tried for some time now to solve what probably is a small issue but I just can’t seem get my head around it. I’ve tried some different approaches, some found at SO but none has worked yet.

The problem consists of this:
I’ve a show-room page where I show some cloth. On each single item of cloth there is four “views”

  1. Male
  2. Female
  3. Front
  4. Back

Now, the users can filter this by either viewing the male or female model but they can also filter by viewing front or back of both gender.

I’ve created my script so it detects the URL query and display the correct data but my problem is to “build” the URL correctly.

When firstly enter the page, the four links is like this:

  1. example.com?gender=male
  2. example.com?gender=female
  3. example.com?site=front
  4. example.com?site=back

This work because it’s the “default” view (the default view is set to gender=male && site=front) in the model.

But if I choose to view ?gender=female the users should be able to filter it once more by adding &site=back so the complete URL would be: example.com?gender=female&site=back

And if I then press the link to see gender=male it should still keep the URL parameter &site=back.

What I’ve achived so far is to append the parameters to the existing URL but this result in URL strings like: example.com?gender=male&site=front&gender=female and so on…

I’ve tried but to use the parse_url function, the http_build_query($parms) method and to make my “own” function that checks for existing parameters but it does not work.

My latest try was this:

_setURL(‘http://example.com?gender=male’, ‘site’, ‘back’);
function _setURL($url, $key, $value) {
    $separator = (parse_url($url, PHP_URL_QUERY) == NULL) ? '?' : '&';

    $query = $key."=".$value;
    $url .= $separator . $query;
    var_dump($url); exit;
}

This function works unless the $_GET parameter already exists and thus should be replaced and not added.

I’m not sure if there is some “best practice” to solve this and as I said I’ve looked at a lot of answers on SO but none which was spot on my issue.

I hope I’ve explained myself otherwise please let me know and I’ll elaborate.
Any help or advice would be appreciated

¿Fue útil?

Solución

You can generate the links dynamically using the following method:

$frontLink = (isset($_GET['gender'])) ? 'mydomain.com?gender='.$_GET['gender'].'&site=front':'mydomain.com?site=front';
$backLink = (isset($_GET['gender'])) ? 'mydomain.com?gender='.$_GET['gender'].'&site=back':'mydomain.com?site=back';

This is a 1 line if statement which will set the value of the variables $frontLink and $backlink respectively. The syntax for a 1 line if statement is $var = (if_statement) ? true_result:false_result; this will set the value of $var to the true_result or false_result depending on the return value of the if statement.

You can then do the same for the genders:

$maleLink = (isset($_GET['site'])) ? 'mydomain.com?gender=male&site='.$_GET['site']:'mydomain.com?gender=male';
$femaleLink = (isset($_GET['site'])) ? 'mydomain.com?gender=female&site='.$_GET['site']:'mydomain.com?gender=female';

Otros consejos

Found this by searching for a better solution then mine and found this ugly one (That we see a lot on the web), so here is my solution :

function add_get_parameter($arg, $value)
{
    $_GET[$arg] = $value;

    return "?" . http_build_query($_GET);
}
<?php
function requestUriAddGetParams(array $params)
{
    $parseRes=parse_url($_REQUEST['REQUEST_URI']);
    $params=array_merge($_GET, $params);
    return $parseRes['path'].'?'.http_build_query($params);
}
?>
if(isset($_GET['diagid']) && $_GET['diagid']!='') { 
     $repParam = "&diagid=".$_GET['diagid']; 
     $params = str_replace($repParam, "", $_SERVER['REQUEST_URI']); 
     $url = "http://".$_SERVER['HTTP_HOST'].$params."&diagid=".$ID; 
} 
else $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&diagid=".$ID;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top