how to delete a single semicolon separated value from database in Codeigniter - codeigniter

My Database table is like below, table name is add_family :
id , Head_id ,Head_Name , CustomerName customer_id
1 , 2589 , Mitesh , anuj^anil , 456^259
2 , 2590 , vijay , amit^ajay , 852^454
if I want to delete any of customer name and its id then how can I delete it in CodeIgniter? e.g if I want to delete only CustomerName Anuj and his customer_id 456 where Head_id is 2586,
how can I delete it?

What you should do is use query builder class and load database library.
Here's an example from official docs:
$data = array(
'CustomerName' => 'anil',//or NULL if you want to 'delete it'
'customer_id' => '259'//or NULL if you want to 'delete it'
);
$this->db->where('Head_id', 2586);
$this->db->update('add_family', $data);
If you want to do it dynamically and you always want the part after the ^ you can use , for example, the php explode function like this:
$this->db->select('CustomerName, customer_id');
$this->db->where('Head_id', 2586);
$query = $this->db->get('add_family');
$result = $query->row_array();
$data = array(
'CustomerName' => explode('^', $result['CustomerName'])[1],
'customer_id' => explode('^', $result['customer_id'])[1]
);
$this->db->where('Head_id', 2586);
$this->db->update('add_family', $data);
Note that you should not update the ID of a table if it's the primary key.

Try something like this. Idea is that to get value from database of both field and replace customer name with empty string
$query = $this->db->get_where('add_family',array('id'=>1));
$name = str_replace("anuj","", $query['CustomerName']);
$id = str_replace(456, "", $query['customer_id ']);
$data = array(
'CustomerName' => $name,//or NULL
'customer_id' => $id//or NULL
);
$this->db->where('Head_id', 2586);
$this->db->update('add_family', $data);

Related

How to connect foreign-key on 3 tables relation

I dont know how to create a great title , iam sorry . so
i have 3 table . USER , SPECIAL_USER, EDUCATION_SPECIAL_USER
in USER i have data basic like
id ,name , address , email ,etc
in SPECIAL_USER data
id , user_id , height , weight
in EDUCATION_SPECIAL_USER i have
id , special_user_id , name_school , skills , etc
i have table LIKE on this table . i have problem how to call EDUCATION_SPECIAL_USER based on id on USER . its have problem because my EDUCATION_SPECIAL_USER is not have relation directly on USER table .
its my controller , how to solve this
public function PDF_profile(Request $request,$id){
$users = User::findOrFail($id);
$special_users= Special_Users::where('user_id',$id)->first();
$EDUCATION_SPECIAL_USER= EDUCATION_SPECIAL_USER::where('Special_user_id',$id)->get();
$pdf = PDF::loadView('admin.pdf_profile',
[
'EDUCATION_SPECIAL_USER '=>$EDUCATION_SPECIAL_USER ,
'users' => $users,
'special_users'=> $special_users,
]);
dd($r_kgb);
//return $pdf->stream('Profile.pdf')->header('Content-Type','application/pdf');
}
this data is not out because this wrong id , how i can solve this ?
You can use "has-many-through" relationship:
// In your model User:
public function educationSpecialUsers()
{
return $this->hasManyThrough(EducationSpecialUser::class, SpecialUser::class);
}
// then use:
$user = User::findOrFail($id);
$educationSpecialUsers = $user->educationSpecialUsers;
hope helpful.

How can i give a where condition before update_batch in codeigniter?

I want to update my table where the input the same input fields names are array and has
add more function which generates the input fields like this:
I want to do update_batch in codeigniter
my model i created a function like this:
This is the code block:
function update_batch_all($tblname,$data=array(),$userid)
{
$this->db->trans_start();
$this->db->where('userid',$userid);
$this->db->update_batch($tblname,$data);
$this->db->trans_complete();
return TRUE;
}
it is not working.
can any one help me that how can i update tables data with update batch that has where condition?
You can read the docs for update_batch() here
Here's the short summary:
You pass in an associative array that has both, your where key, and the update value. As the third parameter to the update_batch() call, you specify which key in your assoc array should be used for the where clause.
For example:
$data = array(
array(
'user_id' => 1,
'name' => 'Foo'
), array(
'user_id' => 2,
'name' => 'Bar'
)
);
$this->db->update_batch($tbl, $data, 'user_id');
Breakdown of arguments passed:$tbl is the table name. $data is the associative array. 'user_id' tells CI that the user_id key in $data is the where clause.
Effect of the above query: Name for user with user_id = 1 gets set to Foo and name for user with user_id=2 gets set to Bar.
In your case, if you want to set the same user_id key in each array with your data array, you can do a quick for loop:
foreach ($data as &$d) {
$d['user_id'] = $user_id;
}
$this->db->update_batch($tbl, $data, 'user_id');
You can use,
$this->db->update_batch($tblname,$data,'user_id');
But all array within data must have a field 'user_id'
eg:
$data=array(
array('user_name'=>'test1','user_id'=>1),
array('user_name'=>'test2','user_id'=>2)
);
You can get more details about update_batch from here

Cakephp 2 Paginate Order By Closest Date

