سؤال

In my application I am trying to populate a sidebar menu list with rows from a database table. More specifically, I try to retrieve team name where the coach has the same user_id as the user that is logged in. I want to list the logged in coach's teams listed in a menu.

In my Model, I am using Active Record to get the desired query results:

public function get_team()
{
    $this->db->select('teamname')->from('teams')->where('coaches', $this->session->userdata('user_id'));
    $this->db->order_by('teamname', 'asc');
    $query = $this->db->get();

    if($query->num_rows() > 0)
    {
        foreach($query->result_array() as $row)
        {
            $data[] = $row;
        }
        return $data;
    }   
}

In the Controller, I am not sure how to go about in order to pass the array data to my view. In my index function, I do this:

public function index()
{
    if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
    } else 
        {
            $data['title'] = 'Team';
            $data['main_content'] = 'team_v';
            $data['username'] = $this->tank_auth->get_username();
            $data['coach'] = $this->tank_auth->is_admin();
            $this->load->vars($data);
            $this->load->view('includes/template');
        }
}

And in the relevant controller function, I try to send the data directly to the sidebar view, (which is included in the previously loaded template file):

public function get_team()
{
    $data = array(
    'result' => $this->team_m->get_team());

    $this->load->view('includes/sidebar', $data);
}

The view code performs a standard foreach and looks like this:

    <li>Teams</li>
        <ul>
            <?php /* if (empty($result)): */ ?>
            <li>Create a team</li>
            <?php /* else: */ ?>
            <?php foreach ($result as $row): ?>
            <li><?php echo $row['teamname'];?></li>  
            <?php endforeach; ?>
            <?php /* endif; */ ?>
        </ul>
    
    <li>Messages</li>
    <li>My profile</li>
    <li><a href="<?php echo base_url('index.php/auth/logout'); ?>">Logout</a></li>
    

Sending the $data array to the sidebar this way doesn't work. I have also tried to load all the views in the template manually and passing the data to the views, but to no avail. No matter how I pass it, it gives me the "Invalid argument supplied for foreach()" error, which AFAIK means that no data gets returned in the array.

However, if I do this in the controller function:

public function get_team()
{
    $data = array(
    'result' => $this->team_m->get_team());

    $this->load->view('get_team_v', $data);
}

and make a get_team_v view with the exact same code in the sidebar view above, then the desired result is listed perfectly. I have also performed an affected_rows test and confirmed that the query part is good. What should I do to get the array data to be listed also in the templated sidebar view?

هل كانت مفيدة؟

المحلول

Solved, for anyone browsing.

Passing the arrays in the index method of the controller like below did it for me.

$data['result'] = $this->team_m->get_team_by_coach();

$this->load->vars($data);

And in the sidebar view like so:

<ul id="menu" class="nav nav-pills nav-stacked">
<li><a href="<?php echo base_url(); ?>"><span class="glyphicon glyphicon-list"></span>My Teams</a></li>
    <ul id="submenu" class="nav">
        <?php foreach ($result as $row): ?>
        <?php echo '<li><a href="http://localhost/master/index.php/team/' . $row['id'] . '">' . $row['teamname'] . '</a></li>';?> 
        <?php endforeach; 
            if(count($result) <= 2) 
            {
                echo '<li><a data-toggle="modal" id="create_team" data-backdrop="true" href="#create_team_modal" href="base_url(index.php/team/create_team)"><span class="glyphicon glyphicon-plus"></span>Create a team</a></li>';
            }
         ?>
    </ul>
<li><a href="<?php echo base_url(); ?>index.php/message"><span class="glyphicon glyphicon-envelope"></span>Messages</a></li>
<li><a href="<?php echo base_url(); ?>index.php/profile"><span class="glyphicon glyphicon-user"></span></i>My Profile</a></li>
<li><a href="<?php echo base_url(); ?>index.php/auth/logout"><span class="glyphicon glyphicon-log-out"></span>Logout</a></li>

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top