Welcome Guest, Not a member yet? Register   Sign In
Nested Query Question (NEWBIE)
#8

[eluser]sl3dg3hamm3r[/eluser]
Ok, here comes some really constructed example. Let's say you have a table 'Country' with two columns 'Id' and 'Code' (aka country-code). This example is now little bit overcomplicated, but I think I would do something similar in your case:

Code:
<?php
/*
* Created on 14.01.2010
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/



class Mdl_country extends Model implements Iterator  {

    
    private $query = null;    // Sql-query result array
    private $position = 0;    // Pointer for Iterator
    
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * Loads data into var
     */
    public function loadData()
    {
        $this->query = $this->db->get('Country');
        $this->query = $this->query->result_array();
    }
    
    
    /**
     * HERE IS DEFINED SOME SPECIAL BUSINESS-LOGIC: IF THE CODE IS 'CH', EMPHASIZE IT!
     */
    public function getCode()
    {
        $field = $this->query[$this->position]['Code'];
        if ($field == 'CH')
            return "CH ROCKS!";
        return $field;
    }
    
    
    // Iterator-Interface
    function rewind() {
        $this->position = 0;
    }
    // Iterator-Interface
    function current() {
        return $this->query[$this->position];
    }
    // Iterator-Interface
    function key() {
        return $this->query[$this->position];
    }
    // Iterator-Interface
    function next() {
        ++$this->position;
    }
    // Iterator-Interface
    function valid() {
        if ($this->query != null && isset($this->query[$this->position]))
            return true;
        return false;
    }

        
}

In the controller (this should be done in a view, but for the sake of having it simple:

Code:
function index()
    {
        
        $this->load->model('mdl_country', '', true);
        $this->mdl_country->loadData();
        
        foreach($this->mdl_country as $row)
            echo $row['Id'] . ": " . $this->mdl_country->getCode() . '<br />';
    }

As you can see, in order to print the country-code, I ask a specialised method 'getCode()'. In there I check what is written in the field and slightly modify it if a given rule matches.
In your case this method could be called 'getPrice()', where you could implement your own logic (if field is null, take another one etc.).
The advantage: Data and behaviour is kept together within one class. The view won't need to know any logic. You would just call the method(s) of your class, the rest is done there.


Messages In This Thread
Nested Query Question (NEWBIE) - by El Forum - 01-13-2010, 03:49 PM
Nested Query Question (NEWBIE) - by El Forum - 01-13-2010, 04:38 PM
Nested Query Question (NEWBIE) - by El Forum - 01-13-2010, 04:55 PM
Nested Query Question (NEWBIE) - by El Forum - 01-13-2010, 05:26 PM
Nested Query Question (NEWBIE) - by El Forum - 01-13-2010, 11:05 PM
Nested Query Question (NEWBIE) - by El Forum - 01-14-2010, 03:35 AM
Nested Query Question (NEWBIE) - by El Forum - 01-14-2010, 07:12 AM
Nested Query Question (NEWBIE) - by El Forum - 01-14-2010, 08:42 AM
Nested Query Question (NEWBIE) - by El Forum - 01-14-2010, 12:08 PM
Nested Query Question (NEWBIE) - by El Forum - 01-14-2010, 03:08 PM
Nested Query Question (NEWBIE) - by El Forum - 01-15-2010, 03:15 AM
Nested Query Question (NEWBIE) - by El Forum - 01-15-2010, 07:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB