将旧的过程PHP函数转换为Codeigniter中的模型 - 视图 - 控制器(MVC)
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
我是Model-View-Controller的新手,我开始在Codeigniter中编码。 我基本上将我的项目转换为MVC,但是,我遇到了这个函数(下面)我想分成MVC。 我有100个这样的函数,如果我得到最好的方法,我将能够将我的其余函数转换为MVC。 p>
这个函数包含PHP,Mysql, 和HTML一体化。 就像我们单独拆分查询和HTML一样,我也想使用Codeingiter框架来做。 即使你不能使用codeigniter默认函数回答,只需告诉我如何拆分。 p>
这里是 : p>
$ 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']。'&lt; br /&gt;';
$ split_up_query = mysql_query(“SELECT issue_id,actualHoursPerDay FROM day_projections WHERE date ='$ today'AND emp ='”。$ 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'&lt; tr&gt;&lt; td&gt;' 。 $ r ['emp_name']。 '&LT; / TD&GT;&LT; TD&GT;' 。 $ cap。 '%&LT; / TD&GT;&LT; TD&GT;' 。 $ r ['ss']。 'hrs&lt; / td&gt;&lt; td&gt;' 。 $ pp。 '&lt; / td&gt;&lt; / tr&gt;';
unset($ maes_array);
unset($ kk);
}
code> pre>
谢谢 p>
div>
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.
I am assuming you know Obect oriented programming ,php 5.5+ and Codeigniter v2+. The code(below) shows how to implement the MVC in codeigniter. Under codeigniter framework there are many folders like models,views,controllers ,config etc.put these(below code) as mentioned.
Model(database related stuff/queries etc) Example_model.php. Put this file under 'application/models' folder
class Example_model extends CI_Model{
function __construct() {
parent::__construct();
}
public function getData($today=""){
$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";
$result=$this->db->query($query);
if ($result->num_rows > 0) {
foreach ($result->result() as $row) {
$data[] = $row;
}
return $data;
} else {
return FALSE;
}
}
}
NOTE: you must autoload 'database' library in autoload.php in 'application/config' folder in your project directory or load it in constructor above like this.
$this->load->library('database);
Controller(Helps to control the user requests/data flow/routing etc )Example.php ,put this file under "application/controllers".
class Example extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Example_model');
}
public function index(){
$today=now();
$result=$this->Example_model->getData($today);
$this->load->view('index_view',['result'=>$result]);
}
}
View(Presentation/ user visible pages etc) index_view.php,put this file under "application/views".
<body>
<table>
<tr>1</tr>
<tr>2</tr>
<?php foreach($result as $r): ?>
<td><?= $r->emp_name ?></td>
<?php endforeach; ?>
</table>
</body>
NOTE: For more abstract info. http://www.codeigniter.com/userguide2/overview/at_a_glance.html