UPDATE 1:
I forgot to add GetTags()
method, so here it is:
public $blog_tags;
public function GetTags()
{
return $this->blog_tags;
}
=========================================================================
I'm working with PHP OOP to develop my project. Basically I have a table called blogs
which contains some fields and data like this image:
Then I created a class Blos.class.php
and made two methods as below:
public function ShowTag()
{
$tag = $this->_db->prepare("SELECT blog_tags FROM blogs");
$tag->execute();
while($row = $tag->fetch())
{
$this->blog_tags = $row['blog_tags'];
}
}
public function NumTag()
{
$cat = $this->_db->prepare("SELECT blog_tags FROM blogs");
$cat->execute();
$row_cat = $cat->rowCount();
return $row_cat;
}
Then in order to retrieve data on screen, I did this:
$tagSet = new Blogs();
$tags = $tagSet->NumTag();
$tagShow = $tagSet->ShowTag();
if(!empty($tags)){
$tagShow->GetTags();
}else{
echo "There is no tag available right now!";
}
But the problem is I get this error message:
Fatal error: Uncaught Error: Call to a member function GetTags() on null in line 20
Which is this line:
$tagShow->GetTags();
So what is the mistake with that ? Can you please help me ?
The problem with the class function is
$tagShow = $tagSet->ShowTag(); //returns no value, hence $tagShow is null/empty.
Moreover,
$tagShow->GetTags();
GetTags() is a function which does not exist in your class. Also, you are calling the function wrong way. $tagShow is originally not an object of your class, it is a variable storing output from ShowTag().
If you have a function named GetTags(), call it using
$tagSet->GetTags();
Try using it this way:
public function ShowTag()
{
$blog_tags=array();
$tag = $this->_db->prepare("SELECT blog_tags FROM blogs");
$tag->execute();
while($row = $tag->fetch())
{
$blog_tags[] = $row['blog_tags'];
}
return $blog_tags;
}
and then in your calling,
$tagShow = $tagSet->ShowTag();
if(count($tagShow)>0){
print_r($tagShow); //Do whatever with the tags array.
}else{
echo "There is no tag available right now!";
}
Try this way and see if it helps.
here your code:
if(!empty($tags)){
$tagShow->GetTags();
}else{
echo "There is no tag available right now!";
}
there GetTags()
function isn't this current class. if GetTags in this class so use $this->GetTags()
.