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;
}
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.
$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);
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);
I have an sql query which outputs an array the output looks like this
Array
(
[0] => Array
(
[customer_id] => 7
[language_id] => 1
[variableitem_id] => 13
[name] => QUESTION_HEADLINE
[value] => Bitte geben Sie Ihren Downloadkey ein:
)
[1] => Array
(
[customer_id] => 7
[language_id] => 1
[variableitem_id] => 15
[name] => QUESTION_BUTTON
[value] => Start!
)
[2] => Array
(
[customer_id] => 7
[language_id] => 1
[variableitem_id] => 6
[name] => PAGETITLE
[value] => Steigenberger Hotels and Resorts - Mediathek
)
)
In my controller I get it as
$data['variables_data'] = $this->Home_model->getVariables($customer_id, $language_id);
Now for different ids in the url like for
localhost/home/user/12 and localhost/home/user/14
the positions of the variable differs
for example in my view when I echo
$variable[0]['value']
it gives QUESTION_HEADLINE for one user and PAGE_TITLE for the other .
Is it possible to make them same for all of the user like if I echo
$variable[0]['value']
it should return me QUESTION_HEADLINE every time and for every user
Code for Home model get_variables function
function getVariables($customer_id, $language_id) {
$query = $this->db->query("SELECT customers_idcustomers AS customer_id,
languages_idlanguages AS language_id,
variableitems_idvariableitems AS variableitem_id,
variableitem AS name,
variabletext AS value
FROM variables v
LEFT JOIN variableitems vi ON v.variableitems_idvariableitems = vi.idvariableitems
WHERE v.customers_idcustomers ='" . $customer_id . "'
AND v.languages_idlanguages =" . $language_id
);
$var = $query->result_array();
return $var;
}
Thanks in advance
You can set it explicitly, like
foreach($outArray as $output)
{
$output['name']="Any thing you want";
}
So , as i understand you want to make reduce your output to one dimension. It can be done with MYSQL also. Here php version:
In your controller
$data = $this->Home_model->getVariables($customer_id, $language_id);
$data['variables'] = array(
'customer_id' => $data[0]['customer_id'],
'language_id' => $data[0]['language_id'],
'variableitem_id' => array(),
'name' => array(),
'value' => array()
);
foreach($data as $k => $v) {
$data['variables']['variableitem_id'][] = $v['variableitem_id'];
$data['variables']['name'][] = $v['name'];
$data['variables']['value'][] = $v['value'];
}
And in your view
echo $variables['value'][2].' '.$variables['value'][3];
//do whatever you want