Question

I have this script, which works perfectly well. BUT I will end up creating hundreds of variations if I keep doing it this way.

    <?php

$q1 = $_GET["q1"];
$q2 = $_GET["q2"];
$q3 = $_GET["q3"];
$q4 = $_GET["q4"];


if ( $q1 == "a" && $q2 == "a" && $q3 == "a" && $q4 == "a" ) {
    header("Location: http://www.mostly-a.co.uk");
    exit;    
}

if ( $q1 == "b" && $q2 == "b" && $q3 == "b" && $q4 == "b" ) {
    header("Location: http://www.mostly-b.co.uk");
    exit;    
}

?>

Basically I need the script to echo 1 of 5 possible urls based on which answers are given

So for example, "url-mostly-a" would be echo'd if the user selected: aaaa aaab aaba abaa baaa aaac aaca acaa caaa

etc etc.....

Était-ce utile?

La solution

4 lines should do it:

$count = array_count_values($_GET);
arsort($count);
$answers = array_keys($count);
header("Location: http://www.mostly-{$answers[0]}.co.uk");
  1. count values occurences
  2. reverse sorting of values
  3. get an array with the keys (still sorted)
  4. use first value of array

Autres conseils

If you understood correctly, what you need first is to find the most common value in array $_GET.

For that you need to get a count of duplicates:

array_count_values($_GET);

Then iterate and find the biggest value.


Edit:

Then you might be able to use this to get the key "name" with the biggest value:

$arrayCnt = array_count_values($_GET);
$theKey = array_search(max($arrayCnt), $arrayCnt)

Are searching for something like this?:

foreach(array(
    'a' => 'http://aaaaaa...', 
    'b' => 'http:/bbbb',
    // ...
as $check => $url) {
    if($q1 == $check && $q2 == $check && $q3 == $check && $q4 == $check) {
        header("Location: $url");
    }
}

The following is a working code-example. It validated the input and as you can see, input validation is a a large part of any script.

// configuration

$qsValidKeys  = ['q1' => 0, 'q2' => 0, 'q3' => 0, 'q4' => 0];
$qValidValues = ['a', 'b', 'c', 'd'];

// input

$qsGet = array_intersect_key($_GET, $qsValidKeys);

if (!$qsGet) {
    trigger_error('No input given.');
    return;
}

$qsFiltered = [];

foreach ($qsGet as $key => $value) {
    if (in_array($value, $qValidValues, true)) {
        $qsFiltered[$key] = $value;
    } else {
        trigger_error(sprintf('Invalid Input value for "%s".', $key));
    }
}

if (!$qsFiltered) {
    trigger_error('No input given (filtered).');
    return;
}

// processing

$count = array_count_values($qsFiltered);
arsort($count);
$topAnswer = array_keys($count)[0];
$location = sprintf("http://www.mostly-%s.co.uk", $topAnswer);

Thanks M8R-1jmw5r,

I looked into what it all meant and get the basic grasp of it all so thanks for that! The very last part didn't seem to be working though so I changed it too:

$location = printf('Click <a href="http://www.open.ac.uk/'.'%s'.'/">here</a> to view your results', $topAnswer);

This now seems to work perfectly well, I hope its still secure enough!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top