Welcome Guest, Not a member yet? Register   Sign In
Passing ID variable within model query
#1

[eluser]include[/eluser]
Hi there,

Firstly hello! I'm very new to codeigniter and experimenting by building a simple project management tool, but i've hit something i can't figure out so was wondering if anybody could assist me :-)

I've got three database tables, Projects, Clients and Tasks.

Projects have a client ID field.
Tasks have a project ID field.

I want to display all the projects in the database, in the following format.

Project name
Client name
Progess (this consists of the number of incomplete tasks subtracted from total tasks for the project, then converting it into a percentage.

-

Now i've managed to get the page to display all the projects, with the associated client, but can't for the life of me work out how to then go and get all the tasks with the same ID as the project, then work out which one's are marked as complete.

It's been driving me round in loops all night, If i could pass the ID of the project to a function it would be easy (for example, i have done this on the "View Project" page) - but i can't figure out how to do it. I've a feeling i need to do some additional joining.

Code is below - any help would be gratefully received and i will shower you with praise for at least a week :-p

Project Controller;

Code:
public function index()
    {
        $this->load->model('projects_model');
    
        $data['projects'] = $this->projects_model->show_projects();    
            
        $data['page_title'] = 'Project Management | View Projects';        
        $data['page_content'] = 'projects_index';
        $this->load->view('includes/template', $data);
    }

Project Model;

Code:
function show_projects()
    {            
        $data = array();
        $this->db->select('projects.id, client_id, projects.name, company_name, projects.description, projects.due_date');
        $this->db->from('projects');
        $this->db->join('clients', 'projects.client_id = clients.id');
        $query = $this->db->get();
        return $query->result();
    }

and finally the view;

Code:
<div class="row">
    <div class="span12 column box box12_padding">
        <div class="page-header">
            <h1>Projects <small>View all projects currently in the system</small></h1>
        </div>
        
        &lt;?php echo anchor("projects/add_project/", "Add a project"); ?&gt;<p></p>
        
        &lt;?php if(isset($projects)) : foreach($projects as $row) : ?&gt;
        
        <div class="project_row">
            <div class="project_left">
                <h2 class="project_name">&lt;?php echo anchor("projects/view_project/$row->id", $row->name); ?&gt; </h2>    
                <div class="project_desc">&lt;?php echo $row->description; ?&gt;</div>
                <div class="project_client">&lt;?php echo anchor("clients/view_client/$row->client_id", $row->company_name); ?&gt;</div>    
            </div>
            <div class="project_right">        
                <div class="project_due">
                    &lt;? $datestring = "d/m/Y";
                    $h_due_date = $row->due_date;
                    $u_due_date = human_to_unix($h_due_date);?&gt;
        
                    Due - &lt;? echo date($datestring, $u_due_date); ?&gt;
                </div>
            </div>
            
        </div>
        
        &lt;?php endforeach; ?&gt;
        
        &lt;?php else : ?&gt;    
        <h2>No Projects are currently in the system.</h2>
        &lt;?php endif; ?&gt;
    </div>
    <div class="span4 columns box box4_padding">Sidebar</div>
</div>

If you need me to post any more code just ask.

Honestly, couldn't be more grateful for help, i've been toying with the idea of just not displaying progress on this page, but it seems a silly way out, and I'd love to learn what i need to do in situations like this.

Again, Thanks in advance for any help :-)

C.




Theme © iAndrew 2016 - Forum software by © MyBB