开发者

decode mysql query before returning it to view

I'm running a query to mysql that returns encrypted data. I'd like, if possible, to decode the results before sending it to the view. It seems like better form to handle the decoding in the controller (or even the model) rather than inside the view.

I can't seem to wrap my head around how to do it, though.

I was thinking I could iterate through the object, decodode it, and push it to another array that would be sent to the view. Problem with this is I won't know (and need to keep) the indexes of the query.

So the query might return something like:

[id] => 742 
[client_id] => 000105 
[last] => dNXcw6mQPaGQ4rXfgIGJMq1pZ1dYAim0 
[first] => dDF7VoO37qdtYoYKfp1ena5mjBXXU0K3dDlcq1ssSvCgpOx75y0A== 
[middle] =>iXy6OWa48kCamViDZFv++K6okIkalC0am3OMPcBwK8sA==
[phone] => eRY3zBhAw2H8tKE

Any ideas?


Ended up with:

function name(){
    $data['e_key']=$this->e_key;
    $clid = $this->uri->segment(3);
    $name = $this->Clients_model->getNameData('*','client_id='.$clid,'');
    $nameArray= array();
    foreach ($name->result() as $row){
        $x = $row;
        $keys = array('id','client_id');
        $unenc = array();
        foreach ($x as $key=>$value){
            if(! in_array($key, $keys)){
                $unenc[$key]=$this->encrypt->decode($value,$this->e_key);
            }else{
                开发者_高级运维$unenc[$key]=$value;
            }
        }
        array_push($nameArray,$unenc);
     }
    $data['name'] = $nameArray;
    $this->load->view('names/name_view',$data);

}


Assuming you know how to decrypt the data, it's but a matter of iterating over the object, decrypting the encrypted fields.

If $YOUR_OBJECT is your object and your function for decryption is decode() then the following code should do the trick.

// The keys corresponding to the encrypted fields
$encoded = array('last', 'first', 'middle', 'phone');

$decoded = array();
foreach($YOUR_OBJECT as $key => $value)
{
    if (in_array($key, $encoded))
    {
        $decoded[$key] = decode($value);
    }
}


if it's a particular index, you could decode it like

$result['last'] = base64_decode($result['last']);

or in the model, use mutators and accessors:

 public function setUp() {
       $this->setTableName('tablename');
        $this->actAs('Timestampable');
        $this->hasMutator('last', '_encode64');
        $this->hasAccessor('last', '_decode64');
   }

 protected function _encode($value) {
     $this->_set('last',base64_encode($value));
 }
protected function _decode($value) { 
     return base64_decode($value); // not sure on this one - might have to
    //  return  $this->set('last', base64_decode($value));
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