1

So I have a few for each loops, and I'm trying to echo out the value of $sql at the very end. I'm actually using it with PDO but the echo demonstrates the issue.

$values = "1,2";
$values = explode(',',$values);

$set1 = "2,4";
$set1 = explode(',',$set1);
$set2 = "3,2";
$set2 = explode(',',$set2);

foreach($set1 as $set1val){
  if ($set1val==2) {
    $sql = "sql one runs";//should run on 1st iteration since 2 will equal 2
    echo 'hi';//should echo on 1st iteration since 2 will equal 2
  }
}

foreach($set2 as $set2val){
  if ($set2val==2) {
    $sql = "sql two runs";//should run on 2nd iteration since 2 will equal 2
    echo 'bye';//should echo on 2nd iteration since 2 will equal 2
  }
}

foreach($values as $value){
  echo $sql;
  $stmt = $db->query($sql);
}

//The Result Output
'hi'  'bye'  'sql two runs'  'sql two runs' <-- doesn't echo 'sql one runs' 

//The Output Required
'hi'  'bye'  'sql one runs'  'sql two runs' <-- Works great, but not outputting this

How can I make it output the output required? Any idea how to do this so the correct statement is echoed/runs?

2
  • What are you trying to do here? Looping through an array just to do something for a single value is a nonsense pattern. Also, are you trying to... dynamically decide which query run? Or what exactly is this code supposed to do? Commented Feb 2, 2012 at 0:39
  • @deceze I think I was complicating something more than I should have. I was trying to dynamically decide which query to run using the same variable name, hoping it would take into consideration the order the $sql variables were 'activated' and execute them in that order, instead of overwriting the previous run of it and running the same query twice. Does that make any sense? Commented Feb 2, 2012 at 3:58

2 Answers 2

2

It never echos sql one runs because the second foreach overwrites it with sql two runs. If you need to save the sql strings through the end of the script, you could store them in an array. It's difficult to specify how as it's not clear how $values is related to the SQL, i.e., which SQL should run with which $value.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks webbiedave, I already know what the issue is, I'm trying to find a solution
I did. I added store them in an array.
0

The problem is that you first save sql one runs in $sql, but the next thing you do with $sql is saving sql two runs, overwriting the previous value. So you need to save those values in an array, and modify the last foreach. Something like this:

foreach($set1 as $set1val){
  if ($set1val==2) {
    $sql[0] = "sql one runs";//should run on 1st iteration since 2 will equal 2
    echo 'hi';//should echo on 1st iteration since 2 will equal 2
  }
}

foreach($set2 as $set2val){
  if ($set2val==2) {
    $sql[1] = "sql two runs";//should run on 2nd iteration since 2 will equal 2
    echo 'bye';//should echo on 2nd iteration since 2 will equal 2
  }
}

foreach($sql as $value){
  echo $value;
  $stmt = $db->query($value);
}

Comments

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.