We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,220 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP Framework and Fetch Assoc

I am using a framework, and I am having an issue with getting the results of this particular associative array. All others work except for this one and I am not sure why. This one only returns one result. Here is what I have in my model:

    public function listStats($id) {
        $sql = DB::inst()->query( "SELECT * 
            FROM ".
                TP."tracking 
            WHERE 
                mID = '$id' 
            GROUP BY 
                mID" 
        );
        $data = array();
        if($sql->num_rows > 0) {
            while($row = $sql->fetch_assoc()) {
                $data[] = $row;
            }
            return $data;
        }
    }

And here is what I have in my view:

        <?php foreach($this->listStats as $k => $v) : ?>
        <tr>
            <td>
                <?php echo clean($v['email']); ?>
            </td>
        </tr>
        <?php endforeach; ?>

Any help with figuring this out is greatly appreciated.

3
Contributors
4
Replies
14 Hours
Discussion Span
3 Months Ago
Last Updated
6
Views
Question
Answered
joshmac
Junior Poster in Training
84 posts since Apr 2008
Reputation Points: 16
Solved Threads: 5
Skill Endorsements: 0

Hi,

What framework are you using?

can you var_dump or print_r this ?

listStats($id);

and this

$this->listStats

what do you get?

I am assuming that $this->listStats is a method of the view class?

veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 130
Skill Endorsements: 13

try this first... put this before the closing bracket of the method listStats;;

    return $data;
    } // closing bracket of method listStats..

like this ..

    public function listStats($id) {
$sql = DB::inst()->query( "SELECT *
FROM ".
TP."tracking
WHERE
mID = '$id'
GROUP BY
mID"
);
$data = array();
if($sql->num_rows > 0) {
while($row = $sql->fetch_assoc()) {
$data[] = $row;
}

}

    return $data;
}
veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 130
Skill Endorsements: 13

Also, since you are querying a specific mID and then grouping by the same field, it's correct to get just a row. So try to change the GROUP BY clause. Bye!

cereal
Veteran Poster
1,145 posts since Aug 2007
Reputation Points: 344
Solved Threads: 222
Skill Endorsements: 22

HMM, the GROUP BY was a later addition because it wasn't working, but removing it makes it work now. Thanks.

joshmac
Junior Poster in Training
84 posts since Apr 2008
Reputation Points: 16
Solved Threads: 5
Skill Endorsements: 0
Question Answered as of 3 Months Ago by veedeoo and cereal

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0722 seconds using 2.72MB