I am using Codeigniter and I want to encode my string using encode() function in Mysql. Is there a way to use that function using $this->db->insert($table_name, $data); in Codeigniter? Here is my sample codes:
CI controller:
$data_array('id' => 1, 'name' => 'Juan Dela Cruz');
$this->load->model('data_mdl');
$this->data_mdl->_insert($data_array);
CI model:
public function _insert($data) {
return $this->db->insert($table_name, $data);
}
Any help would be appreciated. Thanks.
I solve my problem by using $this->db->set() function in my CI model.
public function _insert($data) {
$this->db->set('name', 'ENCODE(\'' . $data['name'] . '\', \'mysecretkey\')', false);
unset($data['name']);
return $this->db->insert($table_name, $data);
}
I don't know if this is the right way but it solved my problem.
Why don't you simple encode your data then send it.
$this->db->insert($table_name, encode($data));
or
$data = array();
$data = encode($data);
$this->db->insert($table_name, $data);
or
$data = array();
$data = 'ENCODE('. $data .')';
$this->db->insert($table_name, $data);
EDITED FROM HERE:
The MYSQL function Encode() just as the name suggests encodes the data, you then unencode that data using the reverse function.
My suggestion would be to use something simple as base64_encode which works with the same principle with a reverse function, base64_decode.
In that case you would use this following code: SOLUTION 1
CI controller:
// ENCODING
$data_array = array('id' => 1, 'name' => 'Juan Dela Cruz');
$serialized = serialize($data_array);
$encoded = base64_encode($serialized);
$this->load->model('data_mdl');
$this->data_mdl->_insert($encoded);
// DECODING
$encoded = //DO QUERY HERE TO GET ENCODED DATA;
$decoded = base64_decode($encoded);
$unserialized = unserialize($decoded);
CI model:
public function _insert($data) {
return $this->db->insert($table_name, $data);
}
You can also use CI inbuilt encrypt library: SOLUTION 2
More details can be found here: https://www.formget.com/codeigniter-encrypt/
Related
I have a question, I want to change string data from database using explode, because the data is imploded before when I post it to the database. Here's my controller
public function add_detail()
{
$symptoms_id = implode(',', $this->input->post('symptoms_id'));
$CF_user = implode(',', $this->input->post('CF_user'));
$data = array(
'symptoms_id' => $symptoms_id,
'CF_user' => $CF_user
);
$this->db->insert('consultation_detail', $data);
redirect('User/consultation_result', $data);
}
That function is to implode and it works, but I want to make a function in controller too for explode the data.
I just try like this
public function consultation_result()
{
$data['consultation_detail'] = $this->db->get('consultation_detail')->row();
$CF_user = explode(',', $CF_user);
}
But it said undefined $CF_user , I'm sorry because I'm newbie :(
try this
public function consultation_result($data)
{
return explode(',', $data);
}
and use like this
$data = array(
'symptoms_id' => consultation_result($this->input->post('symptoms_id')),
'CF_user' => consultation_result($this->input->post('CF_user'))
);
controller code
if($this->input->post('update'))
{
$n=$this->input->post('pps_pin');
$e=$this->input->post('pps_address');
$m=$this->input->post('stm_shipping_type');
$a=$this->input->post('tsm_time_slot');
$b=$this->input->post('pps_price');
$id=$this->input->post('pps_slid');
$this->Config_model->updaterecords($n,$e,$m,$id,$a,$b);
redirect('Admin_ctrl/config/view_pincode_price');
}
}
model code
function updaterecords($n,$e,$m,$id,$a,$b)
{
$id;
$qr=$this->db->query("UPDATE pincode_price_setup,shipping_type_mst,time_slot_mst SET pps_pin='$n',pps_address='$e',stm_shipping_type='$m',tsm_time_slot='$a',pps_price='$b' WHERE pps_slid='$id'");
return $qr;
}
Delivery Method and Delivery Time Slot data are same
You should modify your code to use codeigniters prepared statements. If I correctly understood relations in your DB, your update records method should looks something like this:
function updaterecords($n, $e, $m, $id, $a, $b)
{
$this->db->set('pps_pin', $n);
$this->db->set('pps_address', $e);
$this->db->set('stm_shipping_type', $m);
$this->db->set('tsm_time_slot', $a);
$this->db->set('pps_price', $b);
$this->db->join('shipping_type_mst', 'pincode_price_setup.pps_shipping_type = shipping_type_mst.stm_sqlid');
$this->db->join('time_slot_mst', 'pincode_price_setup.pps_time_slot = time_slot_mst.tsm_sqlid');
$this->db->where('pps_slid', $id);
$this->db->update('pincode_price_setup');
}
Friendly tip: give your variables some more meaningful names, it would be much easier to work with variable named $pin than $n.
Please try like this
//Controller Code
function update_record($id){
$update_array = array(
'pps_pin' => $this->input->post('pps_pin') ///add fields in array which updated
);
$this->modal_name->update($id,update_array);//pass id & array which want to be update
}
//Modal code
function update($id,$array){
return $this->db->where('id',$id)->update('TABLE_NAME',array);
}
Try This code
function updaterecords($n, $e, $m, $id, $a, $b) {
$this->db->set('pps_pin', $n);
$this->db->set('pps_address', $e);
$this->db->set('stm_shipping_type', $m);
$this->db->set('tsm_time_slot', $a);
$this->db->set('pps_price', $b);
$this->db->where('pps_slid', $id);
$this->db->update('pincode_price_setup');
return ($this->db->affected_rows() != 1) ? false : true;
}
Once codeigniter model is loaded then its default update function works
if($this->input->post('update'))
{
$data = [
'pps_pin' => $this->input->post('pps_pin'),
'pps_address' => $this->input->post('pps_address'),
'stm_shipping_type' => $this->input->post('stm_shipping_type'),
'tsm_time_slot' => $this->input->post('tsm_time_slot'),
'pps_price' => $this->input->post('pps_price'),
'pps_slid' => $this->input->post('pps_slid'),
];
$this->Config_model->id = $id;
$this->Config_model->update($data);
redirect('Admin_ctrl/config/view_pincode_price');
}
}
So I am sure we don't need to define a separate function in model for same purpose.
So as I understand it, you want to update records in your database. I will not write it for you. I will show you an example.
This code may not be perfect but it works for my localhost project.
Create a function to update records in your controller.
Example:
public function update($id){
$id = $this->input->post('id');
//create $data array and add your data to array
$data = array(
'nazov_divizia' => $this->input->post('nazov')
);
//send this $data array with $id to your update function in model
$this->Divizia_model->updateData($id, $data);
//you can create flash message if you want
$this->session->set_flashdata('message', 'Údaje upravené');
//redirect to your controller index function
redirect(base_url().'divizia');
}
Create a function to update the record in your model.
Example:
public function updateData($id, $data)
{
$this->db->where('id_divizia', $id);
$this->db->update('divizia', $data);
}
I am using the RPC service of ApiGilty to return some data. I would like to double check whether or not this is the correct way of formatting and returning the data as I am not 100% sure of the correct process.
EDIT: To clarify
The data is being built from a number of entities:
main
main_extra
main_data
main_data_days
main_data_tiers
Is there a way to hit main and get all the sub entities? Currently I am building my data from scratch and returning an array.
My RPC Controller is as follows:
use My\Data\Controller\DataInterface;
use Zend\Mvc\Controller\AbstractActionController;
use ZF\ContentNegotiation\ViewModel;
class MyDataController extends AbstractActionController
{
const GENERAL_ERROR = 'api.rpc.my-data.my-data-controller';
public function __construct(
MyDataInterface $myData
)
{
$this->myData = $myData;
}
public function myDataAction()
{
$my_id = (int) $this->params()->fromRoute('my_id', 0);
if ($my_id == 0)
{
$data = $this->myData->getMyData();
} else
{
$data = $this->myData->getMyData($my_id);
}
$result = new ViewModel(array(
'data' => $data
));
return $result;
}
}
Now to create the data I am doing something like this:
public function getMyData( $my_id = null )
{
$returnArray = [];
$array1 = [
'key_1' => [1,2,3,4],
'key_2' => '123',
'key_3' => ['a','b','c']
];
$array2 = [
'key_1' => [1,2,3,4,5,6,7,8],
'key_2' => '123456',
'key_3' => ['a','b','c','d']
];
if ($my_id == 1) {
$array3 = ['some','or','other'];
} else {$array3 = []; }
$final_array = [
'data1' => $array1,
'data2' => $array2,
'data3' => $array3
];
$returnArray['data'] = $final_array;
$returnArray['success'] = 'true';
$returnArray['reason'] = '';
return $returnArray;
}
When checking with postman, I get the following:
Now since I have nothing to reference this against, my question is simply. Have I gone about this in the correct way and is this how the return code should be formatted?
Thanks!
Right now the Hal plugin is not used to render your result? You are responding a custom json object. Is this really what you want?
The response you currently return is not formatted according to HAL specifications. A proper HAL response should hold at least a _links key with a self href. It would be wrong to return this result with Content-Type headers set to application/hal+json. You should use application/json instead.
Here you can find documentation on how to respond HAL from an RPC-contoller.
I am not sure what you want to achieve but maybe you can be a bit more specific in your question so others can help out...
Doesn't look too bad, perhaps adhere to a standard such as jsend http://labs.omniti.com/labs/jsend or you could use hal-json, matthew weier o'phinney has a good blog post on this https://mwop.net/blog/2014-03-26-apigility-rpc-with-hal.html
Also you don't need to return a view model as you can just return an array and apigility will return JSON. You could also write a jsendViewModel if you go down that route.
Not exactly an answer but hope this helps you!
I have a site developed in codeigniter and I have a model where I have to create two Json but I don't know if is better to save it into my server or pass it itno the controller and after in the view but how?
This is my model simplify:
public function calledFromController(){
//code...
$this->makeRequest();
}
public function makeRequest(){
//code...
foreach ($foo as $foo2){
$values[] = $foo2->value;
}
foreach ($hello as $hello2){
$values_hello[] = $hello2->value;
}
//save it into my server
$file = fopen('foo.json','w+');
fwrite($file, $foo);
fclose($file);
$file2 = fopen('hello.json','w+');
fwrite($file2, $file2);
fclose($file2);
}
Now I save it, but can be a very lot of data and after I have to open each json and parse it into javascript (backbone).
Is better this mode or return this two json (I have to mantain separate Json)?
Is I have to return this two Json how Can I do?
From my controller I call the function calledFromController in the model.
Try by creating an array and returning the array if you want to return both the json:
$data = array();
$data['foo'] = $foo;
$data['hello'] = $hello;
return $data;
In the controller:
$return = $this->model->function();
$foo = $return['foo'];
$hello = $return['hello'];
i'm using zend_tables for my model and i'd like to create a function to convert all data from a single row (retireved like this: $row = $model->find($id)) to htmlentities before outputting it with the usual code:
echo ($row->current()->nameOfField);
I need something like this function i use on arrays:
public static function convertArrayToHtmlEntities($data){
$keys = array_keys($data);
foreach ($keys as $k) {
$d = $data[$k];
$clean = htmlentities($d, ENT_QUOTES, "UTF-8");
$data[$k] = $clean;
}
return $data;
}
Thx in advance!
If you're using Zend View you can just escape the output in your view script. See Escaping Output.
If you're not using it in a view script you can use Zend Filter.