CodeIgniter Forums
Separation of logic versus content inquiry - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Separation of logic versus content inquiry (/showthread.php?tid=2655)



Separation of logic versus content inquiry - El Forum - 08-16-2007

[eluser]stevefink[/eluser]
So just wanted to hear out from the experts how they'd normally go about their everyday life with CI/MVC in general for the following.

I'm populating an initial select box upon load based on information from a database. The actual query I'm putting into an (optional) model, and handing the results back to the controller.

My question is, where would most of you build your HTML string together? Would you dump it into the Model or Controller? Perhaps even the view using a simple foreach construct?

It might sound silly, I just wish to follow 'best practices' and see how the experts do it.

Thanks all!


Separation of logic versus content inquiry - El Forum - 08-16-2007

[eluser]Michael Wales[/eluser]
I would do as you said - query in the model, logic in the controller. I would then set a variable to TRUE/FALSE based on whether it was checked or not. Then in my view, I would echo 'checked' if that variable was TRUE, nothing if it was not - thus, checking the box appropriately.

Another way would be to set that variable to ' checked' or '' and echo that out in your view - but that's lacks the beauty of the TRUE/FALSE to me and is less extensible in the future.


Separation of logic versus content inquiry - El Forum - 08-16-2007

[eluser]stevefink[/eluser]
Well currently I have it setup like this. Very simple example:

Function in my model:

Code:
function get_makes()
    {
        // return all makes.
        $query = $this->db->get('makes');
        return $query;
    }

my controller:

Code:
function carinfo()
    {
        // We'll use the Autodb_model for all of our database interaction
        // and name it autodb for ease of use.
        $this->load->model('Autodb_model', 'autodb');
        
        // Pull the available makes out of the db and populate
        // the respective select parameter.
        $data['makes'] = $this->autodb->get_makes();
        
        // drop knowledge into the view.
        $this->load->view('console/car_info_view', $data);
    }

And finally, my view:

Code:
<label>Make      <span class="required"> * </span></label>
    <select name="make" id="make">
    
        <option>**********</option>
        <option value="add_make">Add Make</option>
        &lt;?php foreach($makes->result() as $make): ?&gt;
        <option value="&lt;?= strtolower($make->name) ?&gt;">  &lt;?= $make->name; ?&gt;  </option>
        &lt;?php endforeach; ?&gt;
        
    </select> <br>

Again it works great, I just like to learn how everyone else does it to keep improving my scalability/architecture type skills. Heck, I'm almost willing to put off all the questions so I can just finish the project, then pay an expert to do nothing but critique my working code just so I can do it more efficiently in the future. :-)

- sf


Separation of logic versus content inquiry - El Forum - 08-16-2007

[eluser]Michael Wales[/eluser]
Ah yeah - that's how I would do that as well. I thought you were referring to dynamically checking the box (whether the database reflects it as checked or not).

I think you are really getting the hang of CI - it will be fun to see your final product.