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.
Related
$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);
Trying to get data from an API and still use the Paginate but it does not work.
I get this error:
Call to a member function links() on array
This is my API controller that where i get the data from:
$positions = DB::table('position')
->join('company', 'position.company_id', '=', 'company.id')
->select('position.*', 'company.name')
->paginate(5);
if (!$positions) {
return response()->json(['message' =>'There is no positions', 'code' =>404], 404);
} else {
//echo "hit bby first";
return response()->json(['data' => $positions], 200);
}
This is the controller which gets data from the API through JSON:
$jsonurl = "http://localhost/laravel.dev/index.php/positions";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
$newarray = json_decode(json_encode($json_output), True);
$data = array(
'positions' => $newarray['data'],
);
return view('mypage', $data);
This is in my view mypage where i loop through the code
#foreach($positions['data'] as $position)
{{$position['title']}}
{{$position['location']}}
#endforeach
{{positions->links()}}
And this is what the array looks like when i try to echo it in the view:
Array
(
[total] => 14
[per_page] => 5
[current_page] => 1
[last_page] => 3
[next_page_url] => http://localhost/laravel.dev/index.php/positions?page=2
[prev_page_url] =>
[from] => 1
[to] => 5
[data] => Array
(
[0] => Array
(
[id] => 1
[company_id] => 1
[title] => Software Developer
)
[1] => Array
(
[id] => 2
[company_id] => 2
[title] => Accountant
)
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 am querying my mysql-db using PDO, with a plain "SELECT * FROM Invoices" query. Now all columns of type "bit" gets a blank value if i do print_r on the result from the query
Array
(
[0] => stdClass Object
(
[ID] => 1
[Client] => 1
[Title] => Mars 2012
[Issued] => 2012-04-02
[Expiration] => 2012-04-22
[OurReference] => 1
[TheirReference] =>
[Payed] =>
[Sent] =>
)
[1] => stdClass Object
(
[ID] => 2
[Client] => 4
[Title] => Apputveckling
[Issued] => 2012-04-30
[Expiration] => 2012-05-21
[OurReference] => 1
[TheirReference] =>
[Payed] =>
[Sent] =>
)
[2] => stdClass Object
(
[ID] => 3
[Client] => 4
[Title] => Administration
[Issued] => 2012-05-28
[Expiration] => 2012-06-18
[OurReference] => 1
[TheirReference] =>
[Payed] =>
[Sent] =>
)
)
How can that be?
This is the getData-method im using
public function getData($sql, $data = null)
{
$statement = $this->query($sql, $data);
try
{
$result = $statement->fetchAll();
}
catch(Exception $e)
{
echo "Error: " . $e->getMessage() . " ";
return array();
}
if(count($result) > 1)
{
return $result;
}
else if(count($result) == 1)
{
return $result[0];
}
else
{
return array();
}
}
EDIT: Forgot to mention, the rows do have values in the fields in the db
The bit fields are mapped to booleans. A boolean false will be printed by print_r in exactly the way you see in your question. Here is a test script showing that:
$a = array(new stdClass());
$a[0]->Payed = false;
print_r($a);
Use var_dump instead and you should be able to see both the datatype of each property as well as the boolean content.
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