I have a function is select from books table insert to the orders table.
Now I want to select from books and users table for insert to the orders table.
This is book table
This is the orders table
This is the users table
model
public function history($book_id){
$this->db->select('book_id,book_title,pickup,return');
$this->db->from('books');
$this->db->where('book_id', $book_id);
$query = $this->db->get();
if ( $query->num_rows() > 0 ) // if result found
{
$row = $query->result_array();
foreach($row as $values){
$data = array(
'book_id' => $values['book_id'],
'title' => $values['book_title'],
'pickup' => $values['pickup'],
'return_time' => $values['return'],
);
$this->db->insert('orders', $data);
}
return true;
}
else{
return false;
} }
You will join the tables like this
$this->db->join('users_table_name', 'users_table_name.id = books.user_id');
Everything else should work the same
for user_id u can get from session,
for example :
$data = [
'book_id' => $values['book_id'],
'user_id' => $this->session->userdata('session_name'),
'title' => $values['book_title'],
'pickup' => $values['pickup'],
'return_time' => $values['return'],
];
hopes this helps
public function insert_record($book_id, $user_id) {
$book = $this->db->select('book_id, book_title, pickup, return')
->from('books')
->where('book_id', $book_id)
->get()
->result_array();
if(!empty($book)) {
$data = array(
'book_id' => $book[0]['book_id'],
'user_id' => $user_id,
'title' => $book[0]['book_title'],
'pickup' => $book[0]['pickup'],
'return_time' => $book[0]['return'],
);
$this->db->insert('orders', $data);
}
}
Related
Anyone help me i am trying to delete the records in Certificates and after deleting with respect to id in Books table is_delete column should update to 0 For me it's updating but particular id is not updating all the id's are updating
public function deleteTC($id)
{
$condition['Items.is_delete'] = 0;
$fields = ['Books.is_delete', 'DegreeStudents.id','Items.some_id'];
$tableList = $this->Books->find('all', ['conditions' => $condition,'fields'=>$fields, 'join' =>
[
'Items' =>
[
'table' => 'items', 'type' => 'INNER',
'conditions' => ['Items.some_id = Books.id','Books.is_delete'=>2],
]
]])->enableHydration(false)->toArray();
foreach ($tableList as $i=>$list){
$result = $this->Certificates->deleteData($id);
if($result) {
$condition['Certificates.is_delete'] = 1;
{
$this->Books->update(['id' => $list['id']], ['is_delete' => 0]);
}
$this->Flash->set('Record deleted successfully..!', ['element' => 'success']);
}
else {
$this->Flash->set('Error while deleting the record', ['element' => 'error']);
}
$this->redirect(array("controller" => "Report", "action" => "someIssued"));
} }
}
I have a working query but its only returning 1 row - where or what can I do to return all existing rows?
Model:
public function getInfo() {
$info_query = $this->db->query("SELECT * FROM `km_info` WHERE ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'");
if ($info_query->num_rows){
return array(
'info_id' => $info_query->row['info_id'],
'name' => $info_query->row['name'],
'amount' => $info_query->row['amount'],
'date_start' => $info_query->row['date_start'],
'date_end' => $info_query->row['date_end'],
'status' => $info_query->row['status'],
'date_added' => $info_query->row['date_added']
);
}
}
Controller:
$info_options = $this->model_extension_total_info->getInfo();
if ($info_options){
$json['info_options'] = array();
$json['info_options'][] = array(
'info_id' => $info_options['info_id'],
'name' => $info_options['name'],
'amount' => $info_options['amount'],
'date_start' => $info_options['date_start'],
'date_end' => $info_options['date_end'],
'status' => $info_options['status'],
'date_added' => $info_options['date_added']
);
}
When I try foreach() in the controller, I still only get one row.:
foreach ($info_options as $info) {
var_dump($info_options['name']);exit;
//var_dump($info_options['name']); results: Warning: Illegal string offset 'name'
}
In the model, when I dump:
$info_query I get 9 -- which is all of the rows I'm expecting.
Not particularly certain what I'm missing or doing wrong.
You're not looping over the result. Not in model nor in the controller. Though I don't know why you'd do this twice. Maybe I'm just not understanding your code.
Model:
$data = [];
if ($info_query->num_rows){
foreach ($info_query->result() as $row) {
$data[] = array(
'info_id' => $row['info_id'],
'name' => $row['name'],
'amount' => $row['amount'],
'date_start' => $row['date_start'],
'date_end' => $row['date_end'],
'status' => $row['status'],
'date_added' => $row['date_added']
);
}
return $data;
}
Controller:
$info_options = $this->model_extension_total_info->getInfo();
$json['info_options'] = array();
if ($info_options){
foreach ($info_options as $info) {
$json['info_options'][] = array(
'info_id' => $info['info_id'],
'name' => $info['name'],
'amount' => $info['amount'],
'date_start' => $info['date_start'],
'date_end' => $info['date_end'],
'status' => $info['status'],
'date_added' => $info['date_added']
);
}
}
Technically, you don't have to loop at all. You can just:
public function getInfo() {
$info_query = $this->db->query("SELECT * FROM `km_info` WHERE ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'");
if ($info_query->num_rows){
return $info_query->result();
}
else
{
return false;
}
}
and
$info_options = $this->model_extension_total_info->getInfo();
// and that's it, just do whatever you need with $info_options
I have a table called uploaded_files in my database where I have the following fields:
id, file_name, upload_id, filetype_id
this last field (filetype_id) is a Foreign Key to a table called filetype where I have these fields:
id, filetype_desc
I'm trying to create a method in my model to return all the fields in table uploaded_files, in an array following this structure:
array(
'file_type_1' => array(
'id' => 1,
'file_name' => "lalala.txt",
'upload_id' => $uploadId,
'filetype_id' => 1
),
'file_type_2' => array(
'id' => 1,
'file_name' => "lelele.txt",
'upload_id' => $uploadId,
'filetype_id' => 2
),
'file_type_3' => array(
'id' => 1,
'file_name' => "blabla.txt",
'upload_id' => $uploadId,
'filetype_id' => 3
),
);
}
Until now, I came to this:
public function get_UploadedFilesFromUploadId($uploadId){
//$query = "select * from uploaded_files where upload_id =".$uploadId;
$query = $this->db->get_where('uploaded_files', array(
'upload_id' => $uploadId
)
);
return $this->db->query($query);
}
$query = $this->db->get_where('uploaded_files', array(
'upload_id' => $uploadId
)
);
$res = $this->db->query($query);
$result = array();
foreach ($res->result_array() as $row)
{
$key = 'file_type_'.$row['filetype_id'];
$result[$key] = $row;
}
$query = $this->db->get_where('uploaded_files',array('upload_id' => $uploadId));
foreach($query->result() as $row)
{
echo $row->filetype_id;
}
I think it's help you.
I am new with codeigniter.
I have following query, each query having different table name and different where condition.
Can i make this query in single query or is it correct way to execute query.
i have to execute all this query.
$q = $this->db->where('id', $st)->update('st_info', array(
'status' => 0,
'updated_time' => $this->info_model->getTime(),
'updated_ip' => $this->info_model->getIP()
));
$q = $this->db->where('verid', $vid)->update('st_version', array(
'status' => 0,
'updated_time' => $this->info_model->getTime(),
'updated_ip' => $this->info_model->getIP()
));
$q = $this->db->where('id', $id)->update('st_raw', array(
'status' => 0,
'updated_time' => $this->info_model->getTime(),
'updated_ip' => $this->info_model->getIP()
));
Since you are using same columns for all the tables, i prefer to work with below method,
$data = array(
'status' => 0,
'updated_time' => $this->info_model->getTime(),
'updated_ip' => $this->info_model->getIP()
);
$q = $this->db->where('id', $st)->update('st_info', $data);
$q = $this->db->where('verid', $vid)->update('st_version', $data);
$q = $this->db->where('id', $id)->update('st_raw', $data);
That is correct you cannot combine all that into a single query.
Try this:
$data=array(
'status' => 0,
'updated_time' => $this->info_model->getTime(),
'updated_ip' => $this->info_model->getIP()
));
$this->db->where('id',$st);
$this->db->update('st_info', $data);
$this->db->where('verid',$vid);
$this->db->update('st_version', $data);
$this->db->where('id',$id);
$this->db->update('st_raw', $data);
Create a single function
public function update($table,$where,$data)
{
$this->db->where($where);
$this->db->update($table,$data);
}
Now call this function like this from controller
function update_call()
{
$data['status'] = 0;
$data['updated_time'] = $this->info_model->getTime();
$data['updated_ip'] = $this->info_model->getIP();
$where['id'] = $st;
$table = 'st_info';
$this->model_name->update($table,$where,$data);
unset($where);
unset($table);
$where['verid'] = $vid;
$table = 'st_version';
$this->model_name->update($table,$where,$data);
unset($where);
unset($table);
$where['id'] = $id;
$table = 'st_raw';
$this->model_name->update($table,$where,$data);
}
I am developing a website using CI and mysql. i want to change date format from yyyy-mm-dd into dd-mm-yyyy. I know that i should use date_format function. But when I try to use it, it didn't work-the date format didn't change. Also i've try to add 2nd parameter(FALSE). but, I also found problem with that, especially, when i need to query more than 1 column. Here is my code:
function __construct() {
parent::__construct();
$this->table = array(
'name' => 'article',
'coloumn' => array(
'article_id',
'article_title',
'article_content',
'article_summary',
'date_format(article_publishdate, \'%d-%M-%Y %H:%i:%s\') as article_publishdate',
'article_source',
'article_link',
'media_type',
'media_link',
'media_position',
'article_category.category_title',
),
'join' => array(
0 => array('article_category' , 'article.article_category_id=article_category.category_id', 'left'),
),
'where' => array(),
'order' => array(),
'limit' => array(),
'idkey' => 'article_id',
);
}
public function getfullbody($id)
{
$this->db->query("update article set article_view = (article_view+1) where article_id = '$id'");
$this->db->select($this->table['column'], FALSE);
if (count($this->table['join'])>0){
foreach ($this->table['join'] as $row):
if (!empty($row[2])){
$this->db->join($row[0], $row[1], $row[2]);
}
else {
$this->db->join($row[0], $row[1]);
}
endforeach;
}
$this->db->where("article_id = '$id'");
$query = $this->db->get($this->table['name']);
$data = $query;
return $data;
}
Problem: when i use date_format() with codeigniter, it didn't change the date format.
Then, how to fixed it? Thank you.
Change coloumn to column index and if you want to show date as dd-mm-yyyy use this below code
function __construct() {
parent::__construct();
$this->table = array(
'name' => 'article',
'column' => array(
'article_id',
'article_title',
'article_content',
'article_summary',
'date_format(article_publishdate, \'%d-%m-%Y\') as article_publishdate',
'article_source',
'article_link',
'media_type',
'media_link',
'media_position',
'article_category.category_title',
),
'join' => array(
0 => array('article_category' , 'article.article_category_id=article_category.category_id', 'left'),
),
'where' => array(),
'order' => array(),
'limit' => array(),
'idkey' => 'article_id',
);
}
public function getfullbody($id)
{
$this->db->query("update article set article_view = (article_view+1) where article_id = '$id'");
$this->db->select($this->table['column'], FALSE);
if (count($this->table['join'])>0){
foreach ($this->table['join'] as $row):
if (!empty($row[2])){
$this->db->join($row[0], $row[1], $row[2]);
}
else {
$this->db->join($row[0], $row[1]);
}
endforeach;
}
$this->db->where("article_id = '$id'");
$query = $this->db->get($this->table['name']);
$data = $query;
return $data;
}
Test on Code igniter 2...
('%d-%m-%Y')
in your coude
"date_format(article_publishdate, ('%d-%m-%Y')) as article_publishdate",
it's work very weel for me