Just trying to get my head around OOP. I have been told that I am doing the script below wrong. I was previously trying to call a method from the database class. When really the database class should only be dealing with database stuff. So I have now created an html_class.php where I believe I need to access the database class and then output the html via this class?.
The method I am trying to call on the index.php is the getBlogPost()method located in the html_class.php file. I am trying to call this using echo $htmlObject->getBlogPosts();
I am assuming this is the proper way to go about doing this?
I am currently getting the following errors:
Notice: Undefined variable: db_login_info in C:\xampp\htdocs\xampp\oop\CMS\classes\html_class.php on line 14
Fatal error: Cannot access private property database::$objDbConn in C:\xampp\htdocs\xampp\oop\CMS\classes\html_class.php on line 15
index.php
<?php
require_once("includes/header.php");
require_once("includes/footer.php");
require_once("classes/database_class.php");
require_once("classes/blog_post_class.php");
require_once("classes/html_class.php");
require_once("conf/config.php");
$header = new header();
// createHeader( Title | Description | Keywords)
echo $header->createHeader('Home - Nissan Skyline GTR Blog', 'This is a blog site about the Nissan GTR', 'Nissan, GTR, R35');
$dbObject = new database($db_login_info);
$htmlObject = new makeHTML();
?>
<div id="container">
<div id="top_nav">
<a href="admin.php" class="admin_link">Admin Login</a>
</div>
<div id="header"></div>
<div id="nav">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="create-blog.php">Create Blog</a></li>
<?php
$sql = "SELECT * FROM create_page";
echo $dbObject->createNavigation($sql);
?>
</ul>
</div>
<div id="content_area">
<?php
echo $htmlObject->getBlogPosts(); // Calling the getBlogPost function located in the html_class.php
// echo $dbObject->getBlogPosts();
?>
</div>
<?php
$footer = new footer();
echo $footer->createFooter();
?>
database_class.php
<?php
class database {
private $objDbConn;
function __construct($db_login_info){
$this->objDbConn = new mysqli($db_login_info['host'], $db_login_info['username'],
$db_login_info['password'], $db_login_info['database']);
if (mysqli_connect_errno()) {
die("Database connection failed". mysqli_connect_error());
}
}
// I was previoulsy calling this method below on the index.php page and it was working great
// function getBlogPosts(){
// $objRes = mysqli_query($this->objDbConn, "SELECT * FROM `blog_posts` ORDER BY id DESC");
// if(mysqli_errno($this->objDbConn)) {
// die("Failed query: $strSql". $this->objDbConn->error);
// }
// $allrows ='';
// while ($row = mysqli_fetch_array($objRes)) {
// $time = strtotime($row['date']);
// $my_format = date("m/d/y @ g:ia", $time);
// $allrows .= '<div id="blog_post">';
// $allrows .= '<h2 id="blog_title">'.$row['title'].'</h2>';
// $allrows .= '<h5> Posted on the '.$my_format.'</h5>'."<br>";
// $allrows .= '<div id="blog_content">'.nl2br($row['content']).'</div>'."<br>";
// $allrows .= '<b>ID:</b>'.$row['id'].'<a href="delete.php?id='.$row['id'].'" name="delete" onclick="return confirm(\'Are you sure you want to delete this post?\');"> Delete</a></div>';
// };
// return $allrows;
// }
?>
html_class.php
<?php
require_once("includes/header.php");
require_once("includes/footer.php");
require_once("classes/database_class.php");
require_once("classes/blog_post_class.php");
require_once("classes/html_class.php");
require_once("conf/config.php");
class makeHTML {
function getBlogPosts(){
$dbObject = new database($db_login_info); // This is where the Notice: Undefined variable: db_login_info is happening
$objRes = mysqli_query($dbObject->objDbConn, "SELECT * FROM `blog_posts` ORDER BY id DESC");
if(mysqli_errno($dbObject->objDbConn)) {
die("Failed query:". $dbObject->objDbConn->error);
}
$allrows ='';
while ($row = mysqli_fetch_array($objRes)) {
$time = strtotime($row['date']);
$my_format = date("m/d/y @ g:ia", $time);
$allrows .= '<div id="blog_post">';
$allrows .= '<h2 id="blog_title">'.$row['title'].'</h2>';
$allrows .= '<h5> Posted on the '.$my_format.'</h5>'."<br>";
$allrows .= '<div id="blog_content">'.nl2br($row['content']).'</div>'."<br>";
$allrows .= '<b>ID:</b>'.$row['id'].'<a href="delete.php?id='.$row['id'].'" name="delete" onclick="return confirm(\'Are you sure you want to delete this post?\');"> Delete</a></div>';
};
return $allrows;
}
}
?>
config.php
<?php
// Database login details below
$db_login_info = array(
'host'=>'localhost',
'username'=>'root',
'password'=>'',
'database'=>'cms'
);
?>
Updated html_class.php
<?php
require_once("includes/header.php");
require_once("includes/footer.php");
require_once("classes/database_class.php");
require_once("classes/blog_post_class.php");
require_once("classes/html_class.php");
require_once("conf/config.php");
class makeHTML {
private $database;
public function __construct(database $database){
$this->database = $database;
}
function getBlogPosts(){
$objRes = mysqli_query($this->database, "SELECT * FROM `blog_posts` ORDER BY id DESC");
if(mysqli_errno($this->database)) {
die("Failed query:". $this->database->error);
}
$allrows ='';
while ($row = mysqli_fetch_array($objRes)) {
$time = strtotime($row['date']);
$my_format = date("m/d/y @ g:ia", $time);
$allrows .= '<div id="blog_post">';
$allrows .= '<h2 id="blog_title">'.$row['title'].'</h2>';
$allrows .= '<h5> Posted on the '.$my_format.'</h5>'."<br>";
$allrows .= '<div id="blog_content">'.nl2br($row['content']).'</div>'."<br>";
$allrows .= '<b>ID:</b>'.$row['id'].'<a href="delete.php?id='.$row['id'].'" name="delete" onclick="return confirm(\'Are you sure you want to delete this post?\');"> Delete</a></div>';
};
return $allrows;
}
}
Update database_class.php
class database {
public $objDbConn;
function __construct($db_login_info){
$this->objDbConn = new mysqli($db_login_info['host'], $db_login_info['username'],
$db_login_info['password'], $db_login_info['database']);
if (mysqli_connect_errno()) {
die("Database connection failed". mysqli_connect_error());
}
}
?>
fetchall method in database_class.php:
function fetchall($sql) {
$query = mysqli_query($this->objDbConn, $sql);
if ($this->objDbConn->connect_errno) {
die("Database connection failed with message: " . $objDbConn->connect_error);
}
}