Welcome Guest, Not a member yet? Register   Sign In
RESOLVED: Correct way to manipulate data?
#1

[eluser]meridimus[/eluser]
Hi there,

I'm working on a personal project, learning the codeigniter framework for future projects. I'm using a traditional MVC approach to my application design.

I'm having a slight difficulty working out where to manipulate data correctly when calling from a database, for example:

* My model is calling a news post

* That data gets passed to my controller

* Which then gets injected to my view where I loop the result -

Code:
<?php foreach($posts->result() as $post): ?>
<!-- HTML -->
<?php endforeach; ?>

If I wanted to manipulate for example, the date of every news post to be "1 day ago" instead of the standard mysql date formatting, where should I do the manipulation? I know I could use the date helper to convert to mysql_to_unix(), then the timespan() but I'd rather not do this manipulation in the view (because this is just an example, I'd like to do more manipulation than this, most likely in the controller).

Because I'm currently passing information into the view, is there a way I can intercept this in the controller without adding too much overhead? What's the best method?
#2

[eluser]xwero[/eluser]
I think the most intuitive way would be to create a view specific model that extends the model which outputs the raw data. This doesn't blow up the controller with data manipulations but it adds another layer.
#3

[eluser]meridimus[/eluser]
Do you think I should output database data as an array inside the model and manipulate it inside the controller, passing the modified array to the view to be looped through?
#4

[eluser]xwero[/eluser]
If your server runs php5 you don't have to convert the object to an array. You can iterate it like an array, manipulating the data in the iteration, and use the object in the view file.

You can do the data manipulations in the controller but i found out it blows up controllers code pretty fast.

Bottom line there is no bad way. You can keep it in the view and make functions to manipulate the data with one function to not overload the view with code.
Do the manipulation in the controller. You could build a manipulation library to prevent the controller code from blowing up.
You could do it in a model that has methods for specific views. You can do some of the manipulations in sql this way you don't have to use php functions.
#5

[eluser]meridimus[/eluser]
I think what I will do is create a helper, then run a function on the data inside the view to convert the data, minimising code in the controller and the view but also creating a re-usable helper file.
#6

[eluser]xwero[/eluser]
That is a good idea and if you post the helper it would even be a better idea Wink
#7

[eluser]meridimus[/eluser]
Awesome! We worked something out!
#8

[eluser]meridimus[/eluser]
Hope this helps people make really neat use of the MVC architecture by pushing some post-database code into a helper to clean up your views. Let me know your thoughts!

Model - blog.php

Code:
<?php
class Blog extends Model {

    function Blog()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function get($amount)
    {
    $this->db->select("title, body, date");
    $this->db->from("posts");
    $this->db->order_by("date");
    $this->db->limit($amount);
        
    return $this->db->get();
    }
}
?>

Controller - blog.php

Code:
<?php
class Blog extends Controller {

    function Blog()
    {
        parent::Controller();
    }
    
    function index()
    {
        // Load "Date Formatter" helpers
        $this->load->helper("date");

        // Load "Blog" model, reference with lowercase "blog"
        $this->load->model("Blog","blog");

        // Fetch data from the "Blog" model
        $data["posts"] = $this->blog->get(10);

        // Load "Latest Posts" view
        $this->load->view("blog/posts",$data);
    }
?>

View - posts.php

Code:
<html>
    <head>
        <title>Demo blog</title>
    </head>
    <body>
        <h1>Blog Title</h1>
        &lt;?php if($posts->num_rows() > 0): ?&gt;
        &lt;?php foreach($posts->reesult() as $post): ?&gt;
            <h2>&lt;?=$post->title?&gt;</h2>
            <h3>&lt;?=format_date($post->date)?&gt;</h3>
            &lt;?=$post->body?&gt;
        &lt;?php endforeach; ?&gt;
        &lt;? else: ?&gt;
            <p>No posts exist</p>
        &lt;? endif; ?&gt;
    &lt;/body&gt;
&lt;/html&gt;


Helper Extension - MY_date_helper.php

Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

function format_date($str)
{
    $date = timespan(mysql_to_unix($str));
    
    $date = str_replace(strstr($date,","),"",$date) . " ago";
    
    return $date;
}
?&gt;
#9

[eluser]xwero[/eluser]
One remark: From CI 1.6 you are able to extend the helpers which means you only have to load the date helper and you extension will be added automagically. In order to make this happen you just have to name your file MY_date_helper.php.
#10

[eluser]meridimus[/eluser]
Updated my code to show that, pretty awesome.




Theme © iAndrew 2016 - Forum software by © MyBB