Can someone show, fixed or tell me why i am getting this error
Call to a member function getRating() on a non-object
Here is the function
public function getRating($movieid){
$movieid = mysql_real_escape_string($movieid);
$average = 0;
$e = mysql_query("SELECT AVG(`rating`) as average FROM movie_ratings WHERE movieid='$movieid'") or die(mysql_error());
if (mysql_num_rows($e)>0){
extract(mysql_fetch_array($e));
}
return $average;
}
And This is how i call the function
$movie = $movie->getByPerma($perma,$language);
if (empty($movie)){
$movie = '';
} else {
$movie['rating'] = $movie->getRating($movie['id']);
$tags = $movie->getMovieCategoryDetails($movie['id'],$language);
if (count($tags)){
$smarty->assign("tags",$tags);
} else {
$smarty->assign("tags","");
}
}
Can someone help me as i'm new to php.
Unclear what the issue is without seeing the rest of the code, but it seems like you are instantiating $movie
for a class at one point and then assigning $movie
as an array. That can't be good since the array $movie
assignment will just wipe out the class $movie
assignment.
I would recommend renaming the class variable or array. Here is it with the array renamed $movie_array
:
$movie_array = $movie->getByPerma($perma,$language);
if (empty($movie_array)){
$movie_array = '';
} else {
$movie_array['rating'] = $movie->getRating($movie_array['id']);
$tags = $movie->getMovieCategoryDetails($movie_array['id'], $language);
if (count($tags)){
$smarty->assign("tags", $tags);
} else {
$smarty->assign("tags","");
}
}
And here it is with the class renamed $movie_class
:
$movie = $movie_class->getByPerma($perma,$language);
if (empty($movie)){
$movie = '';
} else {
$movie['rating'] = $movie_class->getRating($movie['id']);
$tags = $movie_class->getMovieCategoryDetails($movie['id'], $language);
if (count($tags)){
$smarty->assign("tags", $tags);
} else {
$smarty->assign("tags","");
}
}
It looks like
$movie = $movie->getByPerma($perma,$language);
is not returning an object, and so then later in the code when you call
$movie['rating'] = $movie->getRating($movie['id']);
you are calling a function on a non-object. check to see what $movie is equal to before this line by using
var_dump($movie);
$movie=['rating'] = $movie->getRating($movie['id']);
$movie->getByPerma($perma,$language);
is returning something that is not an object.
So I would
print_r($movie)
on line 2 and see what I'm getting.
The second wierd thing is in:
$movie['rating'] = $movie->getRating($movie['id']);
On the left side you are using $movie as an array and on the right side you are using it as an object and then again yo sent the parameter you use $movie['id'] as an array.
So:
If you are getting an array, the array cant have functions, the function should be outside a class and will be called like this:
getRating($movie['id'])
istead of
$movie->getRating($movie['id']).
If you are getting an object, and the object implements the function
getRating($movie_id)
then the way to access the properties of the object is:
$movie->rating and $movie->id
I'm asuming that the properties are declared public. This is not the correct way of doing it though... The properties should be private and you should implemente getters and setters for the objects properties like this:
private $rating;
public function get_rating()
{
return $this->rating;
}
In this case to get the rating, use
$movie->get_rating();
And to asign a value to the rating, implement
public function set_rating($r)
{
$this->rating=$r;
}
And asign value like this:
$movie->set_rating($some_rating);
Dunno if I helped or made everything more confusing :S but feel free to ask me questions :)
Not sure though, Seems like $movie = $movie->getByPerma($perma,$language);
is not returning an object. Try to do var_dump($movie);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.