I have a filter to search the database from it. But if any of the filter fields are empty it is to ignore.
$name = $this->request->data['name'];
$status = $this->request->data['status'];
$code_transaction = $this->request->data['code_transaction'];
$orders = $this->Orders->find('all')->where(['name'=> $name, 'status'=> $status, 'id'=> $code_transaction]);
when you have a blank field behind not query anything database.
I need help to create a query to do the query in the database when you first filled field and ignore those that are blank. Could anyone help me? I accept any idea to create it.
You can do something like this:
$query = ['name'=> $name, 'status'=> $status, 'id'=> $code_transaction];
$newdataquery =[];
foreach($query as $key => $data) {
if (strlen($data) > 0 )
$newdataquery[$key] = $data;
}
$orders = $this->Orders->find('all')->where($newdataquery);
Related
I'm using Zend db to update a record in a MySQL table. I have a SELECT that gets a uuid; and then the following to update the same record:
$data = array(
'fieldname' => 'foobar',
);
$where = array();
$where["uuid = ?"] = $uuid;
$db->update('customers', $data, $where);
Unfortunately the record does not update, and I don't get an error message. The uuid is correct.
I think I was missing the execute. I ended up doing something like this:
$update = $db->update('customers');
$update->where(['uuid' => $uuid]);
$update->set(['fieldname' => 'foobar']);
$statement = $db->prepareStatementForSqlObject($update);
$results = $statement->execute();
This worked fine.
i want to insert two different values into my database.
The field names are same but i want two different values to be saved there.
For Now i have a form which creates Organization. in that i have two fields sales and tech. So i insert Name,last_name,email for sales and tech as well. Now whatever values i get from there i save it to users table and save their id to my organization table.
First i want insert sales person information and then tech person
information and get their id and save it in organization
This is my code:
$this->data['company'] = $this->company_m->get_new();
$this->data['user'] = $this->secure_m->get_new();
$rules = $this->company_m->rules_admin;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE)
{
$data =$this->secure_m->array_from_post(array('first_name','last_name','email'));
$sales_id = $this->secure_m->save($data,$id);
$data =$this->secure_m->array_from_post(array('first_name','last_name','email'));
$tech_id = $this->secure_m->save($data,$id);
$data = $this->company_m->array_from_post(array('org_name','dba','addr1','addr2','city','state','country','pin','sales_id','tech_id','tax_number','comment','url'));
$data['sales_id']= $sales_id;
$data['tech_id']= $tech_id;
$this->company_m->save($data, $id);
redirect('admin/company');
}
// Load the view
$this->data['subview'] = 'admin/company/add';
$this->load->view('admin/_layout_main', $this->
Array from POST code
public function array_from_post($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
You're trying to get values from wrong keys. I.e. you have first_name_sales and first_name_tech, but always read from first_name.
Try
$data_tech = $this->secure_m->array_from_post(array('first_name_tech','last_name_tech','email_tech','tech'));
// ...
$data_sales = $this->secure_m->array_from_post(array('first_name_sales','last_name_sales','email_sales','sales'));
// ...
Also you should set_value from corresponding objects instead of same $user. Pass like $user_tech and $user_sales to view. Smth like
$this->load->view('admin/_layout_main', ['user_tech' => $data_tech, 'user_sales' => $data_sales]);
$values = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'user_type' => $this->input->post('user_type'),
);
$data = $this->model->inset('table_name', $values);
And also use this array to many tables
So I went over this thread to figure out how to send data from controller to a view here.
However I am trying to pass multiple values with identical titles to the view and I cannot seem to figure out how to do this and still access them later.
I have a for loop that grabs all the films a user has in their database, this information is then stored into a array called $data
$data = array();
foreach ($films as $film)
{
$title = $film->title;
$description = $film->description;
$url = $film->fileUrl;
$data = array_add($data, 'title', $title);
$data = array_add($data, 'description', $description);
$data = array_add($data, 'url', $url);
}
$this->layout->content = View::make('profileFilms')->with($data);
This works but I cannot figure out how to access this information, I tried making each "film" its own array with all three data values and then adding that to the larger array and then passing that array to the view, which again worked but I cannot figure out how to access the data. if i use say {{$title}} it will grab one title, but I have no way of getting both.
How can I pass N number of films each with its title, description and url to a view so I can display them, and how can I display them?
You are adding the values directly to the array once and that is it, because Laravels array_add only overwrites non existing keys.
You should do something like that (or use Eloquent):
<?php
$data = array();
foreach ($films as $film){
$data['films'][] = array(
'title' => $film->title,
'description' => $film->description,
'url' => $film->fileUrl
);
}
$this->layout->content = View::make('profileFilms')->with($data);
Then you can access your films (Blade style):
#foreach($films as $film)
{{$film['title']}}
#endforeach
If you want to use it like $film->title you can use the (object) typecast while assigning the array.
Of course, you could also pass $films directly like
$data = array(
'films' => $films
);
Then you can access your films like you would in the controller. You can alsways use print_r and var_dump to see, what the variable really contains.
$data = array();
foreach ($films as $film)
{
$row = array();
$row = array_add($data, 'title', $film->title);
$row = array_add($data, 'description', $film->description);
$row = array_add($data, 'url', $film->fileUrl);
// will send to view
$data[] = $row;
}
$this->layout->content = View::make('profileFilms')->with($data);
I am adding data to three tables, I needed to get the last ID of the first table to use in the second table, which was successful with $this->db->insert_id() function, Trying that with the second table still gives me the ID of the first table. The arrangement of my code is:
function addcrm() {
//Post data collection array from the webform form
$customerdata = array(
"salutation"=>$this->input->post('salutation'),
"mobilenumber"=>$this->input->post('mobilenumber'),
"emailaddress"=>$this->input->post('emailaddress')
);
$this->db->insert('api_customer', $customerdata);
$customer=$this->db->insert_id();
$leaddata = array(
"fieldrep"=>$this->input->post('fieldrep'),
"fk_customerID"=>$customer,
"te"=>$this->input->post('takage'),
"othercost"=>$this->input->post('othercost')
);
$this->db->insert('api_lead', $leaddata);
$leadID = $this->db->insert_id();
for ($i =0; $i<count($_POST['w_product']); $i++){
$productdata = array(
"name" => $_POST['w_product'][$i],
"type" => $_POST['w_type'][$i],
"cost" => $_POST['w_cost'][$i],
"fk_leadID"=> $leadID
);
$this->db->insert('api_prod',$productdata);
}
$url = base_url('cXXXXXXXXXXXXXX);
redirect($url);
}
You are missing something obviously in the second call. ;)
$customer = $this->db->insert_id();
$leadIdD = $this->db->insert_id;
See? :)
Try working with the following methods:
$this->db->start_cache(); // Before query
$this->db->stop_cache(); // After query
$this->db->flush_cache(); // Clear query
This way you make clear and flushed queries.
if you are using an auto increment table you may try using MYSQL_LAST_INSERT_ID();
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html
this is the sql query i used before i switch to codeigniter
UPDATE transaction SET due_amount = to_pay-$payment_amount WHERE id = '$id'
with codeigniter
$this->db->set('due_amount', 'to_pay-'.$payment_amount, FALSE);
$this->db->where('id', $id);
$result = $this->db->update('transaction');
in the codeigniter code am i writing $this->db->set correctly?
Regards
You should do it like this:
$this->db->where('id', $id);
$result = $this->db->update('transaction',array('due_amount'=>'to_pay-'.$payment_amount));
Hope this helps.
There are multiple variants to use the updateDocs function. Yours doesn't look specifically bad (I would however don't bypass escaping if not for a specific reason), maybe you prefer to use an array (or object) to provide the data being updated:
$data = array(
'due_amount' => 'to_pay-'.$payment_amount,
);
$this->db->update('transaction', $data, sprintf('id = %d', $id));
I think here could be some issue with $this->db->set
Standard way of updating content in codeignitor is this
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Produces:
UPDATE mytable
SET title = '{$title}', name = '{$name}', date = '{$date}'
WHERE id = $id