Welcome Guest, Not a member yet? Register   Sign In
encoding / decoding data in models
#1

[eluser]cellulit[/eluser]
hi guys;

i'm just wondering if there's any smart way to encrypt or decrypt results sets provided by db->get or get_where functions. i try to stick to the model and do this basic data manipulation there (instead of decrypting data in view for example).

so far, i've tried a couple of things including:
Code:
array_walk($results,'$this->encrypt->decode');
... but none of my attempts are successful.
any hints?

thanks in advance!
a
#2

[eluser]TheFuzzy0ne[/eluser]
Please show a bit more of your code.

If you get the result array, you'll probably need to iterate through the array, and change the appropriate values.

Code:
$res = $this->db->get('some_table')->result_array();

foreach ($res as &$row)
{
    $row['some_field'] = $this->encrypt->decode($row['some_field']);
}
#3

[eluser]cellulit[/eluser]
hello;

thanks a lot for the reply. i was looking for a way to inject a transformation / function before the final result set is generated, but I believe the way you showed me would work fine.
the only think is I don't know how to access the elements of the modified result set now. the standard method wouldn't work I guess. see the message below.

a part of the model:
Code:
// get all user's data
    function getAll()
    {
        $results=$this->db->get_where('users',array('id'=>$this->session->userdata('id')))->result_array();
        foreach ($results as $row)
        {
            $row['email'] = $this->encrypt->decode($row['email']);
        }
        return $results;
    }

the controller:
Code:
// a form used to edit user's data
    function regFormEdit()
    {
        $data['results']=$this->users_model->getAll();
        $this->load->view('users_edit_view',$data);
    }

and the view:
Code:
<?php $row=$results->row();?>
Email: <input type="text" name="email" value="<?=$row->email?>"/>

All that gives me:
Fatal error: Call to a member function row() on a non-object in users_edit_view.php on line 9


Thanks again.
#4

[eluser]TheFuzzy0ne[/eluser]
In my projects, I have the model pass back the results as an array, I assumed you would have done the same? As far as I know, there's no way to actually edit the resource that's returned from the database, so that's the only way to do it. I think you have two options. You can either have your model pass back the data you want in the format you want, or have it pass back the result object, and format it appropriately from within your controller method before passing it in to the view.

Please note the ampersand in my example. Without it, you won't be modifying the actual array like you need to.
#5

[eluser]cellulit[/eluser]
I'll take it further from here. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB