php - codeigniter where to process sql result? -
class selectedmodel extends ci_model { var $title = 'selectedmodel'; var $content = 'get top n articles'; var $date = '23.2.2011'; function __construct() { parent::__construct(); } function gettoparticles() { $result = $this->db->query('select top 5 article articles;'); if( ! $result->num_rows() > 0 ) die('there no articles in db.'); return $result; } } class front extends ci_controller { function __construct() { parent::__construct(); } function index() { $this->load->database(); $this->load->helper(array('text', 'html')); $this->load->model('selectedmodel'); // controller process data , validate $top_articles = $this->selectedmodel->gettoparticles(); foreach($top_articles->result() $item) { $item->desc = character_limiter($item->desc, 75); if( strlen($item->image) == 0 ) $item->desc = '/images/default.png'; } $data['title'] = 'title'; $data['random_articles'] = $top_articles; $this->load->view('front', $data); } }
front view:
php foreach($random_articles->result() $item): php echo $item->desc php echo br() . $item->image php endforeach;
i'm wondering if knowledge of mvc correct. in controller process data, prepare them show them in view. in view there room html/css code , echo $var.. model function data.
are there other approaches process data. way ok?
optimization issue: $top_articles = $this->selectedmodel->gettoparticles(); don't know how php manages line. i'm asking if $top_articles copy of gettoparticles, use twice many memory if use in view:
front view:
php foreach($random_articles->result() $item): php echo character_limiter($item->desc, 75) php echo br() . if( strlen($item->image) == 0 ) echo images/default.png''; else echo $item->image; php endforeach;
but approach don't use mvc(using character_limit, .... in view).
i think got idea down. mvc approach doesn't mean better optimized.
generating data in model, passing controller, , view correct way this, though seem though view many lines of code.
Comments
Post a Comment