0
<?php
$a = array('f-stat,porcelain mask'=>'jay azima','jay azima,oil painting'=>'japanese doll');
$msg="We can't recommend any items to you.";
foreach($a as $k=>$v)
{
  if($_GET['items']==$k)
  {
    header("location: hola.php?tequilla=".$v);
  }

}
header("location: hola.php?tequilla=".$msg);
?>

What I tried to do is if string from $_GET['items'] matches any of key in $a array, it is redirected to hola.php with $v value. Otherwise $msg string is passed. But the problem is even if the $k and $_GET['items'] matches, latter header function is executed with $msg value. Also if I remove the latter header function, the header function inside the loop works fine.

5
  • 2
    You need exit(); after the header lines so the script doesn't continue to run. Commented Aug 24, 2016 at 8:44
  • I prefer die(); ... I can be morbid like that - but it's the same thing. Commented Aug 24, 2016 at 8:45
  • Additionaly, you can use var_dump($_GET['items']); and trim() to make sure you do not have any spaces or to count characters while you are comparing string in your if condition. Commented Aug 24, 2016 at 8:47
  • $_GET['items']==$k seriously? Commented Aug 24, 2016 at 8:59
  • I'm a rookie to coding. What's wrong with $_GET['items']==$k?. Tip me, I can use the help. Thank You. -@u_mulder Commented Aug 24, 2016 at 14:03

3 Answers 3

1

All your code simplified:

$a = array(
    'f-stat,porcelain mask'=>'jay azima',
    'jay azima,oil painting'=>'japanese doll'
);
$v = !empty($a[$_GET['items']])? $a[$_GET['items']] : "We can't recommend any items to you.";

header("Location: hola.php?tequilla=" . $v);
die();
Sign up to request clarification or add additional context in comments.

2 Comments

Is that extra ';' intentional after the array? The problem persists anyway. :(
No, I removed it. What is the problem now?
0

Use exit() it stops the execution of the script. This will not redirect to second header.

<?php
    $a = array('f-stat,porcelain mask'=>'jay azima','jay azima,oil painting'=>'japanese doll');
    $msg="We can't recommend any items to you.";

    foreach($a as $k=>$v)
    {

      if($_GET['items']==$k)
      {
        header("location: hola.php?tequilla=".$v);
        exit(); // < this will stop the script before the 2nd header()
      }

    }

    header("location: hola.php?tequilla=".$msg);
    exit();

    ?>

4 Comments

the problem persists. Is is even okay to use header function inside the loop? Am I doing it wrong?
This is okay. Did you checked that $_GET['items'] value matched with $k ?
yeah.. i made it sure.. :)
Use trim function. May be space comes in your value.
0

try this

<?php
  $a = array('f-stat,porcelain mask'=>'jay azima','jay azima,oil painting'=>'japanese doll');
  $location = '';
  $msg="We can't recommend any items to you.";
  foreach($a as $k=>$v){
    if($_GET['items']==$k){
       $location = "hola.php?tequilla=".$v;
    }
  }
  if($location != ''){
     $location = "hola.php?tequilla=".$msg;
  }
  header("location: ".$location);
  exit(0);
?>

1 Comment

the problem persists. Is is even okay to use header function inside the loop? Am I doing it wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.