0

This should be a failry simple question for a more experienced programmer.

Im working on a basic "pickem app" which allows users to predict who lets users vote on who they believe will win a sports game. Simple enough.

I wrote the following function to display statistics for each match. I.E the function returns how many votes each team received.

Doing a print_r() on the function it works perfectly and returns the correct results. The results are returned in the form of a multidimensional array containing. gameID, team, number_of_votes

My Code

$sql='SELECT gameID, team, COUNT(*) AS number_of_picks
          FROM picks
          WHERE picks.tournament = :tournament AND picks.weekNum = :weekNum 
          GROUP BY gameID, team
          ORDER BY gameID, team';
    $stmnt = $db->prepare($sql);
    $stmnt->bindValue(':tournament', $tournament);
    $stmnt->bindValue(':weekNum', $week);
    $stmnt->execute();
    if ($stmnt->rowCount() > 0) {
        $result = array();
        foreach ($stmnt->fetchAll() as $row) {
            $result[$row['gameID']][] = $row;
        }
        return $result;
        }
    return false;
}

Print_R function return data

![enter image description here

My Problem

My problem is displaying / echo'ing the data in my foreach loop. I have searched on SO and most answers points towards a simple foreach()

$picks = $calcStats('Tournament', Round);
foreach($picks as $pick){
echo $pick['gameID']; //gameID
echo $pick['team']; //Which Team Will Win
echo $pick['number_of_votes'] //How many votes did each team get
}

My problem is I keep on getting NULL values back when trying to echo variables above. I made a number of tweaks to the loop with same result.

What am I missing here? Any help / advice appreciated...

14
  • "Doing a print_r() on the function it works perfectly" - So where is that implemented in your test? You're also return'ing results, where and how are you using that return? return stops execution there and I don't know if you're using a class/methods to retrieve/echo those. Commented Feb 6, 2018 at 12:08
  • 2
    According to your print_r, you don't have gameID or number_of_votes. Commented Feb 6, 2018 at 12:09
  • there goes the screenshot Commented Feb 6, 2018 at 12:11
  • @FunkFortyNiner wrong screenshot appologies Commented Feb 6, 2018 at 12:11
  • 1
    @TimothyCoetzee So it's a multi-dimensional array? So you'll need 2 foreach..es Commented Feb 6, 2018 at 12:13

1 Answer 1

1

If your print_r() array is $picks, then your array is a multidimensional array, you need one more nested foreach() like below:

<?php
    $picks = $calcStats('Tournament', Round);
    foreach($picks as $keyArr){
        foreach($keyArr as $pick)
            echo $pick['gameID']; //gameID
            echo $pick['team']; //Which Team Will Win
            echo $pick['number_of_picks'] //it should be this according to your print_r data
        }
    }
Sign up to request clarification or add additional context in comments.

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.