Comparing two values from two different tables using nested foreach loop - php

I have a table of item and a table cart. What I'm trying to do is fetch all instances of item and cart then I put them in the loop such that whenever the item's item_id matches the cart's item_id, I'll subtract cart's item_quantity value from item's number_of_units. The result is then used to update item's number_of_items. There's seems to be a problem with my nested foreach loop.
Here's a function in my controller
public function checkout(){
$this->load->model('HomeModel');
$x['item'] = $this->HomeModel->displayAllItems();
$y['cart'] = $this->HomeModel->displayAllCartItems();
foreach($x['item'] as $item){
foreach($y['cart'] as $cart){
if(($item->item_id) == ($cart->item_id)){
$temp1 = $item->number_of_units;
$temp2 = $cart->item_quantity;
$temp = $temp1 - $temp2;
$z = array(
'number_of_units' => $temp,
);
$this->HomeModel->updateItem($z, $item->item_id);
}
}
}
}
Here's some of my model functions:
public function displayAllCartItems()
{
$query = $this->db->select('*')->from('cart')->get();
return $query->result();
}
public function displayAllItems()
{
$query = $this->db->select('*')->from('item')->get();
return $query->result();
}
public function updateItem($data, $id)
{
$this->db->where('item_name', $id);
$query = $this->db->update('item', $data);
}

Related

Facing some problem but can't solved this

When i using this below without where condition query i get the values
Model:
public function get_teacher_number() {
$this->db->select('staff.*');
$this->db->from('staff');
$this->db->join('staff_roles','staff.id=staff_roles.staff_id','inner');
$query = $this->db->get();
return $query->result_array();
}
Output:
2323232,262626,32323,,26262623265265,
When i using this below with where condition query i can't get all the values it show me just one value but when i tried this sql in my mysql server its show me 2 values and here i get only one value.
Model:
public function get_teacher_number() {
$this->db->select('staff.*');
$this->db->from('staff');
$this->db->join('staff_roles','staff.id=staff_roles.staff_id','inner');
$this->db->where('staff_roles.role_id',2);
$query = $this->db->get();
return $query->result_array();
}
Output:
262626,,
one value is missing..
Here i give you controller code:
$all_teacher = $this->teacher_model->get_teacher_number();
$x = '';
foreach ($all_teacher as $val) {
$smsid= $val["id"];
$number = $val["contact_no"];
$x = $x.$number.","; //number separated by comma
}
echo "<pre>";
print_r($x);
exit();

explode function in codeigniter

I am new to programming , i have a problem while using explode function ie, after i explode the value and pass those value to a model to retrive records from db only the last value from the exploded data is showing.
ex i have impoded data as (test,test1,test2) after explode the data is like(testtest1test2) here i pass these value to model to take records from a table where a field name is equal to the exploded value.I dont't know how to explain this properly please forgive me.
public function location($id) {
$query = $this->gt_pav_model->select($id);
$data['selectdata'] = $query->result();
foreach( $data['selectdata'] as $s) {
$explode = $s->package_id; // this is to get imploded data from db
}
$t = explode(",", $explode);
foreach( $t as $tt) {
$query = $this->gt_pav_model->select_pa($tt);
$data['dataa'] = $query->result();
$this->load->view('pav/pavdetails',$data);
}
}
public function location($id) {
$final_result = array();
$result= $this->gt_pav_model->select($id)->result();
foreach( $result as $key=> $re) {
$explode = $re->package_id;
$t = explode(",", $explode);
$param = $t[1];
$datas = $this->gt_pav_model->select_pa($param)->result();
$final_result[$key] => $datas;
}
$this->load->view('pav/pavdetails',array('datas'=>$final_result));
}
You can print result in your view page <?php print_r($datas); ?>

How to pass multiple array of Id in where condition codeigniter?

I want to pass multiple id in where condition how to do it?
A query fetches org_id from database.
now I want to pass that in my where condition, so how to do it?
I tried foreach loop:
Below is my code:
$devices = $this->activation_m->get_activated_devices();
$org_id = array(); // create org_id array
foreach ($devices as $row)
{
$org_id[]= $row['org_id']; // assign ids into array
//$companys =$this->data['devices'] = $this->activation_m->get_company($org_id); // don't call again and again
}
//Now $org_id is array
$companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
echo $this->db->last_query();
Model Code
public function get_activated_devices()
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.site_key = activation.site_key');
$this->db->from('activation');
$query =$this->db->get();
$result = $query->result_array();
return $result;
}
public function get_company($org_id)
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
$this->db->where('company.id IN',(implode(',',$org_id))); // see the change here
$this->db->from('company');
$query =$this->db->get();
$result = $query->result_array();
return $result;
}
now currently my query passes only one org_id , I want to pass all the org_id I get from my first query.
You can use where_in from active-records codeigniter
as
$devices = $this->activation_m->get_activated_devices();
$org_id = array();
foreach ($devices as $row)
{
$org_id[] = $row['org_id'];
}
$companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
if( $companys->num_rows() > 0 )
{
echo "<pre>";
print_r( $companys->result());
echo "</pre>";
}
And for Model
public function get_company($org_ids = array())
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
$this->db->where_in('company.id', $org_ids ); //this is condition
$this->db->from('company');
return $this->db->get();
}
You can use codeigniter's $this->db->or_where() for the purpose. Just traverse the array of organization id's and apply or_where conditions.
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
foreach($org_id as $org)
{ // where $org is the instance of one object of active record
$this->db->or_where('company.id',$org);
}
$this->db->from('company');
$query =$this->db->get();
$result = $query->result_array();
return $result;
Another way to do this is to make a custom query by traversing the array like this and appending where clauses in string.

