Getting error On data Fetch - php

I fetched data and fill the value inside the table with for each loop like:
<?php
foreach ($data['rows'] as $value) {
?>
<tr>
<td> <?php echo $value->rsdntname; ?> </td>
<td> <?php echo $value->rsdntemail;?></td>
<td> <?php echo $value->rsdntphone; ?> </td>
</tr>
<?php } ?>
but in top section i have three columns which also filled with the fetch data like:
<h3 class="form-section">Building
<span class="pull-right" style="font-size:15px;color:#000;">
Core <?php $data['rows']->aptcore; ?>,
Floor,
Apartment No
</span>
</h3>
But it is returning me this error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: admin/page.php
Line Number: 52
Why I am getting this error .. Provide me the solution Not able to understand.and give me the solution i am not able to understand .It saying tryng to access non object y this error returning with that. what i am doing wrong there
working with same page to access data for another level with loop its working outside the loop its not working

You're trying to read a property from an object:
$data['rows']->aptcore;
But the error is telling you that it's not an object. So what is it? This previous line of code implies to me that it's probably an array:
foreach ($data['rows'] as $value) {
Because you're looping over the array and reading properties from each object therein.
In a comment elsewhere on this page you specify some debugging output (print_r($data);):
Array (
[res] => 1
[rows] => Array (
[0] => stdClass Object (
[buldname] => BT Tower
[aptno] => 901
[aptcore] => 2
[aptfloor] => 2
[rsdntname] => Gaurav
[rsdntemail] => Gaurav#gmail.com
[rsdntphone] => 9891110987 )
(formatting mine)
If I understand that output correctly, $data is an associative array with two named elements, res and rows. rows is itself also an array containing one object. (Or does it contain more that you're just not showing us?) An array of one object is still an array, not an object. (A basket containing one apple is still a basket, not an apple.)
You could try to index the array to access the first element therein:
$data['rows'][0]->aptcore
Or if there are more elements and you want all of them, you can loop over the array exactly like you already do:
foreach ($data['rows'] as $value) {
echo $value->aptcore;
}

You are calling $value as if it is an object, but more than likely your $value is actually a nested array. Assuming your data in $data['rows'] are arrays then instead of using $value->rsdntname you should call it as $value['rsdntname']... and so on for each $value.

Related

Stuck in echoing data out of variable CodeIgniter

I try to get some information out of my database to my webpage. Everything seems to be fine but there is one thing that doesn't want to go right. I put all my information out my database into $data. When i do this
print_r($data);
My webpage gives me this:
(
[0] => stdClass Object
(
[reparatie_id] => 19
[customer_id] => 4
[medewerker] => 4
[name] => Joost
)
)
Everything seems to be good but when i try to do this:
echo $data->voornaam;
I keep getting this error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: reparaties/cases.php
Line Number: 7
Backtrace:
File: C:\Ampps\www\beco\application\views\reparaties\cases.php
Line: 7
Function: _error_handler
File: C:\Ampps\www\beco\application\controllers\Reparaties.php
Line: 57
Function: view
File: C:\Ampps\www\beco\public\index.php
Line: 315
Function: require_once
Since your $data is a single-dimensional-array,so it need to be-
$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name;//so on for other indexes
Actually the $data array has an object at 0 position. So you need to any property of object. do like this:
<?php
$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name; ?>
output will be:
19, 4, 4, joost
First of all, your $data array does not have the voornaam element in it. So, assuming you want to echo out the elements that are inside the array, you would use the foreach array, like this:
foreach($data as $value) {
echo $value->name;
echo $value->voorname; //if it exists
}
But, if you just want to access that single element from the array then you would do this:
echo $data[0]->name;

count() returning wrong value

I am using the following code:
$row_arr=$_POST['row_driver'];
print_r($row_arr);
returns:
Array ( [0] => d1 [1] => d2 [2] => d3 [3] => d5 )
but
echo count($row_arr);
is returning me a value of
1
Any reason why?
Here row_driver is an array being received through a form from a previous PHP page using hidden element property of HTML form. Also,
foreach($row_arr as $driver)
{
//code here
}
is returning:
Warning: Invalid argument supplied for foreach() in
D:\XAMPP\htdocs\Carpool\booking_feed.php on line 36
The issue you are facing is with the fact that $_POST['row_driver'] is not an array.
If you have one hidden HTML input:
<input type="hidden" name="row_driver" value="<?php print_r($rows); ?>">
...then $_POST['row_driver'] would be a string, e.g.:
$_POST['row_driver'] = "Array ( [0] => d1 [1] => d2 [2] => d3 [3] => d5 )";
, and therefore, your count() function results in 1.
This would also explain the second issue you are facing, with foreach(), where the function expects an array, but you are providing a string.
A solution would be to use a foreach loop for your hidden HTML inputs like this:
<?php foreach($rows as $row_driver){?>
<input type="hidden" name="row_driver[]" value="<?php echo $row_driver; ?>"/>
<?php }?>
This would then turn your $_POST['row_driver'] into an array.
You might just store the count value in some variable :
$row_arr=Array('d1','d2','d3','d4');
print_r($row_arr);
$count = count($row_arr);
echo 'Your Count is:- '.$count;
PHP document:
expression
The expression to be printed. return
If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will
return the information rather than print it.
Return Values
If given a string, integer or float, the value itself will be printed.
If given an array, values will be presented in a format that shows
keys and elements. Similar notation is used for objects.
When the return parameter is TRUE, this function will return a string.
Otherwise, the return value is TRUE.
print_r() can use as special printing method to display all values in arrays and for associative arrays(more helpful for this).
Associative array:
Associative arrays are arrays that use named keys that you assign to them.
If you use echo you have print it with a array index. As a example $row_arr[0] or if you use for associative array instead of index, key is used. it may be string.
The problem lies with the hidden field
foreach ($rows as $value){
<input type="hidden" name="row_driver[]" value="<?php echo $value; ?>">
}

PHP echo single 2d array value

I have a 2d array $locations that is the result of a sql query. I can use the foreach function to get all of the rows, like this, and it works perfectly:
foreach($locations as $row) {
echo $row->NICKNAME;
echo $row->POC;
}
I just want to grab the first row of the index NICKNAME in the array. I try
echo $locations["NICKNAME"][0];
and it says "Undefined index: NICKNAME"
I try:
echo $locations[0][0];
and it says "Cannot use object of type stdClass as array"
When I echo gettype($locations) it prints the word array, and the foreach function (which is only for arrays right?) works, so I really don't understand that error.
I know this is simple but I don't know what else to try and Googling hasn't helped.
Try using this as $location is an array of objects and to reference each object, you have to use $location along with the key of the object you want to select. Once selected, use the the Nickname from it as a regular object property.
echo $locations[0]->NICKNAME;

Extraing data from json array and separate according to type

So I am currently trying to extract information from a json array using json_decode($result,true). The background here is that there is another php script getting information from a database and it is sending the data to me as json_encoded result.
using print_r($json) i get the following
Array
(
[result] => 1
[message] => Query Successful
[data] => Query Output
[0] => Array
(
//Several values
[test] => test
[Example] => catcatcat
[choice2] => B
)
[1] => Array
[test]=> test
//etc....
I understand we can use a simple for loop to get some stuff to display or in this case I used
for($i=0;$i<=count($json); $i++){
echo $json[$i]['test'];
//etc etc
}
and that will display the value. But what I cant figure out is how to send that to my HTML page as an output as a list.
I am trying to get it to display as following
Test catcatcat B
Test etc etc
--This may be a separate question but for me to learn I want to know if it's possible to actually break down the array and send to html as radio input and turn it into a value to choose from.
Your JSON result is a mixture of 1st level elements and sub-arrays, so you'll need to filter those.
Use a foreach loop like this to output radio buttons:
foreach($json as $current) {
if(!is_array($current))
continue; // skip top level properties that aren't sub arrays
echo '<input type="radio" name="yourradio" value="' . $current['choice2'] . '"> ' . $current['test'] . ' ' . $current['Example'];
}
The value of the radio button and the labels are up to you, but that's the general idea.

PHP Fatal error: Cannot use string offset as an array

Facing a weird situation with arrays..
I am using LinkedIn API to get profile info which returns data in two formats..
If user has just one educational item
educations=>education=>school-name
educations=>education=>date
...
If more than one education item
educations=>education=>0=>school-name
educations=>education=>0=>date
...
educations=>education=>1=>school-name
educations=>education=>1=>date
...
Now I am trying to make it consistent and convert
educations=>education=>school-name
to
educations=>education=>0=>school-name
But getting error in code that i believe should work
if(empty($educations['education'][0]['school-name']))
{
$temp = array();
$temp['education'][0]=$educations['education'];
$educations = $temp;
}
This fails for "just one educational item", generates error on the first line for (isset,is_array and empty)
PHP Fatal error: Cannot use string offset as an array in ...
print_r returns
[educations] => Array
(
[education] => Array
(
[id] => 109142639
[school-name] => St. Fidelis College
[end-date] => Array
(
[year] => 2009
)
)
)
Usually you'd write the assignment like this:
$temp = array(
"education" => array($educations['education'])
);
To avoid any issues with indexes. This might also fix yours.
If you're unsure about the contents of $educations['education'][0]['school-name'] you can simply check each part:
if(isset($educations['education'], $educations['education'][0], $educations['education'][0]['school-name']))
This works because isset doesn't behave like a normal function. It takes multiple arguments in a lazy manner.
You want:
if(array_key_exists('school-name',$educations['education']))
{
$educations['education'] = array($educations['education']);
}
Today I experienced the same problem in my application. Fatal error: Cannot use string offset as an array in /home/servers/bf4c/bf4c.php on line 2447
line 2447
if (!isset($time_played[$player]["started"])) {
$time_played[$player]["started"] = $time;
}
$time_played was overwritten elsewhere and defined as a string. So make sure you do use unique variable names.
Here's a tip if you're running through a loop, and it breaks:
if( $myArray != "" ){
// Do your code here
echo $myArray['some_id'];
}

Categories