Welcome Guest, Not a member yet? Register   Sign In
turn array into object active record can use
#4

[eluser]mddd[/eluser]
I think the root of the problem is that you start with a 'result object' from CodeIgniter ($name). This object has methods like 'result()' and 'num_rows()'. Then you decode it. In the process of decoding, you make an array ($nameArray). That array does not have those methods. You can't do $nameArray->result(). That's what you have to change the view.

If you want to keep everything the same, you should decode the data in the object itself. You could extend the database class to add a 'decode()' method to the result object. Or you can do it a bit 'dirtier', by operating on the object. This is 'dirty' because if the CI DB class changes and you upgrade your CI installation, it might stop working. But as long as that's not the case it will work.

For reference, here's the result_object method that is used by CI to get the results you want.
Code:
// this code is in /system/database/DB_result.php

    function result_object()
    {
        if (count($this->result_object) > 0)
        {
            return $this->result_object;
        }
        
        // In the event that query caching is on the result_id variable
        // will return FALSE since there isn't a valid SQL resource so
        // we'll simply return an empty array.
        if ($this->result_id === FALSE OR $this->num_rows() == 0)
        {
            return array();
        }

        $this->_data_seek(0);
        while ($row = $this->_fetch_object())
        {
            $this->result_object[] = $row;
        }
        
        return $this->result_object;
    }

As you can see, CI assembles the object and returns it. But if it's already assembled, it just returns it right away. So what you can do is call the result() method yourself beforehand and then decode the information in the object. You then pass it to the view, and the view calls the result() method, it gets the information already decoded by you.

Code:
// controller code

// do the query
$name = $this->Clients_model->getNameData('*','client_id='.$clid,'');

// first call the result
$dummy = $name->result();

// now decode the information
$ignore_keys = array('id', 'client_id');
foreach ($name->result_object as &$row) {  // watch! we're getting the row as a reference so we're really changing it; not working on a copy
     foreach (get_object_vars($row) as $key=>$value){
        if(!in_array($key, $ignore_keys)){
            $row->$key = $this->encrypt->decode($value,$this->e_key);
        }
    }
}

// show it; in the view, $name->result() will get the decoded results.
$data = array('name'=>$name);
$this->load->view('names/name_view',$data);


Messages In This Thread
turn array into object active record can use - by El Forum - 06-09-2010, 09:29 AM
turn array into object active record can use - by El Forum - 06-09-2010, 09:42 AM
turn array into object active record can use - by El Forum - 06-09-2010, 10:10 AM
turn array into object active record can use - by El Forum - 06-09-2010, 10:28 AM
turn array into object active record can use - by El Forum - 06-09-2010, 10:52 AM
turn array into object active record can use - by El Forum - 06-09-2010, 11:02 AM
turn array into object active record can use - by El Forum - 06-09-2010, 11:37 AM
turn array into object active record can use - by El Forum - 06-09-2010, 11:50 AM
turn array into object active record can use - by El Forum - 06-09-2010, 12:54 PM
turn array into object active record can use - by El Forum - 06-09-2010, 02:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB