How to update record in codeigniter? - php

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);
}

Related

How to Explode data from database in controller of CodeIgniter?

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'))
);

How dou you write/call 2 tables in $this->db->table('table_name') in Codeigniter 4

Hello i have 2 tables that i want to call right now, for the EDIT (part of the CRUD)
tables:
table_a
table_b
i found in youtube how to update/edit from 2 tables, i need to call bot of the tables.
here's the code for the model
public function edit_this($ID_A)
{
return $this->db->table('table_a', '*i don't know how to insert the 2nd table')->where('ID_A', $ID_A)->get()->getRowArray();
}
Here's the controller
public function this_edit($ID_A)
{
$data = [
'title' => 'Admin',
'navbartitel' => 'You know this',
'alledit' => $this->theModel->edit_this($ID_A),
'validation' => \Config\Services::validation()
];
return view('this/all/edit', $data);
}
it works but i only can accsess the tabel_a, but i need them both so i can show what i've written in the edit form, from the database
anyone can help? thank you
$this->db->table(...) returns an instance of QueryBuilder and will happily accept a single string of comma-separated tables ("table1, table2..."), or even an array for that matter (['table1', 'table2'...]), as its first parameter. You are doing neither and instead passing multiple parameters.
When you call table(), the value passed in the first parameter is used during the creation of the database-specific Builder class:
public function table($tableName)
{
if (empty($tableName))
{
throw new DatabaseException('You must set the database table to be used with your query.');
}
$className = str_replace('Connection', 'Builder', get_class($this));
return new $className($tableName, $this);
}
The DB-specific Builder class has no constructor of its own so falls back on the __construct defined in BaseBuilder, which it extends:
public function __construct($tableName, ConnectionInterface &$db, array $options = null)
{
if (empty($tableName))
{
throw new DatabaseException('A table must be specified when creating a new Query Builder.');
}
$this->db = $db;
$this->from($tableName);
...
I've truncated this for brevity because the important part is that call to $this->from, which is in the end how multiple tables get processed:
public function from($from, bool $overwrite = false)
{
if ($overwrite === true)
{
$this->QBFrom = [];
$this->db->setAliasedTables([]);
}
foreach ((array) $from as $val)
{
if (strpos($val, ',') !== false)
{
foreach (explode(',', $val) as $v)
{
$v = trim($v);
$this->trackAliases($v);
$this->QBFrom[] = $v = $this->db->protectIdentifiers($v, true, null, false);
}
}
else
{
$val = trim($val);
// Extract any aliases that might exist. We use this information
// in the protectIdentifiers to know whether to add a table prefix
$this->trackAliases($val);
$this->QBFrom[] = $this->db->protectIdentifiers($val, true, null, false);
}
}
return $this;
}

How to do an array merge between two functions?

I'm trying to get an array_merge but I can't. Calling the example.php file must also embed the array of the Usage_Module function.
Note: The getFunctionModule function must not be changed.
Surely I'm wrong something in the Usage_Module function, can you help me understand what I'm wrong?
Code example.php
function getFunctionModule() {
require "module.php";
$func = "Usage_Module";
if (!function_exists($func)) {
echo "function not found";
}
return (array) $func();
}
$data = ['status' => 'ok'];
$data = array_merge(getFunctionModule(), $data);
print_r($data);
Code module.php
function Usage_Module()
{
$data = ['licensekey2' => 'ok'];
return (array) $data;
}

couldn't get data using laravel eloquent

What I want is that when a user visits this link /api/bonus?provider[]=MyCompany
the result will show only bonus provided by providers=[MyCompany].
In my controller:
public function all(Request $request)
{
$size = $request->input('size');
$bonus = Bonus::with('category', 'bonus');
$bonus = Filterer::apply($request, $size, $bonus);
$bonus = Filterer::apply($request, $size, $bonus);
return response()->json([
'code' => 0,
'success' => true,
'data' => BonusResource::collection($bonus),
}
My expected result is to get all the providers that equal the [MyCompany]
But somehow this query doesn't work.
Filterer
public static function apply(Request $filters, $size, $bonus)
{
if ($filters->has('provider')) {
$bonus->whereHas('bonus', function ($query) use ($filters) {
$query->whereIn('providers', $filters->input('provider',[]));
});
}
return $bonus->paginate($size);
}
but at the end I'm getting this result. The data is null [].
I'm wonder why I can't get the data. Which part I had done wrong?
To actually get results you should use some function that gets data from database, for example get, first (depends whether you want to get multiple rows or only first row). Now you only prepare query that should be executed but you never execute it, so yo should change line:
$bonus = Filterer::apply($request, $size, $bonus);
into
$bonus = Filterer::apply($request, $size, $bonus);
$bonus = $bonus->get();
to execute query and get results.
I might be wrong, but based on your code, the function apply didn't return anything, it will always empty. Shouldn't it be return $bonus? and don't forget the get.
public static function apply(Request $filters, $size, $bonus)
{
if ($filters->has('provider')) {
$bonus->whereHas('bonusRelease', function ($bonus) use ($filters) {
return $bonus->whereJsonContains('providers', $filters->input('provider',[]));
});
}
return $bonus->get();
}
Updated my answer
The providers is a json type. You can't just query it with where in (a,b,c). Change to whereJsonContains. Please check this link another case and laravel docs.
First, convert the input into array, here I'm using explode()
and then I loop the converted array and use %% to search the providers. Hope it will help!
if ($filters->has('provider') && trim($filters->input('provider')) != "") {
$bonus_list->whereHas('bonusCompany', function ($query) use ($filters) {
$providers = explode(',', (trim($filters->input('provider'), '[]')));
foreach ($providers as $key => $provider) {
if ($key == 0) {
$query->where('providers', 'LIKE', ['%' . trim($provider) . '%']);
} else {
$query->orwhere('providers', 'LIKE', ['%' . trim($provider) . '%']);
}
}
});
};

Query in php on Codeigniter

I can't use my query in php when i use codeigniter.
I think that it's all ok but my code doesn't work.
What is it wrong ?
I need to cofront two parametres.
I have a table and i need colonn3
Models:
function get_colonn3($data){
$this->db->select("table.colonn3");
$this->db->where("table.colonn3",$data);
return $this->db->get("table.colonn3")->result_array();
Controller:
$colonn3_exsist = $this->my_model->get_colonn3($data)
if($colonn3_exsist):
echo "ok";
endif;
Remove table.colon3 from get(), maybe it will work
return $this->db->get()->result_array();
I believe is because you're passing the table name to the where statement and it expects a column.
CodeIgniter where documentation
function get_colonn3($data) {
$this->db->select("table.colonn3");
$this->db->where("colonn3", $data);
return $this->db->get("table")->result_array();
}
They also allow you to pass an array to the where parameters too:
function get_colonn3($data) {
$this->db->select("table.colonn3");
$this->db->where([
"colonn3" => $data
]);
return $this->db->get("table")->result_array();
}
CI also allows you to chain DB functions like so:
function get_colonn3($data) {
$this->db->select("table.colonn3")
->where([
"colonn3" => $data
]);
return $this->db->get("table")->result_array();
}
try this:
//model
function get_colonn3($data){
$this->db->select("colonn3");
$this->db->where("colonn3",$data);
return $this->db->get("table")->result_array();
//controller
$colonn3_exsist = $this->my_model->get_colonn3($data)
if($colonn3_exsist>0){
echo"ok";
}
You are passing the value to WHERE in a wrong way. You have to pass the values in an array without passing the table name.
You have to pass the array in WHERE clause like this if you have more than one parameter to compare from Database:
function get_colonn3($para1,$para2){
$this->db->select("table.colonn3");
$this->db->where(['columnname1' => $para1, 'columnname2' => $para2]);
return $this->db->get("table.colonn3")->result_array();
}
If you have only one parameter to compare then you can use this:
function get_colonn3($para1){
$this->db->select("table.colonn3");
$this->db->where('columnname1',$para1);
return $this->db->get("table.colonn3")->result_array();
}
Model
function get_colonn3($data){
return $this->db->select("table.colonn3 as col3")->from('table')->where(['table.colonn3' => $data])->get()->row()->col3;
}
Controller
$colonn3 = $this->my_model->get_colonn3($data)
if(!empty($colonn3)):
echo "ok";
endif;

Categories