Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert old procedural PHP function into Model-View-Controller (MVC) in Codeigniter

I am new to Model-View-Controller and I started coding in Codeigniter. I am basically converting my project into MVC, however, I encountered this function(below) which I would like to split into MVC. I have 100s of functions like this and if I get the best approach to this, I will be able to convert the rest of my functions into MVC myself.

This function contains PHP, Mysql, and HTML everything in one. Like we split queries and HTML separately, I also want to do it using Codeingiter framework. Even if you cannot answer using codeigniter default functions, just tell me how to split.

Here it is :

 $fetch_projections = mysql_query("SELECT issue_id, emp_name, employeeId, sum(actualHoursPerDay) as ss FROM day_projections WHERE date = '$today' GROUP BY employeeId ORDER BY emp_name ASC");
    while ($r = mysql_fetch_array($fetch_projections)) {
        $maes_array[] = $r['issue_id'];
        $all_maes_for_emp = implode($maes_array);
        // echo $r['emp_name'] $r['ss'].'<br/>'; 

        $split_up_query = mysql_query("SELECT issue_id, actualHoursPerDay FROM day_projections WHERE date = '$today' AND emp_name = '" . $r['emp_name'] . "'");
        while ($t = mysql_fetch_array($split_up_query)) {
            $kk[] = $t['issue_id'] . ' = ' . $t['actualHoursPerDay'] . ' hrs';
        }
        $pp = implode(', ', $kk);
        $cap = round((((8 - $r['ss']) / 8) * 100), 2);
        echo '<tr><td>' . $r['emp_name'] . '</td><td>' . $cap . '%</td><td>' . $r['ss'] . ' hrs</td><td>' . $pp . '</td></tr>';
        unset($maes_array);
        unset($kk);
    }

Thanks

like image 922
user1941043 Avatar asked Aug 18 '26 00:08

user1941043


1 Answers

Your code is a bit funky and not optimal. You are recalling a sql query and iterating where you dont need to. What I would do to fix it is take advantage of MYSQL's GROUP_CONCAT, then convert it all into MVC using Codeigniter. Here's my approach:

Model: application\models\My_model.php

The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.

class My_model extends CI_MODEL{

    function fetch_projections($today){
        $this->db->select("emp_name, sum(actualHoursPerDay) as ss, GROUP_CONCAT( issue_id,'=',actualHoursPerDay,'hrs' SEPARATOR ';') as pp");
        $this->db->from("day_projections");
        $this->db->where("date" , $today);
        $this->db->group_by("employeeId");
        $this->db->order_by("emp_name" , "asc");
        $query = $this->db->get();
        return $query->result();
    }

}

Controller: application\controllers\My_controller.php

The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.

class My_controller extends CI_Controller {

    function calculate() {
        $today = "0000-00-00"; // or whatever code you have to come up for "today"
        $this->load->model("My_model");
        $projections_results = $this->My_model->fetch_projections($today);
        if ($projections_results) {
            foreach ($projections_results as $projection) {
                $projection->cap = round((((8 - $projection->ss) / 8) * 100), 2);
            }
        }
        $view_data["results"] = $projections_results;
        $this->load->view("my_view", $view_data);
    }

}

View: application\views\my_view.php

The View is the information that is being presented to a user. A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of "page".

<table>
    <?php foreach ($results as $res) { ?>
        <tr>
            <td><?= $res->emp_name ?></td>
            <td><?= $res->cap ?>%</td>
            <td><?= $res->ss ?>hrs</td>
            <td><?= $res->pp ?></td>
        </tr>
    <?php } ?>
</table>

Source: http://www.codeigniter.com/userguide3/overview/mvc.html

Hope this helps.

like image 112
CodeGodie Avatar answered Aug 20 '26 14:08

CodeGodie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!