$this->db->select("*");
$this->db->from("panTab");
$this->db->where("QCJobPanelTestId = ",$TestId);
$query = $this->db->get();
$data['get_JobPan_Data'] = $query->result();
for($i = 0; $i < sizeof($data['get_JobPan_Data']);$i++)
{
$table = "Form1";
$whereField1 = "QC1TestId";
$whereField2 = "QC1JobPanelId";
$currDiv = "form1";
$this->db->select("*");
$this->db->from($table);
$this->db->where($whereField1." = ",$TestId);
$this->db->where($whereField2." = ",$data['get_JobPan_Data'][$i]['QCJobPanelId']); //--This value gives an error . How to access it?
$query1 = $this->db->get();
$data['getTestData'] = $query1->result();
}
Data that is coming
Array (
[get_JobPan_Data] => Array (
[0] => stdClass Object(
[QCJobPanelId] => 293 [QCJobPanelNo] => 1
[QCJobPanelDesc] => Pan 1
[QCJobPanelJobId] => 3
[QCJobPanelPanelId] => 0
[QCJobPanelTestId] => 63
)
[getTestData] => Array (
[0] => stdClass Object (
[QC1Id] => 77
[QC1JobId] => 3
[QC1TestId] => 63
[QCTestDesc] => 0
[QC1DielectricACC_LC] => 0
[QC1DielectricACC_IRA] => 0
[QCRemark] => Completed
[QCTestedBy] => aa
[QCReviewedBy] => bb
[QCWitnessedBy] => cc
[QC1JobPanelId] => 293
[QCTestCompletionDate] => 2016-07-29 00:00:00
[QC1DateAdded] => 2016-07-29
)
)
)
Also unable to get value from $data['getTestData']:
Tried with :
$data['getTestData'][0]['TestDesc']
$data['getTestData'][0][0]['TestDesc']
$data['getTestData']['TestDesc']
Answer to access $data including help from #Anish & #Anant :
$data['getTestData'][0]->TestDesc
ResultVariable[Array1][Array2]->Object variable
In short:
- If Array, then use []
- If Object, then use ->
yes there is mistake to access object
$this->db->where($whereField2." = ",$data['get_JobPan_Data'][$i]['QCJobPanelId']);
replace it with
$this->db->where($whereField2." = ",$data['get_JobPan_Data'][$i]->QCJobPanelId);
Related
I'm trying to batch update a table by an ID
My controller:
if(customCompute($this->data['student'])) {
$studentextendID = $this->data['student']->studentextendID;
$this->data['students'] = $this->studentextend_m->get_studentextend(array('studentextendID' => $studentextendID));
} else {
$this->data['students'] = [];
}
$post = $this->input->post();
for($i=0; $i < count($post['subject']); $i++) {
$studentExtendArray[] = array(
'studentextendID' => $studentextendID,
'studentID' => $studentID,
'subject' => $post['subject'][$i],
'subjectng' => $post['subjectng'][$i],
'subjectlg' => $post['subjectlg'][$i],
'subjectcre' => $post['subjectcre'][$i],
);
$this->db->update_batch('studentextend', $studentExtendArray, 'studentextendID');
}
My Model
function get_studentextend($array=NULL, $signal=FALSE) {
$query = parent::get($array, $signal);
return $query;
}
Array Output:
Array (
[0] => Array
(
[studentextendID] => 143
[studentID] => 97
[subject] =>
[subjectng] => 5235
[subjectlg] => 5231
[subjectcre] => 523155
)
[1] => Array
(
[studentextendID] => 143
[studentID] => 97
[subject] =>
[subjectng] => 2
[subjectlg] => 99
[subjectcre] => 3
) )
As you can see, 'studentextendID' is duplicated on both arrays, when it should be dynamically obtained, for example: 143 and 144, because there are two rows in the table with the same 'studentID'
You can try with change in model
public function update_batchs($table, $data)
{
if ($data) {
return $this->db->update_batch($table, $data, 'studentextendID');
}
}
You are not pass the $studentextendID in for loop.You need to pass this in for loop.
$studentextendID = $this->data['student']->studentextendID;
And also please verify $this->data['student'].
How can I extract the field: chat_id from this array? I'm trying to put the chat_id in the id of a button in html.
Array
(
[0] => stdClass Object
(
[id] => 2
[first_name] => Armani
[last_name] => Dee
[contact_number] => 123456
[email_address] => hello#gmail.com
[home_address] =>
[username] => armani
[password] => 12345
[status] => 0
[email_confirmed] => 0
[chat_id] => 14
[admin_id] => 0
[customer_id] => 2
[partner_id] => 0
[timestamp] => 2018-06-15 22:38:24
[chat_exists] => 1
[reply_id] => 208
[message] => Hello
[timestamp_reply] => 2018-06-16 14:02:44
[chat_status] => 0
)
)
I got my output from my model:
public function chat_customer($enum)
{
$this->db->select('*');
$this->db->from('customer');
$this->db->order_by('timestamp','desc');
$this->db->where('customer.id',$enum);
$this->db->join('chat','chat.customer_id = customer.id','left');
$this->db->join('chat_reply', 'chat_reply.chat_id = chat.chat_id', 'left');
$query = $this->db->get();
return $query->result();
}
Then I retrieved it in my controller like this:
$customer_id = $this->input->get('message');
$this->load->model('Crud_model');
$data = $this->Crud_model->chat_customer($customer_id);
echo print_r($data);
I'm trying to print the 14 on the chat_id.
Thank you for your help!
Hope this will help you :
Return data with $query->row(); if it always return single row
Your model should be like this :
public function chat_customer($enum)
{
$this->db->select('*');
$this->db->from('customer');
$this->db->order_by('timestamp','desc');
$this->db->where('customer.id',$enum);
$this->db->join('chat','chat.customer_id = customer.id','left');
$this->db->join('chat_reply', 'chat_reply.chat_id = chat.chat_id', 'left');
$query = $this->db->get();
return $query->row();
}
In your controller :
$customer_id = $this->input->get('message');
$this->load->model('Crud_model');
$data = $this->Crud_model->chat_customer($customer_id);
echo $data->chat_id;
I would suggest to use a foreach loop if you are going to get multiple rows in your query.
foreach($data as $d){
echo $d['column_name'];
}
This will make sure you iterate over all the rows.
In my code, I am getting an array like below
Array
(
[12247] => stdClass Object
(
[wid] => 12247
[uid] => 1626
[timestamp] => 1482021161
[weight_value] => 63.9
[nid] => 1195487
[source] => 0
[weight_entered] =>
)
[34843] => stdClass Object
(
[wid] => 34843
[uid] => 1632
[timestamp] => 1485298453
[weight_value] => 72.5
[nid] => 1206019
[source] => 8176
[weight_entered] =>
)
[35316] => stdClass Object
(
[wid] => 35316
[uid] => 1632
[timestamp] => 1485723529
[weight_value] => 54.2
[nid] => 1209357
[source] => 0
[weight_entered] =>
)
[35177] => stdClass Object
(
[wid] => 35177
[uid] => 1632
[timestamp] => 1485559176
[weight_value] => 53.3
[nid] => 1209357
[source] => 0
[weight_entered] =>
)
[12248] => stdClass Object
(
[wid] => 12248
[uid] => 1633
[timestamp] => 1481662016
[weight_value] => 56.6
[nid] => 1209357
[source] => 0
[weight_entered] =>
)
[12249] => stdClass Object
(
[wid] => 12249
[uid] => 1635
[timestamp] => 1479839680
[weight_value] => 54
[nid] => 1209357
[source] => 0
[weight_entered] =>
)
)
The array is order by the nid, timestamp, wid in descending order. I need to get an output array which will contain only the latest, second latest and the first weight values with the nid as an index. So that I will get the details of a particular nid only.
The output which I think about is like below and the logic behind that is
These multidimensional arrays have a value nid. I need to get the
latest, second latest and the first weight_value from a particular
nid. As the array is already sorted in descending order of time, I
just need to fetch the first, second and last values from each inner
array having common nid
Array
(
[1195487] => stdClass Object
(
[latest_weight] => 63.9
[second_latest_weight] =>
[first_weight] =>
)
[1206019] => stdClass Object
(
[latest_weight] => 72.5
[second_latest_weight] =>
[first_weight] =>
)
[1209357] => stdClass Object
(
[latest_weight] => 54.2
[second_latest_weight] => 53.3
[first_weight] => 54
)
)
I tried the code, but I am stuck up by not getting the proper logic to apply. Please help.
The code which I am trying is given below. But it is not full and not correct too.
if(!empty($history_details)){
$last_weight = 0;
$second_last_weight = 0;
$first_weight = 0;
$prev_nid = '';
$prev_wid = '';
$prev_count = 1;
foreach($history_details as $single_history){
if(empty($weight_array[$single_history -> nid]['field_weight_format'])){
$current_weight = $weight_array[$single_history -> nid]['field_weight_value']." lbs";
}
else{
$current_weight = str_replace('-', ' ', $weight_array[$single_history -> nid]['field_weight_format']);
}
if($prev_nid == '' || $prev_nid != $single_history -> nid){
$last_weight = $single_history -> weight_value;
if($prev_nid != $single_history -> nid){
$prev_count = 1;
}
}
else if($prev_count === 2){
$second_last_weight = $single_history -> weight_value;
$first_weight = $single_history -> weight_value;
}
else if($prev_count > 2 && $prev_nid != $single_history -> nid){
$first_weight = $history_details[$prev_wid] -> weight_value;
}
$prev_count++;
$prev_nid = $single_history -> nid;
$prev_wid = $single_history -> wid;
}
}
You can just sort by the weight within a group I guess.
function findInGroup($outerArray, $targetGroup) {
$array = array_filter($outerArray, function ($item) use ($targetGroup) {
return $item->nid == $targetGroup;
});
uasort($array,function ($a,$b) {
return $a->weight_value<=>$b->weight_value;
});
$first = reset($array);
$second = next($array);
$last = end($array);
return [ $first, $second, $last ];
}
Then you can do:
findInGroup($history_details,1209357);
Or you can do it for all groups via:
$keys = array_unique(array_map(function ($item) {
return $item->nid;
}, $history_details); //All distinct keys
$pluckedValues = array_map(function ($id) use ($history_details) {
return findInGroup($history_details, $id);
}, $keys); //Just the values for each distinct key
$values = array_combine($keys, $pluckedValues); //Combined with the key.
Of course this is not an optimal solution but would be a place to start.
Query run is based on 2 id, however I get one output value
Input:
public function get_place_order_category($id)
{
/* $id = 1,2;*/
$ids = explode(',',$id);
foreach($ids as $catid)
{
$this->db->where('product_id =',trim($catid));
$query = $this->db->get('products');
return $query->result_array();
}
}
Output:
Array
(
[0] => Array
(
[product_id] => 12
[product_name] => Product1
[product_code] => pro12345
[product_price] => 200.00
[product_newprice] => 0.00
[size] => 0-6M , 0-9M ,9-12M
[product_front] => 1446200664.JPG
[product_back] => 14462006641.JPG
[product_left] => 14462006642.JPG
[product_right] => 14462006643.JPG
[product_description] =>
[styling_tips] =>
[category_id] => 62
[user_id] => 5
[status] => 1
[parent_id] => 42
[qty] => 4
[promo_id] => 0
[etc4] => 0
[etc5] => 0
)
)
You can use one global array and add result arrays to it. Try this:
public function get_place_order_category($id)
{
/* $id = 1,2;*/
//first test here what comes in $id
echo $id."<br/>";
$ans_arr = array();
$ids = explode(',',$id);
/*test here too whats actually happens after explode weather
arrayhas generated and what are the element of it */
echo "<pre>";print_r($ids);echo "<br/>";
foreach($ids as $catid)
{
$this->db->where('product_id =',trim($catid));
$query = $this->db->get('products');
$ans_arr[] = $query->result_array();
}
return $ans_arr;
}
I have an array of objects.
What I need is to take each [name] of each object in put into another array, but I don't want duplicates.
How can I do it?
Array (
[0] => ADOFetchObj Object
(
[name] => Team 1
[att] => None
[idGrupo] => 1
[idModulo] => 4
[ler] => 1
[escrever] => 1
[excluir] => 1
)
[1] => ADOFetchObj Object
(
[name] => Team 1
[nomeModulo] => Aplicar Juros
[idGrupo] => 1
[idModulo] => 1006
[ler] => 1
[escrever] => 1
[excluir] => 1
)
[2] => ADOFetchObj Object
(
[name] => Team 2
[att] => None
[idGrupo] => 1
[idModulo] => 10
[ler] => 1
[escrever] => 1
[excluir] => 1
)
[3] => ADOFetchObj Object
(
[name] => Team 2
[att] => None
[idGrupo] => 1
[idModulo] => 1012
[ler] => 1
[escrever] => 1
[excluir] => 1
)
)
Thanks!
You can do this:
$names = array();
foreach($arr as $list) {
$names[$list->name] = true; // can be *any* arbitrary value
}
$names = array_keys($names);
This will work because by definition array keys have to be unique.
array_unique(array_map(function($element) {
return $element->name;
}, $my_array));
There you go
$res = array();
foreach($arr as $var)
{
if(!in_array($var->name, $res))
{
$res[] = $var->name;
}
}
First, copy the names to a new array:
$arrNames = array();
foreach($arrOriginal as $objObject) {
array_push(
$arrNames,
$objObject->name
);
}
Then remove the duplicate names:
$arrNames = array_unique($arrNames);
$n = array();
foreach($array as $d) $n[] = $d->name;
$n = array_unique($n);