CodeIgniter Forums

Full Version: Opinion on best coding practices
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I'm still relatively new to CI so this is more of a "what would you do" question.

I'm migrating this system to CI and rewriting some pages, and one in particular in a page with a large calendar, and then along the right side are several ToDo lists for various departments. (This page is a manager overview of tasks each dept is working on.) Not all ToDo lists would show (only if there were items, otherwise they would not display so we didn't have blank ToDo lists.

The old page is a single PHP page that accessed the database and simply displayed a table, such as:

Code:
$lines = $db->query("select * from tasklist where (Active=1 or Priority=1)");
$acol=0;
while ($line = $db -> fetch_array ($lines))
{
    if ($acol++==0)
    {
        echo "<table class='calendarsidebargreen'>";
        echo "<tr><th colspan=3>DEPT XYZ LIST</th>";
        echo "</tr>";
    }
    echo "<tr class='calendarsidebar'><td><a href='/task.php?do=edit&i=".$line['>".$line['title']."</a>&nbsp;&nbsp;";
    if ($line['Active']==1) echo "(A)&nbsp;";
    if ($line['Priority']==1) echo "(P)&nbsp;";
                
    echo "</tr>";
                
}    
if ($acol>0) echo "</table><P>";

So if we have lines from this department, then display the header, the items, and close the table. We have several such tables.

Now the question is -- what's the best practice in the CI environment.

My first instinct is to just move this code into a model, change the echo statements to a bunch of statements like $html .= where the model function is just building the html, then passing it back to the view and the only thing in the view is a simple echo $html statement. (writing to a variable as it loops through, so the end variable is the HTML display).

But that doesn't seem very "MVC" like because I'm not separating the "view" from the "database". Another option I guess is to move the database read to a model and load up an array with the information, then pass that array to the view and have the view loop through the array. But in that case, I'm simply trading the loop through the table with a loop through an array, which doesn't seem like I'm gaining a whole lot -- plus I'm having to build the array which means I'm looping through all the data TWICE now.

Am I overthinking this? Any recommendations? (Note some of this code didn't format really well in this message, but mostly it came through.)

El Forum

[eluser]nelson.wells[/eluser]
[quote author="ktwbc" date="1278738456"]
But that doesn't seem very "MVC" like because I'm not separating the "view" from the "database". Another option I guess is to move the database read to a model and load up an array with the information, then pass that array to the view and have the view loop through the array. But in that case, I'm simply trading the loop through the table with a loop through an array, which doesn't seem like I'm gaining a whole lot -- plus I'm having to build the array which means I'm looping through all the data TWICE now.
[/quote]

Why populate an array to pass to the view? Why not just pass the result object retrieved from the model?

In the model:
Code:
function get_data()
{
    $where = array(
        "Active" => "1",
        "Priority" => "1"
    );

    $this->db->where($where);
    return $this->db->get("tasklist");
}

Then in the controller
Code:
$data['result'] = $this->Your_model->get_data();
$this->load->view("my_view.php", $data);

Then in the view:
Code:
foreach($result->result() as $row)
{
    //process each row
}

This is MVC, and there is only one loop.

El Forum

[eluser]stuffradio[/eluser]
Have a model with

Code:
$lines = $this->db->query("select * from tasklist where (Active=1 or Priority=1)");
return $lines->result();

- pass the array in your controller from the model to the view
- do a foreach loop and echo the table

Edit: Exactly what he said is what I did here.