0

I'm trying to do a for loop inside a foreach loop.

I have a table in my database as follows:

ID        | Fname | Lname
------------------------
1          Bart    Simpson
1          Mickey  Mouse
2          Peter   Griffin
2          Clark   Kenet
2          David   Johnson

i call the data from the database (and it works, tested on phpmyadmin), I want the data to be shown on the page by the ID value for exemple:

Bart Simpson, Mickey Mouse & Peter Griffin

Clark Kenet & David Johnson

as you see there in an "&" before the last name at any row but that isnt my problem.

this is the code i wrote:

//HERE I RETRIVE ALL THE DATA FROM THE DATABASE.
$query = $dbh->query(" SELECT *
        FROM table
        WHERE ID= '{$ID}'
        ");   

//THIS CODE SHOWS THE NUMBER OF ROWS WITH THE CURRENT ID 
        $sql    = "SELECT COUNT( * ) FROM table WHERE ID= '{$ID}'";
        $result = $dbh->prepare($sql);
        $result->execute();
        $number_of_rows = $result->fetchColumn();

//THIS IS THE FOREACH IN WHICH I CALL ALL THE ROWS BY THEIR VALUE
          foreach ($query as  $key =>$value) {
                   $value['wFname'][$key] = !empty($value['wFname'][$key]) ? $value['wFname'][$key] : '';                      
$numRow = $number_of_rows-2; // THE ITEM BEFORE THE LAST ONE IN THE ARRAY
$lastItem = $number_of_rows-1; //LAST INDEX IN THE ARRAY
for($counter = 0; $counter = $numRow; $counter++){
 return  $value['wLname'][$counter] . ', '.  $value['wFname'][$counter];
  }
   return  "&" . $value['wLname'][$lastItem] . ', '.$value['wFname'][$lastItem] ;
   return ' ';

                };

with this code i get only the first pair of names with each id. any help would be great.

1 Answer 1

1

Try changing:

for($counter = 0; $counter = $numRow; $counter++){

to:

for($counter = 0; $counter =< $numRow; $counter++){

You are checking if $counter equals $numRow, and you should check if it equals and is less than...

Hope this helps.

EDIT Based on user requests

Try storing the results in a variable and return that variable at the end of both loops, but im not sure what you are trying to achieve here, try running the following code:

//HERE I RETRIVE ALL THE DATA FROM THE DATABASE.
        $query = $dbh->query(" SELECT *
        FROM table
        WHERE ID= '{$ID}'
        ");

//THIS CODE SHOWS THE NUMBER OF ROWS WITH THE CURRENT ID 
        $sql = "SELECT COUNT( * ) FROM table WHERE ID= '{$ID}'";
        $result = $dbh->prepare($sql);
        $result->execute();
        $number_of_rows = $result->fetchColumn();

//THIS IS THE FOREACH IN WHICH I CALL ALL THE ROWS BY THEIR VALUE
        $return = "";
        foreach ($query as $key => $value) {
            $value['wFname'][$key] = !empty($value['wFname'][$key]) ? $value['wFname'][$key] : '';
            $numRow = $number_of_rows - 2; // THE ITEM BEFORE THE LAST ONE IN THE ARRAY
            $lastItem = $number_of_rows - 1; //LAST INDEX IN THE ARRAY
            for ($counter = 0; $counter <= $numRow; $counter++) {
                $return .= $value['wLname'][$counter] . ', ' . $value['wFname'][$counter];
            }
            $return .= "&" . $value['wLname'][$lastItem] . ', ' . $value['wFname'][$lastItem];
            $return .= ' ';
        }
        return $return;
Sign up to request clarification or add additional context in comments.

8 Comments

TNX, not anging anyting, i dont get wy i dont see te last result
Thats because your return statement, instead of returning in the for loop you should assign the value to a variable and then show it when the loops ends after the curly braces.
tnx, the foreach is a part of a method, thats why it is assignd in return
Yes, but at soon the code reaches the return, the method will stop executing the for loop.
didn't knew tat retun kills a for loop. tnx! I chaged the return inside the loop but nothing changed, still see only te fisrt pair of names
|

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.