I´m currently trying to build a website with oop. For a better view I´ve created a file called Article.class.php. In this file are all functions to get and process the data. Then I´ve the second file called _header where are all instances of the class php files to work with them individualy. My last file is the “my_articles.php” file where my table gets loaded with the data of the class file. When the code gets runned and I open up the website where the table has to be, it shows me the error: undefined variable: article_variable in line 27 and Fatal error: Uncaught Error: Call to a member function my_articles() on null.
Here is the Article.class.php file
public function my_articles(){
include_once("my_articles.php");
$sql = "SELECT * FROM items ORDER BY item_name DESC";
$statement = $this->_database_intance->prepare($sql);
$statement->execute();
$users = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)){
$users[] = $row;
}
return $users;
}
Here is the _header.php file (all instances)
#Database Connection:
include('classesDatabase.class.php');
$database_variable = new Database();
$database_instance = $database_variable->connection();
#Article class instance
include('classesArticle.class.php');
$article_variable = new Article($database_instance);
$article_instance = $article_variable->poll();
include('classesUsers.class.php');
$users_variable = new Users($database_instance);
$users_instance = $users_variable->poll_users();
and my last file (my_articles.php) where the data should print out:
<tbody>
<?php
include_once('shared_header.php');
$users = $article_variable->my_articles();
foreach ($users as $row){
?>
<tr>
<td><?php echo $row['item_name']; ?></td>
<td><?php echo $row['amount']; ?></td>
<td>
<a href="C:xampphtdocsAufgabeArticlesarticle_details.php?id=<?php echo $row['item_name']; ?>"class="view_button">View</a>
<a href="edit.php?id=<?php echo $row['item_name']; ?>"class="edit_button">Edit</a>
<a onclick="return confirm('Are you sure you want to delete this record?');" href="Articlesdelete_article.php?id=<?php echo $row['item_name'];?>"class="delete_button">Delete</a>
</td>
</tr>
<?php
}
?>
</tbody>