I am trying to get the list of results from the Orders table in my CakePhp 2.x application to sort themselves by the date closest to today.
In a usual mysql query I have had something similar working with say the following syntax:
ABS(DATEDIFF(Order.duedate, NOW()))
However in Cake I am not sure how to get such a custom query to work within the paginate helper. Is this something I may need to set in a finder query in the model?
Here is the controller I currently have (set to a bog standard descending sort)
public function index() {
$this->paginate = array(
'conditions' => array (
'Order.despatched' => array('Not Despatched' , 'Split Despatched')
),
'limit' => 25,
'order' => array('Order.duedate' => 'DESC')
);
$data = $this->paginate('Order');
$this->set('orders', $data);
}
Edit: Using information from the comment below I added a virtual field into the controller which causes an sql error. At first I thought this was due to the associations within the model, to attempt to rectify this I added the virtualField into the Order Model and into the constructor of the associated Account Model. However this made no change to the part of the sql which breaks. The full sql error is:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `Order__closestdate`, `Account`.`id`, `Account`.`company`, `Account`.`contac' at line 1
SQL Query: SELECT `Order`.`id`, `Order`.`order_id`, `Order`.`orderdate`,
`Order`.`duedate`, `Order`.`rework`, `Order`.`rework_notes`, `Order`.`comments`,
`Order`.`status`, `Order`.`customer`, `Order`.`last_edited`, `Order`.`value`,
`Order`.`quantity`, `Order`.`order_for`, `Order`.`warehouse`, `Order`.`haulier`,
`Order`.`terms`, `Order`.`type`, `Order`.`despatched`, `Order`.`despatched_date`,
`Order`.`invoiced`, `Order`.`invoice_date`, `Order`.`bookingref`,
`Order`.`purchaseref`, `Order`.`payment_due`, `Order`.`payment_status`,
`Order`.`payment_date`, (ABS(DATEDIFF(duedate, NOW())) AS `Order__closestdate`,
`Account`.`id`, `Account`.`company`, `Account`.`contact`, `Account`.`phone`,
`Account`.`email`, `Account`.`postcode`, `Account`.`created`, `Account`.`modified`,
`Account`.`customer`, `Account`.`address1`, `Account`.`address2`, `Account`.`town`,
`Account`.`county`, (ABS(DATEDIFF(duedate, NOW())) AS `Account__closestdate` FROM
`hunter_om`.`orders` AS `Order` LEFT JOIN `hunter_om`.`accounts` AS `Account` ON
(`Order`.`customer` = `Account`.`customer`) WHERE `Order`.`despatched` IN ('Not
Despatched', 'Split Despatched') ORDER BY (ABS(DATEDIFF(duedate, NOW())) DESC LIMIT 25
For reference the code in the models are:
//Order Model
public $virtualFields = array(
'closestdate' => 'ABS(DATEDIFF(duedate, NOW())'
);
//Account Model
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->virtualFields['closestdate'] = $this->Order->virtualFields['closestdate'];
}
Any help is appreciated thanks.
You'll need to add a 'virtualField' to the Order model before paginating;
$this->Order->virtualFields['closestdate'] = "ABS(DATEDIFF(Order.duedate, NOW()))";
Then use that field as a regular field in your query/paginate
public function index() {
$this->Order->virtualFields['closestdate'] = "ABS(DATEDIFF(Order.duedate, NOW()))";
$this->paginate = array(
'conditions' => array (
'Order.despatched' => array('Not Despatched' , 'Split Despatched')
),
'limit' => 25,
'order' => array('Order.closestdate' => 'DESC')
);
$data = $this->paginate('Order');
$this->set('orders', $data);
}

Multiple ActiveRecord Queries in CodeIgniter

I want to do the following:
//set up insert....
$this->db->insert('property');
$id = $this->db->insert_id();
//some stuff
//set up get
$this->db->where('id', $id);
$id = $this->db->get();
This does not work. It would appear the insert is not being executed before the get - the get is returning zero rows. The insert does (eventually) work. Any suggestions?
You need to give insert some data to insert.
With either the set method:
$this->db->set('name', 'Eric');
$this->db->insert('property');
or by passing an array as the 2nd parameter:
$this->db->insert('property', array(
'name' => 'Eric'
));
As, for your select, you need to tell it what table to select from.
Use either the from method:
$this->db->from('property');
$this->db->where('id', $id);
$id = $this->db->get();
or pass get a table as a parameter:
$this->db->where('id', $id);
$id = $this->db->get('property');
Also, note that get() returns a query object. You need to use ->row (or ->result) to get the data.
$this->db->from('property');
$this->db->where('id', $id);
$query = $this->db->get();
$id = $query->row()->id;
You're missing an argument - insert() takes two:
A table name
An array or object containing columns and values
From the documentation:
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
So, you need to supply insert() with the necessary data by including an array or object as the second argument.
Edit:
Alternatively, you can use the $this->db->set() method for setting values, as explained in Rocket's more comprehensive answer, which also points out you need to specify a table when selecting data.

How to get table structure in CodeIgniter

I want to know the structure of a table. How I can do it in CodeIgniter. Using database class I got 'Invalid SQL Statement' error when I ran $this->db->query('desc mytable');
Try:
$fields = $this->db->list_fields('table_name');
foreach ($fields as $field)
{
echo $field;
}
From manual
For more descriptive information, you should use
$fields = $this->db->field_data('table_name');
You're going to get something like this foreach field in fields as stdClass
name = "id"
type = "int"
max_length = 11
default = null
primary_key = 1
For Get Table Schema in CodeIgniter query:
$query = $this->db->query('SHOW CREATE TABLE yourTableName');
$queryResultArray = $query->result_array();
print_r( $queryResultArray[0]['Create Table'] );

Resources