Welcome Guest, Not a member yet? Register   Sign In
where & how to modify db results
#1

[eluser]brainer[/eluser]
Hi there,

I'm a bit of a newbie, who's trying to convert my entire procedural php website to a codeigniter based site.

But I've hit a bit of a problem early on, I'm sure its really simple enough, but im just not that used to working with an mvc pattern.

The thing is, im not sure where or how to modify my db results before i send it on to the view. At the moment this is what im doing...

The model

Code:
class Home_model extends Model
{
    function getLatestProjects()
    {
        $query = "SELECT DISTINCT projects.name, projects.projectnameid, dudes.usernameid, dudes.username, folders.foldernameid, projects.url
                    FROM projects
                        INNER JOIN dudes
                            ON projects.userid = dudes.userid
                        INNER JOIN folders
                            ON projects.folderid = folders.folderid
                        INNER JOIN pieces
                            ON projects.projectid = pieces.projectid WHERE projects.published = 1 ORDER BY projects.date DESC LIMIT 40";
        $q =  $this->db->query($query);
        if ($q->num_rows() > 0){
            foreach ($q->result() as $row)
            {
                $data[] = $row;
            }
            
            return $data;    
        }
    }
}

the controller

Code:
class Home extends Controller
{
    
    function Home()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->model('home_model');
        $data['records'] = $this->home_model->getLatestProjects();
        $this->load->view('home_view', $data);
    }    
}

the view

Code:
<?php foreach ($records as $row) :?>
                        <div class="box" title="&lt;?=$row->name?&gt; by &lt;?=$row->username?&gt;">
                            <a >usernameid?&gt;/&lt;?=$row->foldernameid?&gt;/&lt;?=$row->projectnameid?&gt;">
                                <img >url?&gt;_thumb_s.jpg" alt="&lt;?=$row->name?&gt; by &lt;?=$row->username?&gt;">
                            </a>
                        </div>
                        &lt;?php endforeach; ?&gt;

The issue is, the "$row->url" needs to have the extension taken off, which i used to do during the loop in my old procedural script, but here, i dont know how to do it with my current structure.

...sorry for the massive post
#2

[eluser]brainer[/eluser]
sorry, this line in the view didnt display correctly...

<img >url?&gt;_thumb_s.jpg" alt="&lt;?=$row->name?&gt; by &lt;?=$row->username?&gt;">

neither did that apparently, but its basically my a img tag with an src which loads in the start of the filename dynamically and the end will always be "_thumb_s.jpg".

am i making any sense?
#3

[eluser]Zack Kitzmiller[/eluser]
Why not just use regular expressions to remove an unneeded part of a string?
#4

[eluser]brainer[/eluser]
yeah, thats what i want to do, but i dont know how and where to do this and how to store the result. this is probably a stupid question, sorry. im only doing php for 5 months now.
#5

[eluser]jedd[/eluser]
Hi linasaur,

You might be better served with result_array() rather than getting each row as you're doing now in your model.

As to the question - it's possibly a matter of taste, as your model should handle all your data abstraction requirements, your controller should handle your 'business' logic, and your view should be fairly dumb (but is allowed to do simple 'display logic'). Of course you can do whatever the heck you like, but this is the general or ideal approach.

Having said that, I would probably be inclined to have my model return an array of actual filenames - appending the relevant thumb_s.jpg(?) suffixes to the filenames you're picking out of the DB. I might split that up into a different function (possibly private) in the model. It means if you change the way your data is stored or named, the view and controller never need to know about it. (The same could be said of handling it all in your view, or your controller, and saving the other two places from data changes .. but remember, the model is meant to abstract your data.)
#6

[eluser]brainer[/eluser]
Okay, i got it. Thanks for your help, hopefully this will help someone else...

I've changed the controller like the following. The url row is now being modified in the controller with the use of a private function:

Code:
class Home extends Controller
{
    
    function Home()
    {
        parent::Controller();    
    }
    
    function _removeExtension($strName)
    {
    $ext = strrchr($strName, '.');
    
    if($ext !== false)
    {
    $strName = substr($strName, 0, -strlen($ext));
    }
    return $strName;
    }
    
    function index()
    {
        $this->load->model('home_model');
        $data['records'] = $this->home_model->getLatestProjects();
        foreach ($data['records'] as $row){
            $row->url = $this->_removeExtension($row->url);
        }
        
        $this->load->view('home_view', $data);

    }
}




Theme © iAndrew 2016 - Forum software by © MyBB