Pass array in the CodeIgniter query

I want to pass an array in codeigniter query. Below is my code.
Controller
foreach($array as $values) {
$array_values = $values['download_subcategory_name'];
}
$this->data['get_downloads_content'] = $this->my_model->get_downloads_content($array_values );
Model
public function get_downloads_content($array_values ){
$this->db->select('*');
$this->db->from('my_table');
$this->db->where_in('download_subcategory_name', $array_values );
$this->db->order_by('download_id', 'ASC');
$query = $this->db->get();
return $query->result();
}
My problem is query showing results for only last value of array instead of all array values.
Please help.
Just need little changes within foreach
$array_values = array();
foreach($array as $values) {
$array_values[] = $values['download_subcategory_name'];
^^^
}
OR
$array_values = array();
foreach($array as $key => $values) {
$array_values[$key] = $values['download_subcategory_name'];
^^^^^^
}
Within your code it's storing only the last value from the foreach loop thats why it only gets the single value stored within it.

What is wrong with this PHP code?

I have two database tables, one for allowances and one for deductions. I want to calculate net salaries.
I'm using CodeIgniter. Here's my current code:
function get_allowances($eid)
{
$this->db->from('allowances');
$this->db->where('eid',$eid);
$query = $this->db->get();
if($query->num_rows()==1)
{
return $query->row();
}
else
{
//Get empty base parent object, as $item_id is NOT an item
$salary_obj=new stdClass();
//Get all the fields from items table
$fields = $this->db->list_fields('allowances');
foreach ($fields as $field)
{
$salary_obj->$field='';
}
return $salary_obj;
}
}
function get_deductions($eid)
{
$this->db->from('deductions');
$this->db->where('eid',$eid);
$query = $this->db->get();
if($query->num_rows()==1)
{
return $query->row();
}
else
{
//Get empty base parent object, as $item_id is NOT an item
$salary_obj=new stdClass();
//Get all the fields from items table
$fields = $this->db->list_fields('deductions');
foreach ($fields as $field)
{
$salary_obj->$field='';
}
return $salary_obj;
}
}
and in controller,
function net_salary($eid)
{
$allownces[] = $this->Salary->get_allowances($eid);
$deductions[] = $this->Salary->get_deductions($eid);
return $net_salary = array_sum($allownces) - array_sum($deductions);
}
My net_salary() function gives me a result of 0. What am I doing wrong, and how can I fix it?
Your models with plural names are only going to return a single object.
so what you are ending up with is...
Array
(
[0] => allowance_object
)
and
Array
(
[0] => deduction_object
)
While we really need the schema of your database try this (and make same edits for deductions)...
function get_allowances($eid)
{
$this->db->from('allowances');
$this->db->where('eid',$eid);
$query = $this->db->get();
if($query->num_rows()==1)
{
return $query->row_array(); //<--- return an Array
}
else
{
// make an array instead of object
$salary_obj = array();
//Get all the fields from items table
$fields = $this->db->list_fields('allowances');
foreach ($fields as $field)
{
$salary_array[$field] = 0; //<---- add array keys and set to integer 0 instead of empty string.
}
return $salary_array;
}
}
then in your net_salary function
function net_salary($eid)
{
$allownce = $this->Salary->get_allowances($eid);
$deduction = $this->Salary->get_deductions($eid);
return array_sum($allownce) - array_sum($deduction);
}
Try something like this:
function get_values($eid, $table_name)
{
$this->db->where('eid',$eid);
$query = $this->db->get($table_name);
$salary_obj = $query->result();
$values = array();
foreach($salary_obj as $row){
$values[] = $row->value_column_name;
}
return $values;
}
where value_column_name is the name of the table column (filedname) where the desired value stands.
call in controller:
function net_salary($eid)
{
$allownces = $this->Salary->get_values($eid, 'allowances');
$deductions = $this->Salary->get_values($eid, 'deductions');
return $net_salary = array_sum($allownces) - array_sum($deductions);
}

Categories