I am having 2 files, one to fetch ajax and to decode it. the other (page2.php) is modal popup in which I want to populate the received data from page1.php.
page1.php is below which returns invoice numbers perfectly.
$newArr = array();
$decoded = array();
$decoded = json_decode($result, true);
foreach($decoded ['invoices'] as $result) {
$newArr = $result['invoiceno'];
echo $newArr; //JUST FOR DEBUGGING
}
page2.php is a modal where I try to get the received data and populate into a dropdown.
function sort_(){
global $newArr;
$output='';
$output.= '<option value = '.$newArr.'>'.$newArr.'</option>';
return $output;
}
I know I am not looping the array to sort. I have tried different ways like below which didn't work.
function sort_(){
global $newArr;
global $result;
global $decoded;
$decoded= json_decode($result, true);
$output='';
$output.= '<option value = "Select INO">Select Select INO</option>';
foreach($decoded['invoices'] as $result) {
$output.= '<option value = "'.$newArr.'">"'.$newArr.'"</option>';
}
return $output;
}
The error that I am getting in developer console is;
<b>Warning</b>: Trying to access array offset on value of type null in <b>C:\xampp\htdocs\order\page2.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>: foreach() argument must be of type array|object, null given in <b>C:\xampp\htdocs\order\page2.php</b> on line <b>17</b><br />
where I am stuck?
Get the records from the DB (In case your data is not in form of a json string in the DB you don't need to decode)
Create the following functions :
function getOptions($acc, $record){
return $acc."<option value='".$record['your key']."'>".$record['your key']."</option>" ;
}
function createOptionsHtml($records) {
return array_reduce($records, 'getOptions') ;
}
Hope I got your problem right.
it was an easy fix.
page1.php had
global $decoded;
$decoded= array();
$decoded= json_decode($result, true);
page2.php
function sort_(){
global $decoded;
global $result;
$_invoiceReturn='';
$_invoiceReturn .= '<option value = "Select Invoice">Select Invoice</option>';
foreach($decoded['invoices'] as $result) {
$invoice_number = $result['invoice_number'];
$_invoiceReturn .= '<option value = "'.$invoice_number.'">'.$invoice_number.'</option>';
}
return $_invoiceReturn;
}
I am looping the invoices on page2.php based retrieving page1.php JSON. I decoded the json so it becomes a normal PHP array and looping through.
Related
I have an API which is returning some nested JSON data with multiple levels. My PHP code to loop through is below but I'm not getting any output:
$data = json_decode($output, true);
foreach($data as $item){
$title = $item->events->name->text;
echo $title;
}
An example of the data can be found here: http://i.imgur.com/Y55vl7n.png
I am trying to print the text name of each of the events (events->name->text)
There is a problem in your code, when you decode the json string, you use:
$data = json_decode($output, true);
It is converting everything to "array" (http://php.net/manual/en/function.json-decode.php), so you cannot access it like if they were objects.
You have to do:
foreach($data as $item){
$title = $item["events"]["name"]["text"];
echo $title;
}
Hope this helps!
I have the following problem. When i am trying to read some json data that are posted from an html page, i'm facing with the following error "Trying to get property of non-object on line".
Jquery script to create the json
var json = {"data":[]};
json.data.push({serialNumber: $serialNumber, xreosi: $xreosiToPost,
forma: $forma, apolia: $apolia});
Jquery for posting to php
$.post("page.php",{jsonData: JSON.stringify(json),
customer: $("#cusID").val()},function(data){});
PHP file
$json = json_decode($_POST['jsonData']);
foreach($json as $value){
$serialNumber = $value->serialNumber;
echo $serialNumber;
}
Thanks in advance.
Thereafter:
var json = {"data":[]};
json.data.push({serialNumber: $serialNumber, xreosi: $xreosiToPost,
forma: $forma, apolia: $apolia});
You have:
Object[data][0] = array('serialNumber' => ...);
Need:
$json = json_decode($_POST['jsonData'][0]);
or
$json = json_decode($_POST['jsonData']);
foreach($json as $row){
foreach($row as $value) {
$serialNumber = $value->serialNumber;
echo $serialNumber;
}
}
json_decode without second parameter returns result as php object. You have to pass true as second parameter. Also your data are in $json['data'], not $json:
$json = json_decode($_POST['jsonData'], true);
foreach($json['data'] as $value) {
$serialNumber = $value->serialNumber;
echo $serialNumber;
}
I'm using a php library from https://sourceforge.net/p/wowarmoryapi/home/Home/. It pulls json data from battle.net and stores it in my database for cache reasons.
code to get json from db (I'm not sure if it's still considered json at this point):
$data = $con->query('SELECT Data FROM wa_guilds');
I use the following code to see the data:
foreach($data as $row) {
echo "<span style='color:#ff0099'>";
var_dump($row);
echo "</span>"; }
which looks like this minus the errors, that's from another code:
I've tried various methods, mostly from this site to show my data in a way that's easy to read but I always get error.
This is definitely an object. if (is_object($data)) { echo "yay!"; } <--this works
Once I use $decodedjson = json_decode($data); it's no longer an object and I can't seem to print the results to see what it looks like. var_dump($decodedjson) returns NULL
Finally, when I use the following code:
foreach ($data as $da){
echo $da['Data']['character']['name']; }
returns Warning: Illegal string offset 'character'
and:
foreach ($data as $da){
echo $da['character']['name']; }
returns Notice: Undefined index: character
I don't understand what I'd doing wrong, or right. Do I need to somehow turn $data into a string?
NEW CODE
$sth = $con->query('SELECT Data FROM wa_guilds');
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($data as $row) {
foreach($row as $r) {
$myData = json_decode($r, true);
echo "<span style='color:#ff0099'>";
var_dump($myData['Data']);
echo "</span>"; } }
NEW ERROR
NULL NULL
From the warning I'm guessing you're using PDO. If $con is your PDO instance representing a connection to a database, try the following:
$sth = $con->prepare('SELECT Data FROM wa_guilds');
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($data as $row) {
$myData = json_decode($row['Data'], true);
echo "<span style='color:#ff0099'>";
// $myData should now be a PHP array which you can access easily
print_r($myData);
echo "</span>";
}
You will need to convert the json string first, I'm not sure how many rows you are expecting from the DB but if it's only one you don't need the loop:
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
$decoded = json_decode($data['Data'], true);
echo "<span style='color:#ff0099'>";
var_dump($decoded);
echo "</span>";
if you need a loop it should work like this:
foreach($data as $d)
{
$decoded = json_decode($d['Data'], true);
echo "<span style='color:#ff0099'>";
var_dump($decoded);
echo "</span>";
}
I can't retrieve values from $info (as stated below) in CodeIgniter View.
Here is the scenario:
I explained everything the code.
function info() {
{...} //I retrieve results from database after sending $uid to model.
$dbresults = $this->my_model->get_info($uid); //Assume that this model returns some value.
foreach($dbresults as $row) {
$info = $row->address; //This is what I need to produce the results
$results = $this->my_model->show_info($info);
return $results; //This is my final result which can't be achieved without using $row->address. so first I have to call this in my controller.
}
// Now I want to pass it to a view
$data['info'] = $results;
$this->load->view('my_view', $data);
//In my_view, $info contains many values inherited from $results which I need to call one by one by using foreach. But I can't use $info with foreach because it is an Invalid Parameter as it says in an error.
using $result inside foreach is not reasonable. Because in each loop $result will take a new value. So, preferably use it as an array and then pass it to your view. Besides, you should not use return inside foreach.
function info() {
{...} //I retrieve results from database after sending $uid to model.
$dbresults = $this->my_model->get_info($uid); //Assume that this model returns some value
$result = array();
foreach($dbresults as $row) {
$info = $row->address; //This is what I need to produce the results
$result[] = $this->my_model->show_info($info);
}
// Now I want to pass it to a view
$data['info'] = $result;
$this->load->view('my_view', $data);
}
to check what $result array has do var_export($result); or var_dump($result); after the end of foreach. and make sure that this is what you want to send to your view.
Now, in your view you can do:
<?php foreach ($info as $something):?>
//process
<?php endforeach;?>
Please remove return statement from
foreach($dbresults as $row) {
$info = $row->address; //This is what I need to produce the results
$results[] = $this->my_model->show_info($info);
// return $results; remove this line from here;
}
$data['info'] = $results; // now in view access by $info in foreach
$this->load->view('my_view', $data);
now $info can be access in view.
hope this will help you!
original I thought the $query is array object, since I can print these data to a list table like this.
my controller code
$data['dates'] = $this->calendar_model->get_cal_eventDate();
$this->load->library('table');
$this->load->view('my_test', $data);
my view code
echo $this->table->generate($dates);
but when I changed my code to try to print $tbDataArr via foreach. It didn't work. How can I convert the $query result (eventDate field values) to array object.
function get_cal_eventDate() {
$this->db->select('eventDate');
$query = $this->db->get('tb_event_calendar');
return $query;
}
$tbDataArr = $this->calendar_model->get_cal_eventDate();
foreach ($tbDataArr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Are you using CodeIgniter? If you are you can do this
function get_cal_eventDate() {
$this->db->select('eventDate');
$query = $this->db->get('tb_event_calendar');
$result = $query->result_array();
return $result;
}
If not need more info about what your doing with your PHP.
Codeigniter, right?
Use the $query->result_array() method.
see more here:
http://codeigniter.com/user_guide/database/results.html
//Convert $query->result_array() to $query->result() after add new field in array
$result = $query->result_array();
for($i=0;$i<$query->num_rows();$i++){
//insert new field in array (test_field with value 1)
$result[$i]+=array('test_field'=>1);
$result[$i] = (object)$result[$i];
}
return $result; //equivalent to return $query->result() without new field;