This will probably be tagged as duplicate but might as well try since I can't get a specific solution.
my controller function seems to result to "Trying to get property of non-object" error. I want to get a specific data from an index of an array. the array is from a Session data that I used in another page.
function within my controller
public function checkPwd()
{
$oldPwd = $this->input->post('oldPass');//getting POST data from ajax
$newPwd = $this->input->post('newPass');//getting POST data from ajax
$currLogged = $this->session->all_userdata();
foreach ($currLogged as $log)
{
echo $log->pwd; //this results to Trying to get property of non-object
}
}
You can access it as
echo $currLogged['logged'][0]->pwd
In foreach you can access it as
foreach($currLogged as $log){
echo $log[0]->pwd;
}
Related
I'm working with php, codeignitor 3 and groceryCRUD.
I'm trying to get access to variables inside of a callback function of groceryCRUD and tried to set it as a property to the this object. I wanted to reset or renew it on every foreach loop iteration and expected to be able to access it inside of the function like shown below. Unfortunately this doesn't work and I get an error 'Illegal Offset Type'.
Thanks for any help :)
public function somefunc(){
...
$array = [['one', 'two'], ['one', 'two']];
foreach ($array as $x) {
$this->my_prop_var = $x;
$crud->callback_column('column_name',
function ($fieldValue) {
echo $this->my_prop_var[0];
return something... ;
}
);
unset($this->my_prop_var);
};
};
Hi i have made a function in which i am getting results from db and storing it to an array and returned an array to the function. Below is the function:
public function products() {
shopping::connection();
$get = mysqli_query($this -> connection, 'Select * from products limit 5');
$result[] = array();
while($results = mysqli_fetch_object($get)) {
$result[] = $results;
}
return $result;
}
so now this function is another file called functions.php i called this file into another file where i want to pull all the data and display on that page so i included the file and stored the function in variable. Below how i did
<?php require_once('codec/functions.php'); ?>
<?php $products = $object -> products(); ?>
Now if i do print_r($products) gives me an array of data, but when i try to retrieve the data using this function
<?php echo $product -> price; ?>
It gives me an error of
Notice: Trying to get property of non-object in C:\xampp\htdocs\Shopping\index.php on line 15
I did search on google but didn't found result that i want. Please tell me what i am doing wrong. Thanks
You are trying to access a property of a non-object. As the error tells you.
There is a huge difference between objects and arrays.
You have an array of products, which is also an object.
If you want to access the data from the db, you need to loop the results.
foreach ($products as $product) { // where $product is a single row from your query
echo $product->price; // here you take the price of a single product.
}
You also have a problem with initializing your $result variable in the function products. It will always have an empty array on the first index. Try $result = array(); so that you wont have any problems.
It's strange, I can access the property before the if statement, and if I change the if condition to something else, I can accessible the property inside the if statement as well. But trying to access the property inside the if condition throws an error:
$market_ids = [];
$state->cities->each(function($city) use (&$market_ids)
{
dd($city->market->id); // <-Accessible here
if (!in_array($city->market->id, $market_ids)){ // Error: Trying to get property of non-object
dd($city->market->id); // Accessible here
$market_ids[] = $city->market->id;
}
});
Error: Trying to get property of non-object
Anyone run into this before?
I think, you should make sure that for each of your cities your relation doesn't return null:
$market_ids = [];
$state->cities->each(function($city) use (&$market_ids)
{
if ($city->market === null) {
echo " null market for ".$city->name."<br />";
continue;
}
if (!in_array($city->market->id, $market_ids)){
$market_ids[] = $city->market->id;
}
});
As you used dd you checked it for first record and it doesn't mean that for 2nd or 3rd or later rows there's corresponding market object. For example you have 100 cities and 99 nine of them have market and one of them doesn't. You will get the error you asked because for one of them you don't have market so you cannot ask for market id.
So to make it clear this error is rather not connected to if or in_array, because if you use:
$market_ids = [];
$state->cities->each(function($city) use (&$market_ids)
{
echo $city->market->id; // not dd but simply echo
});
you will probably also get the same error.
Trying to get property of non-object
means the source $city->market not an object in some cases, or check this edit:
$market_ids = [];
$state->cities->each(function($city) use (&$market_ids)
{
$id = $city->market->id;
dd($id);
if ( ! in_array($id, $market_ids) ){
dd($id);
$market_ids[] = $id;
}
});
Baffled by what should be a fairly straightforward issue.
I have two objects related to each other:
class Country extends Eloquent {
public function hotspot()
{
return $this->hasOne('Hotspot');
}
}
and
class Hotspot extends Eloquent {
public function country()
{
return $this->belongsTo('Country');
}
}
I want to retrieve my hotspots and the countries they belong to, so:
$hotspot_list = Hotspot::with('country')->get();
As a test, I just want to loop through the list and output the country codes:
foreach ($hotspot_list as $hotspot_item) {
$hotspot = $hotspot_item->country;
echo $hotspot->country_code;
}
Throws an error: "Trying to get property of non-object"
So obviously I also can't do echo $hotspot_item->country->country_code;
If I access $hotspot as an array, it works: echo $hotspot['country_code'];
Because of this, I can't access $hotspot as an object. Since $hotspot is actually a Country object, I'm wanting to check another relation I have with Country but I can't since it's giving me an array instead of the object.
So even though I shouldn't have to do it this way, I tried this:
$country_id = $hotspot['id'];
$country = Country::find($country_id);
echo $country->name;
Still no go, it's still returning as an array, so I can do echo $country['name'];
Suggestions?
Ensure all your hotspots have countries, or you can validate them as you loop...
foreach ($hotspot_list as $hotspot_item) {
$hotspot = $hotspot_item->country;
if(isset($hotspot->country_code)) {
echo $hotspot->country_code;
}
}
Or even better if you have Laravel 4.1, only get the countries that have hotspots with...
$hotspot_list = Hotspot::has('country')->get();
I consider myself as a php beginner, so it may be possible that this question is too easy for someone, but I got really confused on how to solve it. I am trying to loop something from the database in my views. So, in a quick way I solved it like this:
I've created a function in my model that does the loop and in the same time is creating the html and saves it in a variable. Then, I get that variable from my controller and I pass it in my view. But, it seems that this is not a good way to solve it, since if I want to change my html I need to enter my model function instead some of the view files.
Then, I've created another function in my model that looks like this:
function displayUsers() {
$sql = $this->pdo->prepare('select * from user');
$sql->execute();
while($row = $sql->fetch())
$results[] = $row;
return $results;
}
Now... I take the result in my controller, and send it in the view, but then... I don't know how to extract the results from my variable. I have done something like this:
while($output) {
foreach($output[$i] as $key => $value)
$data[$key] = $value;
echo $data['email'];
$i++;
}
But then, in the end it says to me undefined offset, which means I am referring to an array key that doesn't exist. Can anyone help me on how to solve this issue?
Proper MVC shouldn't have any output in the model or the controller.
Ideally you would have a model that just gets the raw data and returns it in the controller. The controller can then build up an array of values that we'll call data. For example:
Controller
$data['users'] = $this->MyModel->getusers(); // Getting the users from your model
$data['posts'] = $this->MyModel->getposts(); // Getting the posts from your model
$this->getTemplate('userdisplay', $data); // Get the template userdisplay and pass data
This gets the data from the model, and then assigns it to a key within the "data" variable. You can then pass the data variable into the template. You'll then have two variables to work with in the template, $users and $posts.
You'll need a "getTemplate" function that properly maps the data array to individual variables for use in the template, but all of the display should be located in the template.
To answer your specific question at the end, something like this should work in the template:
if (count($users) > 0) {
foreach ($users as $person) {
echo $person['email'];
}
}
You should be able to do this:
foreach($output as $row) {
echo $row['email'];
}