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'))
);
Related
Hello There I need Help To Update on Vehicle Wies on Specific Dates.I'm Attaching Form image and Here is my Controller code.
public function add_vehicle_expense(Request $request)
{
$veh = array('veh_reg_num' => $request->veh_reg_num);
foreach ($veh as $data) {
print_r($request->date[$data]);
dd();
$veh = Sale_report::where('date',$request->date[$data])->where('veh_reg_num', $request->veh_reg_num[$data])->update(['diesel_qty'=> $request->qty[$data], 'diesel_rate'=> $request->rate[$data], 'diesel_amount'=> $request->total_amount[$data], 'other_expense'=> $request->other_exp[$data]]);
}
if ($veh) {
return redirect()->back()->with('Data Store Successfully');
}
//$veh = Sale_report::where('date',$request->date)->where('veh_reg_num', $request->veh_reg_num)->update(['diesel_qty'=> $request->qty, 'diesel_rate'=> $request->rate, 'diesel_amount'=> $request->total_amount, 'other_expense'=> $request->other_exp]);
/* foreach ($request->date as $reg_num => $key ) {
$s = Sale_report::where('date',$request->date[$reg_num])->where('veh_reg_num',$request->veh_reg_num[$reg_num])->first();
$s->diesel_qty = $request->qty[$reg_num];
$s->diesel_rate = $request->rate[$reg_num];
$s->diesel_amount = $request->total_amount[$reg_num];
$s->other_expense = $request->other_exp[$reg_num];
$s->update();
} */
}
I have try to Update Data by matching Each Vehicle Number with Date Some times it Show ErrorException Attempt to read property on int,ErrorException Undefined array key "data"
I have Found The Solution of this Problem.
I've use implode(',' , $request->veh_reg_num) Then I use $datas = explode() function then I use foreach() With exploded Vairable as foreach($datas as $data => $value){
match the each row that with $data array then I Update the values to data base }
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 have a database table named 'services'. With the function below I get all the records from that table. This function is located in my ServicesController.php.
public function index() {
$roadmap = Roadmap::all();
return view('services', compact('roadmap'));
}
Now in my view I also do the following:
<?php
foreach($roadmap as $roadmap_item) {
$new = array();
$splitted = explode("|", $roadmap_item->steps);
foreach($splitted as $split) {
$new = explode(":", $split);
}
}
?>
Lets say I get the string 'step1:hello|step2:bye' back from '$roadmap-item->steps'. I split them in substrings with explode etc. This is working by the way.
But is there a way I can manipulate the string in the Controller so my view will be nice and clean without many php code and still remain the variable $roadmap with all the database records.
Kind regards,
Dylan
I think you can do very similar things in your controller. Do foreach in your controller first then make a changes in every item in your collection and pass the new collection.
For example:
$roadmaps = Roadmap::all();
foreach($roadmaps as $roadmap){
$roadmap->something = " the changes you want to do as string " . $roadmap->something;
$roadmap->save();
}
return view('services', compact('roadmaps'));
or
$roadmaps = Roadmap::all();
$new = new Collection();
foreach($roadmaps as $roadmap){
$new[] = array('roadmap' => $roadmap, 'something' => $roadmap->string . " etcetc ")
$new->save();
}
return view('services', compact('new'));
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/
I am VERY new to PhP. Quick question that I am having trouble finding the answer to online, although I am sure most of you will quickly know the answer. I have the following code creating an associative array and then I am trying to display it in a table. I know there are easier ways to display it in a table like a foreach loop, but I would like to learn this method first :
class CarDealer extends Company {
var $navbar_array = array();
function create_navbar_array ( ) {
$mainurl = $this->company_url; // get the main url address of this web page
$this->navbar_array = array( "Home Page"=>"$mainurl?whichpage=home", "Sales"=>"$mainurl?whichpage=sales",
"Support" => "$mainurl?whichpage=support", "Contacts" => "$mainurl?whichpage=contact" );
}
function getLeftNavBar() {
$data ="<table border='1' style='background-color:yellow; width:35%'>";
$data .="<tr><td>$this->navbar_array['Home Page']</td></tr>";
$data .="<tr><td>$this->navbar_array['Sales']</td></tr>";
$data .="<tr><td>$this->navbar_array['Support']</td></tr>";
$data .="<tr><td>$this->navbar_array['Contacts']</td></tr>";
$data .="</table>";
return $data;
}
}
Later in my code I create an object for my class and then try to print the table. Unfortunately I am just getting an output of things like Array['Home Page'].
$carobject = new CarDealer();
$carobject->create_navbar_array();
print $carobject->getLeftNavBar();
you need to pass array values to the function try
class extends Company {
var $navbar_array = array();
function create_navbar_array ( ) {
$mainurl = $this->company_url; // get the main url address of this web page
$this->navbar_array = array( "Home Page"=>"$mainurl?whichpage=home", "Sales"=>"$mainurl?whichpage=sales",
"Support" => "$mainurl?whichpage=support", "Contacts" => "$mainurl?whichpage=contact" );
return $this->navbar_array;
}
function getLeftNavBar($arr) {
$data ="<table border='1' style='background-color:yellow; width:35%'>";
$data .="<tr><td>".$arr['Home Page']."</td></tr>";
$data .="<tr><td>".$arr['Sales']."</td></tr>";
$data .="<tr><td>".$arr['Support']."</td></tr>";
$data .="<tr><td>".$arr['Contacts']."</td></tr>";
$data .="</table>";
return $data;
}
}
$carobject = new CarDealer();
$arr = $carobject->create_navbar_array();
print $carobject->getLeftNavBar($arr);
or need to make public your array
public $navbar_array = array();
before return $data;, print_r($data); so you can know the associative array's structure. If it didnt print on the page, try looking in view-page-source. You'll get an clear picture. I think $data should be an array, so prob it should be like
$data['someField'] = $someValues;
And, Create or define the array before you use them.
You need to concatenate the array values inside the getLeftNavBar() method so php knows to parse them.
try
$data .="<tr><td>{ $this->navbar_array['Home Page'] }</td></tr>";
or
$data .="<tr><td>".$this->navbar_array['Sales']."</td></tr>";
Why not just call create_navbar_array() inside getLeftNavBar()
function getLeftNavBar() {
$this->create_navbar_array();
.....
You could rewrite it like this
class CarDealer {
private function getUrls() {
return array(
"Home Page" => "$this->company_url?whichpage=home",
"Sales" => "$this->company_url?whichpage=sales",
"Support" => "$this->company_url?whichpage=support",
"Contacts" => "$this->company_url?whichpage=contact"
);
}
public function getLeftNavBar() {
$data ="<table border='1' style='background-color:yellow; width:35%'>";
foreach($this->getUrls() as $url ) {
$data .="<tr><td>".$url."</td></tr>";
}
return $data . "</table>";
}
}
$carobject = new CarDealer();
print $carobject->getLeftNavBar();