Stuck in echoing data out of variable CodeIgniter - php

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;

Related

Get value from STDClass

I'm using PHP and CodeIgniter.
I ran a query using the following script:
$query = $this->db->query('select login_id, date_created from prjsite_login');
$row = $query->result();
print_r($row);
The result of the print_r is:
Array ( [0] => stdClass Object ( [login_id] => admin [date_created] =>
2018-04-04 13:18:42 ) )
Which is correct. Thou when I tried to fetch 1 object or value from stdClass using the following script:
echo $query->login_id;
I received an error below:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: pages/home.php
Line Number: 21
Backtrace:
File: C:\xampp\htdocs\BMI_PRJSITE\application\views\pages\home.php
Line: 21 Function: _error_handler
File: C:\xampp\htdocs\BMI_PRJSITE\application\controllers\Pages.php
Line: 11 Function: view
File: C:\xampp\htdocs\BMI_PRJSITE\index.php Line: 315 Function:
require_once
What am I doing wrong?
TIA
You cannot directly get a value from $query because at this point you are just generating a query and you will get the result from $query only after executing it which you are doing at
$row=$query->result();
Looking at your result you are getting a result as an array of a stdClass object so need to json-encode your object and then decode it back to an array
$array = json_decode(json_encode($row), True);
If you are sure you will get only one row then no need for loop and you can simply do it by
echo $array[0]->login_id;
otherwise, you have to go for a loop
foreach ($array as $value) {
echo $value->login_id;
}
As your result is in array and array contain object your have to first access that array then object
echo $row[0]->login_id;
Or use foeach to get all values
foreach($row as $value)
{
echo $value->login_id;
}

Getting error On data Fetch

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.

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'];
}

Unable to get number of likes in an array php

I am using the following code to get the number of likes on a page.
<?php
$site="http://graph.facebook.com/?ids=http%3a%2f%2fXXXXXXXX/svce.php";
$graph= file_get_contents($site);
$json_string=$graph;
$array = json_decode($json_string, true);
//echo "<pre>";
//print_r($array);
$var = $array['shares'];
echo $var;
?>
But whenever i try to echo out the following code. I always get an unidentified index Notice which is as following: Notice: Undefined index: shares in C:\xampp\htdocs\graphapi.php on line 19
Where am i going wrong?
Here's the print_r output:
Array
(
[http://xxxxxxxxx/svce.php] => Array
(
[id] => http://xxxxxxxxx/svce.php
[shares] => 7
[comments] => 3
)
)
According your print out looks like there is an array more in $array. Try this;
echo $array['http://xxxxxxxxx/svce.php']['shares'];
You have to use the site-name as an key before.
Structure:
- http://example.com
- id
- shares
This means in PHP:
$array["http://example.com/path/to/site"]["shares"];

PHP list issue ( Undefined offset: )

Original SQL query is this;
SELECT id,post_title,post_date FROM wp_posts where id='1'
When I retrieve the record, I am finding it but when it comes to returning the results, I am puzzled. Here is the where I got stuck.
while ($row = mysql_fetch_assoc($RS)) :
print_r ($row);
list($id,$post_title,$post_date) = $row;
endwhile;
print_r ($row) outputs this;
Array ( [ID] => 1 [post_title] => Hello world! [post_date] => 2012-03-27 03:28:27 )
And when I run the list function in there ( for debug purposes obviously ), I get this;
Notice: Undefined offset: 2 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 1 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 0 in F:\inetpub\wwwroot\whatever\sql.php on line 147
What's causing this?
Replace:
mysql_fetch_assoc($RS)
with:
mysql_fetch_array($RS, MYSQL_NUM)
then it should work, because the list function trys to access the array using numeric keys.
I guess the answer lies somewhere within this;
list() only works on numerical arrays and assumes the numerical indices start at 0.
:(
You might be able to use extract() here instead, as well; (documentation here.)
You used mysql_fetch_assoc, so the resulting array per row has data under a key by column name, whereas "list" tries to match variables to values using numerical array indexes. You can use mysql_fetch_array instead.
$categ = val1 | val2
list($one,$two,$three)=#split('[|]',$categ);
If you try to list the value which is not available it will return the error Undefined Offset.
Here the error will be Undefined Offset 2.
Because while spliting $categ, it will have only two values, if you try to access third value then it will return error.

Categories