get value from json php codeigniter - codeigniter

I have a json object like like this
[
[
{
"class":"com.ehealth.data.Sample_Data",
"collectedBy":"2013-07-21",
"collectedDate":"Kamal",
"orderID":2,
"sampleID":2.897033553E9
},
{
"class":"com.ehealth.data.Order_Data",
"doctorUsername":"Kamal",
"dueDate":"2014-01-02",
"orderDate":"2013-12-12",
"orderID":2,
"patientID":"P0001",
"prority":1,
"status":"complete",
"testType":"Fasting Blood Sugar"
}
],
[
{
"class":"com.ehealth.data.Sample_Data",
"collectedBy":"2013-07-22",
"collectedDate":"Kamal",
"orderID":3,
"sampleID":5.978956192E9
},
{
"class":"com.ehealth.data.Order_Data",
"doctorUsername":"Kamal",
"dueDate":"2014-01-02",
"orderDate":"2013-12-12",
"orderID":3,
"patientID":"P0001",
"prority":2,
"status":"complete",
"testType":"Fasting Blood Sugar"
}
]
]
and i decode it to php array
$data['query'] = json_decode($curl_response);
i try to access valuse like this
foreach ($query as $row) {
echo $row->sampleID;
}
but when i am going to access the the values inside it i can not access i get a error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
can anybody help me. i dont know what to do

You can get it:
foreach ($query as $row) {
echo $row[0]->sampleID;
}

<?php
$str = '[[{"class":"com.ehealth.data.Sample_Data","collectedBy":"2013-07-21","collectedDate":"Kamal","orderID":2,"sampleID":2.897033553E9},{"class":"com.ehealth.data.Order_Data","doctorUsername":"Kamal","dueDate":"2014-01-02","orderDate":"2013-12-12","orderID":2,"patientID":"P0001","prority":1,"status":"complete","testType":"Fasting Blood Sugar"}],[{"class":"com.ehealth.data.Sample_Data","collectedBy":"2013-07-22","collectedDate":"Kamal","orderID":3,"sampleID":5.978956192E9},{"class":"com.ehealth.data.Order_Data","doctorUsername":"Kamal","dueDate":"2014-01-02","orderDate":"2013-12-12","orderID":3,"patientID":"P0001","prority":2,"status":"complete","testType":"Fasting Blood Sugar"}]]';
$json = json_decode($str,true);
foreach ($json as $datas){
foreach ($datas as $data){
echo (isset($data['sampleID']) ? $data['sampleID'] :'')."<br/>";
}
}
echo '<pre>';
print_r ($json);
echo '</pre>';
// exit;
?>

Related

Laravel Passport doesn't bring results in foreach loop sometimes

I'm trying to get data with laravel passport API, which returns null for some IDs in a foreach loop. But if I pick that Id and try to get individual results, it works fine. When we have 50 or more records, it starts to create problems.
Code Sample:
public function name()
{
set_time_limit(0);
ini_set('memory_limit', '-1');
try{
$all_product_prices = SProductNcrPadPrice::select('id', 'product_id', 'sku')->get();
if (!empty($all_product_prices)) {
foreach ($all_product_prices as $key => $row_signal_product_price) {
$ncr_token = $this->createNcrPadsToken();
$response_get_list = Http::withToken($ncr_token)->get($this->base_url.'/product/get_list?queryString={"product_id": "'.$row_signal_product_price->sku.'"}');
$products_list_get = json_decode($response_get_list->body(), true);
info($products_list_get);
if(!empty($products_list_get["data"]) && count($products_list_get["data"]) > 0){
echo "<pre>";
echo "In";
}else{
echo "<pre>";
echo "Out";
}
}
}
}catch(HttpEmptyResponse $e)
{
abort(500, 'contact support');
}
}

codeigniter how to show data in functions

My model:
function get_data($id)
{
$this->db->select('id, Company, JobTitle');
$this->db->from('client_list');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->result();
}
I want to get the the data from get_data(), is this the right way?
public function show_data( $id )
{
$data = $this->get_data($id);
echo '<tr>';
echo '<td>'.$data['Company'].'</td>';
echo '<td>'.$data['JobTitle'].'</td>';
echo '<td>'.$data['id'].'</td>';
echo '<td></td>';
echo '</tr>';
}
Use the row_array() function to get the data in the array format.
Reference url
http://ellislab.com/codeigniter/user-guide/database/results.html
you can use foreach loop to print
foreach ($data->result() as $row)
{
echo '<tr>';
echo '<td>'.$row['Company'].'</td>';
echo '<td>'.$row['JobTitle'].'</td>';
echo '<td>'.$row['id'].'</td>';
echo '<td></td>';
echo '</tr>';
}
thank you
just to improve answer, I use "general_model" for all my controllers, there are some exceptions where I need special queries, I just create desired model so "general_model" stays same and I can use it in any project.
e.g.
general_model.php
function _getWhere($table = 'table', $select = 'id, name', $where = array()) {
$this->db->select($select);
$q = $this->db->get_where('`'.$table.'`', $where);
return ($q->num_rows() > 0) ? $q->result() : FALSE;
}
.
.
.
//bunch of another functions
in controller I just call
$this->data['books'] = $this->general_model->_getWhere('book', '*', array('active' => '1'));
$this->render('book_list_view'); // $this->load->view('book_list_view', $this->data);
sidenote: I am extending CI_Controller therefore I use $this->data['books'] instead $data['books'] to pass data into view
in view
//check if there are any data
if ($books === FALSE) {
//some error that there are no books yet
} else {
//load data to table or something
foreach ($books as $book) {
$book->id; // book id
}
}

