Welcome Guest, Not a member yet? Register   Sign In
Where to format data query results
#7

[eluser]gfraser99[/eluser]
Actually..what I did to was make MY_Model extend CI_Model and store it in the application/core folder. Then all my other models extend off MY_Model, such as User_Model.

This enables me to create a parent method in My_Model like:
primary_table is set as a property in the child model class.
Code:
/**
* Return an array of qualified record arrays by key value pair
* @param string $key The key you want to index the return recordset by (eg..id)
* @param string $value The value you want to grab from the recordset to set as the value in the return array
* @return array Array of qualified record arrays or empty array if no units were retrieved
*/
function get_list($key, $value, $table=NULL){    
    
    if(is_null($table)){
        $table = $this->primary_table
    }
    $query = $this->db
             ->get($table);
    
    
   if($query->num_rows > 0){
      foreach($query->result_array() as $row){
        $arrReturn[$row[$key]] = $row[$value];
     }
     return $arrReturn;
   }
   return array();
}
If I need a list of ids and names I would simply call:
Code:
$this->load->model('model_name');
$arrNames = $this->model_name->get_list('id', 'name');
This is accessible to all my child models and I don't have to keep writing that function which I use in almost all my models. Shapes the data in a return ready to drop into a CodeIgniter Select list.


Messages In This Thread
Where to format data query results - by El Forum - 08-21-2012, 10:40 AM
Where to format data query results - by El Forum - 08-21-2012, 10:54 AM
Where to format data query results - by El Forum - 08-21-2012, 11:00 AM
Where to format data query results - by El Forum - 08-21-2012, 11:25 AM
Where to format data query results - by El Forum - 08-21-2012, 02:52 PM
Where to format data query results - by El Forum - 08-22-2012, 01:01 AM
Where to format data query results - by El Forum - 08-22-2012, 04:22 AM
Where to format data query results - by El Forum - 09-17-2012, 06:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB