How to insert a data in a table in CodeIgniter - php

I'm having some trouble updating records with the Codeigniter framework. I'm using the MVC pattern and active records.
public function save_yarn($data)
{
$this->db->select('sub_name');
$this->db->from('mst_subject');
$this->db->insert($data);
}

This function should works for you:
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
https://ellislab.com/codeigniter/user-guide/database/active_record.html#update

If you're using models for your tables, you can do something easy like
$this->yarn_model->update($row_id_to_update, $data);
Otherwise, Angel's method will work too.

Related

Codeigniter model with multiple update conditions using manual where statement

I have this code in model that returns data
$this->db->select('title, content, date');
$where = "name='Joe' AND status='boss'";
$this->db->where($where);
$query = $this->db->get('mytable');
return $query->result();
I am using a manual where and i wanted to know whether the concept of manual where in updates and possibly in deletes is allowed.
This code updates a table based on a single where condition
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Is doing this allowed in codeigniter
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$where = "id=$id, name='Joe' AND status='boss'";
$this->db->where($where);
$this->db->update('mytable', $data);
As per document in CodeIgniter Where, you have to mention here with OR or AND "id=$id, name='Joe'.
$where = "id=$id AND/OR name='Joe' AND status='boss'";
^^^^
Chose one
Or use array

How to get array data in url in Laravel 5.2?

This is my Route.php
Route::get('users/{user_name}/{source}/{destination}/{members}' , 'Userscontroller#index');
I want array of members in url. How can I get it and How can I pass to index() in UsersController.
Here is my UsersController.php
public function index($user_name, $source , $destination , $members[] )
{
$users = DB::table('users')->insert(array(
'user_name' => $user_name,
'source' => $source,
'destination' => $destination,
));
}
How Can I store array of members in database as well ?
Thank You in Advance.
You can do something like this:
Route::get('users/{user_name}/{source}/{destination}/{members}' , 'Userscontroller#index')->where(['members'=>'.*']);
Then in your controller...
$members = explode("/", $members);
This would let you use a URL like
/users/user_name/source/destination/member1/member2/member3
For storing arrays in databases whilst using eloquent... I've just realised you're not using a Model here but inserting straight into the database, I'd suggest creating a Users model and then you can do this with the below. Otherwise any request for this data you'll need to remember to manually unserialise in whatever format you serialised the data in.
Using attribute casting inside a model (https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting):
Add to the Model:
protected $casts = ['members'=>'array'];
Then you let Laravel do the rest for you:
$users = Users::insert(array(
'user_name' => $user_name,
'source' => $source,
'destination' => $destination,
'members' => $members
));

How to use findAllBy() with pagination?

I'm working on a project where I want to search for people based on skills they say they have on their profile. I want to use the findAllBy(); function but I'm not sure how this works with the Pagination component packaged with CakePHP.
Just use conditions in Paginator, like :
public function skills($skill = null){
$this->Paginator->settings = array(
'conditions' => array(
'skill LIKE' => '%'.$skill.'%'
)
);
$people = $this->Paginator->paginate('People');
$this->set('people', $people);
}

Codeigniter passing data from model to controller parser

I am about to implement the parser class to my codeigniter project and would like some guidance in passing the data from my model to the parser array. Which is the better more efficient way to do it.
My model gets the data and returns it to the controller. How can I get it into the array in the controller?
Model:
function getSeo($data){
$this->db->select('seo_title, seo_description, seo_keywords');
$this->db->from('content');
$this->db->where($data);
$query = $this->db->get();
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'seo_title' => $row->seo_title,
'seo_description' => $row->seo_description,
'seo_keywords' => $row->seo_keywords
);
}
return $data;
}
Controller:
$viewdata['pageseo'] = $this->Content_model->getSeo($data);
$viewdata = array(
'seo_title' => 'My seo Title',
'seo_description' => 'My seo description',
'seo_keywords' => 'My seo keywords',
);
What is the best way to get the data from my model into the '$viewdata' array, how is it done????
Since the getSeo function from your model returns an array, the Controller will store this information to your $viewdata array as well.
If you try a print_r($viewdata) you'll see that the structure is as expected. Which is $viewdata['seo_title'] => 'My seo Title'
and so on...
There is function called result_array() which is used to get the result set as array and the below link may help you. This is the core library function.
Plz refer,
http://codeigniter.com/user_guide/database/results.html

Inserting NOW() into Database with CodeIgniter's Active Record

I want to insert current time in database using mySQL function NOW() in Codeigniter's active record. The following query won't work:
$data = array(
'name' => $name ,
'email' => $email,
'time' => NOW()
);
$this->db->insert('mytable', $data);
This is because CodeIgniter’s ActiveRecord class automatically escapes the input.
The following works fine, by calling set() and passing peratmeter FALSE, so that it doesn't escape the NOW().
$data = array(
'name' => $name ,
'email' => $email,
);
$this->db->set('time', 'NOW()', FALSE);
$this->db->insert('mytable', $data);
However, my question is that is there any other way than this? For example, if i can use somehow use by adding everything in the data array only?
For example, something like:
$data = array(
'name' => $name ,
'email' => $email,
'time' => NOW(), FALSE
);
I typically use triggers to handle timestamps but I think this may work.
$data = array(
'name' => $name,
'email' => $email
);
$this->db->set('time', 'NOW()', FALSE);
$this->db->insert('mytable', $data);
Unless I am greatly mistaken, the answer is, "No, there is no way."
The basic problem in situations like that is the fact that you are calling a MySQL function and you're not actually setting a value. CI escapes values so that you can do a clean insert but it does not test to see if those values happen to be calling functions like aes_encrypt, md5, or (in this case) now(). While in most situations this is wonderful, for those situations raw sql is the only recourse.
On a side, date('Y-m-d'); should work as a PHP version of NOW() for MySQL. (It won't work for all versions of SQL though).
aspirinemaga, just replace:
$this->db->set('time', 'NOW()', FALSE);
$this->db->insert('mytable', $data);
for it:
$this->db->set('time', 'NOW() + INTERVAL 1 DAY', FALSE);
$this->db->insert('mytable', $data);
This is the easy way to handle timestamp insertion
$data = array('created_on' => date('Y-m-d H:i:s'));
you can load the date helper and use the codeigniter interal now() if you want to reference the users GMT time offset
it woulk look somewhat like this
$data = array(
'created_on' => date('Y-m-d H:i:s',now())
);
If you don't use the GTM master settings and let your users set their own offsets there is no advantage over using php's time() function.
$data = array(
'name' => $name ,
'email' => $email,
'time' =>date('Y-m-d H:i:s')
);
$this->db->insert('mytable', $data);
According to the source code of codeigniter, the function set is defined as:
public function set($key, $value = '', $escape = TRUE)
{
$key = $this->_object_to_array($key);
if ( ! is_array($key))
{
$key = array($key => $value);
}
foreach ($key as $k => $v)
{
if ($escape === FALSE)
{
$this->ar_set[$this->_protect_identifiers($k)] = $v;
}
else
{
$this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
}
}
return $this;
}
Apparently, if $key is an array, codeigniter will simply ignore the second parameter $value, but the third parameter $escape will still work throughout the iteration of $key, so in this situation, the following codes work (using the chain method):
$this->db->set(array(
'name' => $name ,
'email' => $email,
'time' => 'NOW()'), '', FALSE)->insert('mytable');
However, this will unescape all the data, so you can break your data into two parts:
$this->db->set(array(
'name' => $name ,
'email' => $email))->set(array('time' => 'NOW()'), '', FALSE)->insert('mytable');
putting NOW() in quotes won't work as Active Records will put escape the NOW() into a string and tries to push it into the db as a string of "NOW()"... you will need to use
$this->db->set('time', 'NOW()', FALSE);
to set it correctly.
you can always check your sql afterward with
$this->db->last_query();
Using the date helper worked for me
$this->load->helper('date');
You can find documentation for date_helper here.
$data = array(
'created' => now(),
'modified' => now()
);
$this->db->insert('TABLENAME', $data);
$this->db->query("update table_name set ts = now() where 1=1")
also works for current time stamp!
run query to get now() from mysql i.e select now() as nowinmysql;
then use codeigniter to get this and put in
$data['created_on'] = $row->noinmyssql;
$this->db->insert($data);

Categories