0

I need to pass a variable to a foreach loop from a mySQL result.

So I have this code:

    $GetClaim = "SELECT * FROM cR_Claimants WHERE memberID = '".$memberID."' AND ParentSubmission ='".$refNumb."'";
    $resultGetClaim=mysql_query($GetClaim) or die("Error select claimants: ".mysql_error());
    while($rowGetClaim = mysql_fetch_array($resultGetClaim)) {
    $name = $rowGetClaim['Name'];
$city = $rowGetClaim['city'];
$region = $rowGetClaim['region'];

    }

Now I need to pass the variable to the foreach

    foreach($name as $k=>$v) {
    echo $city;
echo $region;
etc..
    }

The above code does not work. I think I cannot pass a variable from a mySQL loop. The problem is also tat every row I get from the database should be related to the specific $name. So obvioiusly one $name will have its own $city etc..

How do I achieve this?

Please help

5
  • 1
    $name is just a variable . not an array is it? Commented Jun 16, 2011 at 20:22
  • 1
    What's the problem with the first code block? Looks like you already get the names. Commented Jun 16, 2011 at 20:22
  • The foreach is not working as it should. And yes $name is an array. There are more $name that will get from the database result..This is why I need a foreach $name. Commented Jun 16, 2011 at 20:24
  • 1
    $name cannot be an array in the code you show... its the value of the Name column (ell unless you are somheow unserializing it, but if thats the case you have completely ommited that code) Commented Jun 16, 2011 at 20:29
  • @prodigitalson this is why I am asking this question...because I need to know how?! Commented Jun 16, 2011 at 20:31

2 Answers 2

2

You are not retrieving an array with all returned records, you are retrieving an array which contains a single record.

To get the next name (the next record), you must make another call to mysql_fetch_array.

The code you present does that implicitly by assigning $rowGetClaim within a while conditional. A failed mysql_fetch_array call would return false, which would exit the while loop.

There is absolutely no need to use the for each as you presented. Just place the echo right after the assignment (e.g.

$region = $rowGetClaim['region'];

echo $region

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

2 Comments

No no no..That is simple I know that..I really need to know how to pass that in a foreach. I cannot explain the reason because I would need one hour in that case. I just need to know how to transform that $names in an array valid for the foreach and that EVERY result related to $name will be consecutive in the foreach, such as $city, $region etc...
for each works on arrays. $name isn't an array and DOES NOT CONTAIN EVERY RECORD. period. The while is your for each statement in all relevant respects.
1

Either out put directly fromt eh loop or build an array and then loop through it.

while($rowGetClaim = mysql_fetch_array($resultGetClaim)) {
  echo $rowGetClaim['Name'];
  echo $rowGetClaim['city'];
  echo $rowGetClaim['region'];

}

OR

while($rowGetClaim = mysql_fetch_array($resultGetClaim)) {
  foreach($rowGetClaim as $k => $v{
    echo $v;
  }
}

OR

$names = array();
while($rowGetClaim = mysql_fetch_array($resultGetClaim)) {
  $names[] = $rowGetClaim;
}


foreach($names as $data){
  foreach($data as $k => $v) {
    echo $v;
  }
}

4 Comments

I am not sure to understand the last code. Why do you use two foreach, one for $names and one for $data? I just need to pass the array $names from the mySQL loop to the foreach and then the results should be related and consequential..Is that what the code you provided does?
foreach ($data as $k=>$v) splits out each row into name/value pairs such that $data[$k]=$v and for every value of $k. It means you don't need to hard-code what you're expecting to see in the $data array (in terms of keys)
Well your question wasnt clear. The third example puts all of the rows retrived from the DB into an array in the while loop. Then in the foreach loop it goes over that array outputting all the values of the row. You woudl use this example if you had the mysql code in a serete file on in a completely separate part of the document from where you want to output.
The firs and second examples both out the data directly in place while youre actually looping over the result set. The only differrence is that example one outputs only 3 of the columns and explicity access them by associative key. The second example outputs all the columns on a record and does so using a loop.

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.