Welcome Guest, Not a member yet? Register   Sign In
Query database and insert result in an existing array
#1

[eluser]keugant[/eluser]
Hello everyone,

I'm currently working on a community site built with CodeIgniter and I keep bumping into an error while trying to insert some data in an existing array. I'm new to the MVC pattern and to the framework itself so please excuse any obvious error.

Here is the problem and source code:

I query the database to get a list of groups (in the model):
Code:
public function view_groups() {
                $sql = "SELECT
                                `id`, `name`, `description`
                        FROM
                                `groups`";
                $query = $this->db->query($sql);
                return $query->result();
}

Then, while the results are passed to the controller, I send them back to another function to check if the current user have joined the group:
Controller:
Code:
public function groups() {
                        $this->load->model('groups_model');
                        $results = $this->groups_model->view_groups();
                        foreach($results as $row) {
                                $status = $this->groups_model->is_joined($row->id);
                                if($status == TRUE) {
                                        $row->status => 'true';
                                }
                                else {
                                        $row->status => 'false';
                                }
                        }
                

                $this->load->view('includes/template', array(
                        'groups' => $results,
                        'main_content' => 'groups'
                ));
        }

Model:
Code:
public function is_joined($group_id) {
                $sql = "SELECT * FROM `interface_groups` WHERE `user_id` = ".$this->session->userdata('id')."AND `group_id` = ".$group_id;
                
                $query = $this->db->query($sql);
                if($query->num_rows() > 0) {
                        return true;
                }      
                else {
                        return false;
                }      
        }

The purpose is to get the existing groups (that can be created by users), I check if the current user is part of them or not (detail stored in the interface_group table). But, all I get is a blank page..

Could you please help.

Thank you
#2

[eluser]CroNiX[/eluser]
Code:
foreach($results as $row) {

Try changing it to
Code:
foreach($results as &$row) {//pass by reference - use ampersand
that way when you
Code:
$row->status => 'true'; // or false
it will be overwriting the value in the ORIGINAL array ($results - which is what you are returning). Otherwise you're just changing the value of the $row variable which gets overwritten on each iteration and has nothing to do with $result.
#3

[eluser]keugant[/eluser]
Thank you very much, I didn't know about this technique. Maybe you could tell me about a different approach to achieve what I'm trying to do, in theory at least.
Should I use two functions in the model or only one? Should I link the two in the controller?

Thanks Smile
#4

[eluser]keugant[/eluser]
Thank you sorry I typed it wrong, but this is definitely the solution Smile

Thanks a lot Smile




Theme © iAndrew 2016 - Forum software by © MyBB