Welcome Guest, Not a member yet? Register   Sign In
How to make this fully MVC?
#1

[eluser]Slowcheetah[/eluser]
I have a problem with the making this code 100% MVC. It works perfect like this, but the view is not 100% MVC

I'm relative new with the OOP and MCV principle.Thanks in advance..

FULL CODE

MODEL
Code:
function get_clubtypes()
            {
                $query = $this->db->query('
                SELECT
                    club_types.id as clubtypeid,
                    club_types.name as clubtypename,
                    COUNT(clubs.id) as clubtypecount
                FROM
                    club_types
                INNER JOIN
                    clubs
                ON
                    club_types.id = clubs.club_type_id                    
                GROUP BY
                    clubtypename
                ORDER BY
                    clubtypename
                ASC');
                
                return $query->result();
            }

        function get_clubs_by_type($id)
            {
                $query = $this->db->query('
                SELECT
                    clubs.id as clubid,
                    clubs.name as clubname
                FROM
                    clubs
                WHERE
                    clubs.club_type_id  = "'.$id.'"
                ORDER BY
                clubname
                ASC');

                return $query->result();
            }
CONTROLLER
Code:
function overzicht()
            {
                $this->load->model('vereniging_model');

                $data['get_clubtypes'] = $this->vereniging_model->get_clubtypes();

                $this->load->view('vereniging/overzicht', $data);
            }
VIEW
Code:
<?php
                    // Loop trough clubtypes
                    foreach ($get_clubtypes as $clubtype)
                        {
                            // Setting Variables
                            $clubtypeid = $clubtype->clubtypeid;
                            $clubtypename = $clubtype->clubtypename;
                            $clubtypecount = $clubtype->clubtypecount;
                    
                            // Return Club Type Name
                            echo '<h4 class="club-overview-title">'.$clubtypename.' ('.$clubtypecount.')</h4>';
                            echo '<ul class="club-overview clear">';
                    
                            // Load Clubs (NOT MVC)
                            $get_clubs_by_type = $this->vereniging_model->get_clubs_by_type($clubtypeid);
                    
                            // Loop trough Clubs
                            foreach ($get_clubs_by_type as $club)
                                {
                                    // Setting Variables
                                    $clubname = $club->clubname;
                                                                
                                    // Return Clubs
                                    echo '<li>'.anchor('', $clubname).'</li>';
                                }
                            echo '</ul>';
                        }
                    
                    ?&gt;
#2

[eluser]TheFuzzy0ne[/eluser]
I would probably do it something like this (untested:

MODEL:
Code:
function get_club_types()
{
    $this->db->select('club_types.id, club_types.name, COUNT(clubs.id) as count', FALSE);
    $this->db->from('club_types');
    $this->db->join('clubs', 'club_types.id = club_type_id', 'INNER');
    $this->db->group_by('name');
    $this->db->order_by('name');
    $res = $this->db->get();
    
    if ($res->num_rows() > 0)
    {
        return $res->result_array();
    }
    
    return FALSE;
}

function get_clubs_by_type($club_type_ids=array())
{
    if (is_array($club_type_ids) && count($club_type_ids) > 0)
    {
        $this->db->select('id, name, club_type_id');
        $this->db->from('clubs');
        $this->db->where_in('club_type_id', $club_type_ids);
        $this->db->order_by('name');
        $res = $this->db->get();
        
        if ($res->num_rows() > 0)
        {

            $res = $res->result_array();
            $ret = array();
            
            foreach ($res as $row)
            {
                $ret[$row['club_type_id']][] = $row['name'];
            }
            $res->free_result();
            
            return $ret;
        }
    }
    
    return FALSE;
}

CONTROLLER:
Code:
function overzicht()
{
    $this->load->model('vereniging_model');
    
    $data['club_types'] = $this->vereniging_model->get_club_types();
    
    $club_type_ids = array();
    foreach ($data['club_types'] as $club_type) { $ids[] = $club_type['id']; }
    
    $data['clubs'] = $this->vereniging_model->get_clubs_by_type($ids);
    
    $this->load->view('vereniging/overzicht', $data);
}

VIEW:
Code:
&lt;?php foreach ($club_types as $club_type): ?&gt;

    <h4 class="club-overview-title">&lt;?php echo $club_type['name'] ?&gt; (&lt;?php echo $club_type['count']; ?&gt;)</h4>';
    <ul class="club-overview clear">';
    
    &lt;?php foreach ($clubs[$club_type['id']] as $club): ?&gt;
        <li>&lt;?php echo anchor('', $club['name']); ?&gt;</li>';
    &lt;?php endforeach; ?&gt;
    </ul>
&lt;?php endforeach; ?&gt;

I'm sure there's a lot of room for improvement, however.




Theme © iAndrew 2016 - Forum software by © MyBB