• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
encoding / decoding data in models

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:
... but none of my attempts are successful.
any hints?

thanks in advance!

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.

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

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


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:
// get all user's data
    function getAll()
        foreach ($results as $row)
            $row['email'] = $this->encrypt->decode($row['email']);
        return $results;

the controller:
// a form used to edit user's data
    function regFormEdit()

and the view:
<?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.

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.

I'll take it further from here. Thanks!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.