1

This is my code:

if ( 0 < $matches->total() ) {
    while ( $matches->fetch() ) {
        ?>
            <?php $ma1_winner      = $matches->display( 'winner' ); ?>
            <?php $ma1_team_1      = $matches->display( 'team_1' ); ?> 
            <?php $matches_array_1['winner'] = $ma1_winner; ?>
                <?php $matches_array_1['team1'] = $ma1_team_1; ?>
            <?php
                    } // end of while loop
            } // end of if any exists
            ?>


            <?php var_dump($matches_array_1); ?>
            <?php die(); ?>

But it outputs in var_dump only one winner and team not 15 from my database. How to fix it?

2
  • You only need/should only have one open/close pair of <?php ?> for that entire block of code. Commented Oct 11, 2012 at 18:06
  • Yeah, I know php tags are not a problem though. Commented Oct 11, 2012 at 18:07

2 Answers 2

1

For each iteration, append a new array with winner and team as its keys. The result will be a 2-dimensional array containing all your values.

while ($matches->fetch() {
  // Append a new array via [] = array()
  // for every loop iteration
  $matches_array_1[] = array(
     'winner'=>$matches->display('winner'), 
     'team'=>$matches->display('team')
  );
}
var_dump($matches_array_1);

Otherwise, you are just overwriting the same two keys winner and team on every iteration.

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

3 Comments

working! great, thanks a lot. Could I ask you why my code was not working?
@Derfder Yours didn't work because on every loop iteration, you were just setting the keys winner,team of a 1-dimensional array to the current loop's fetched values. Over and over, you overwrote the same two array keys.
ok, thanks a lot, I will tick your answer in 6 minutes according to the popup
1

You need some kind of unique match identifier for each match when constructing the array. Something like:

<?
if ( 0 < $matches->total() ) 
{
    while ( $matches->fetch() ) 
    {
        $matches_array_1[$matches->display('match_id')]['winner'] = $matches->display( 'winner' );
        $matches_array_1[$matches->display('match_id')]['team1']  = $matches->display( 'team_1' );
    } // end of while loop
} // end of if any exists
var_dump($matches_array_1); 
die();
?>

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.