Codeigniter Printing Query Results

Simple question but I don't know why it is not printing correctly or working as expected. I have this in model (works because when I print_r($result) it shows data:
function get_latest_entries()
{
$query = $this->db->get('property');
return $query->result_array();
}
And for controller:
public function index()
{
$resultSet['properties'] = $this->propertymodel->get_latest_entries();
$this->load->view('home', $resultSet);
}
In the view I want to iterate over the array (the table has description, city, address columns):
<?php
foreach($resultSet as $item)
{
echo $item->city;
echo $item->description;
}
?>
I am getting two records on home page where am displaying the results as above:
Severity: Notice
Message: Undefined variable: resultSet
Filename: views/home.php
Line Number: 16
And
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/home.php
Line Number: 16
use $properties instead of $resultSet
use this... you are passing $properties to your view and not $resultSet..
<?php
foreach($properties as $item) // use properties
{
echo $item->city;
echo $item->description;
}
?>
Here is the problem in your code.
-you have returned data as array of arrays and you are trying to access as an object,
-and the second one is,you have passed properties variable to view and you are accessing resultSet.
-so here is your code in view which contains errors
<?php
foreach($resultSet as $item)
{
echo $item->city;
echo $item->description;
}
?>
-And the Correct version of your code is here...
<?php
foreach($properties as $item)
{
echo $item['city'];
echo $item['description'];
}
?>

accessing array in array data pass from controller to view of codeigniter

I have this following code in my controller. I want to print the data i have my array.. should it be double forloop or foreach?
CONTROLLER:
public function index()
{
$in_cart = array();
if (!isset($_SESSION['cartProducts'])){
$in_cart['list'] = "No Products";
}
else{
foreach ($_SESSION['cartProducts'] as $key => $value) {
$in_cart[$key] = $this->shopmod->get_one_data($key);
}
$cart['list'] = $in_cart;
}
$this->load->vars($cart);
$data['cart'] = $this->load->view('shop/cart', '', TRUE);
$this->load->view('layout/default', $data);
}
VIEW:
<?php if(is_array($list)): ?>
<?php foreach($list as $row):?>
<tr>
<td><?=$row->name?></td>
</tr>
<?php endforeach ?>
<?php endif;?>
but i have this following error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: shop/cart.php
Line Number: 18
anyhelp guys? :(
I see you are using Code igniter (nice!)
I would do the following to ensure the $key is taking 'name'
public function index() {
foreach ($_SESSION['cartProducts'] as $key => $value) {
echo $key;
} }
Then
public function index() {
$in_cart = array();
if (!isset($_SESSION['cartProducts'])){
$in_cart['list'] = "No Products";
}
else{
foreach ($_SESSION['cartProducts'] as $key => $value) {
$in_cart[$key] = $this->shopmod->get_one_data($key);
}
}
$this->load->vars($cart);
$data['cart'] = $this->load->view('shop/cart', '', TRUE);
$data['list'] = $in_cart;
$this->load->view('layout/default', $data); }
PHP is telling you exactly what is wrong.
You are trying to access the name property of the $row array when the -> syntax is specifically reserved for objects.
To access array values, you use square brackets and keys: <?=$row['name']?>
Helpful hint to understand - What you're doing is quasi-equivalent to: 7->name, insofar as the left value (7) has no idea how to use the arrow syntax. It's reserved for when the left value is an object. Not an integer, or in your case, an array.
Update after your comment:
You would get at the data like this:
<? foreach($row['list'] as $r):?>
<tr>
<td><?=$r[0]->name;?></td>
</tr>
<? endforeach;?>

I have a question about using form_dropdown()

I have a question about using form_dropdown().
table:category
cat_id
cat_name
Controller:
function index()
{
$this->load->model('category_model');
$data['category'] = $this->category_model->cat_getallcat();
$this->load->view('category_input',$data);
}
Model:category_model
function cat_getallcat()
{
$this->load->database();
return $this->db->get('category')->result();
}
View:
<?php
$this->load->helper('form');
echo form_open('send');
$list[''] = 'Please select a Category';
foreach($query as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}
echo form_dropdown('category', $list);
echo form_close();
?>
error obtained:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/category_input.php
Line Number: 28
the ->result() will return the first row only, so this function is what you want to loop over.
Model
function cat_getallcat()
{
$this->load->database();
return $this->db->get('category');
}
View
foreach($query->result() as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}
EDIT:
Also, you are sending the result as $data['category'] then trying to access it as $query. So really the foreach would be foreach($category->result() as $row) in this example
You are asking foreach to loop over $query, but I you haven't set $query as a variable anywhere that I can see.
Since you set $data['category'] to hold your query result() in your controller, you need to loop over $category in the view:
foreach($category as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}

Resources