OOP: display result of a database with while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: OOP: display result of a database with while loop

Post by lauthiamkok »

I finally got many things sorted as I wanted! this is very exciting! this is my up to date classes in class_lib.php,

Code: Select all

// get the database
class db {
	protected $_connection = null;
	protected $_error = null;

	#make a connection
	public function __construct($hostname,$username,$password,$database)
	{
		$this -> _connection = new mysqli($hostname,$username,$password,$database);
	}
        
	#Fetches all result rows as an associative array, a numeric array, or both
	public function fetch_all($query) 
	{
		$result = $this -> _connection -> query($query);
		if($result) {
			return $result -> fetch_all(MYSQLI_ASSOC);
		} else {
			$this -> _error = $this -> _connection -> error;
			return false;
			}
	}
		
	#Fetch a result row as an associative array
	public function fetch_assoc($query)
	{
		$result = $this -> _connection -> query($query);
		if($result) {
			return $result -> fetch_assoc();
		} else {
			$this -> _error = $this -> _connection -> error;
			return false;
		}
	}
		
	#Get a result row as an enumerated array
	public function fetch_row($query)
	{
		$result = $this -> _connection -> query($query);
		if($result) {
			return $result -> fetch_row();
		} else {
			$this -> _error = $this -> _connection -> error;
			return false;
		}
	}
         
	#Display error
	public function get_rror() 
	{
		return $this -> _error;
	}
	
	#Closes the database connection when object is destroyed.
    public function __destruct()
    {
        $this -> _connection -> close();
    }
}

// get the session ->to find who is logging in the site
class session {
	protected $_connection = null;
	protected $_session = null;

	public function __construct($session,$hostname,$username,$password,$database)
    {
		$this->_connection = new db($hostname,$username,$password,$database);
		$this -> _session = $session;
    }
		
	public function get_session() 
	{
		$sql = "SELECT * 
		FROM root_users 
		WHERE usr_username='".$this -> _session."'";
		//return $db -> fetch_assoc($sql);
		return $this-> _connection -> fetch_assoc($sql);
	}
}

// get the menu
class menu {
	protected $_connection = null;
	
	public function __construct($hostname,$username,$password,$database)
    {
		$this->_connection = new db($hostname,$username,$password,$database);
    }
	
	public function get_menu() 
	{
		//$db = new db($hostname,$username,$password,$database);
		$sql = "SELECT *
		FROM root_menu

		LEFT JOIN root_pages
		ON root_menu.pg_id = root_pages.pg_id

		WHERE root_menu.mnu_hide != '1'
		AND mnu_id = mnu_parent_id
		ORDER BY mnu_order ASC";
		return $this->_connection -> fetch_all($sql);        
	}
}

// get the requested page
class page {
	protected $_connection = null;
	protected $_session = null;
	protected $_url = null;

	public function __construct($url,$session,$hostname,$username,$password,$database)
    {
		$this -> _connection = new db($hostname,$username,$password,$database);
		$this -> _session = new session($session,$hostname,$username,$password,$database);
		$this -> _url = strtolower(str_replace('-', ' ', $url));
    }
		
	public function get_page() 
	{
		#instantiate new object of session
		$item = $this->_session -> get_session();
		
		#If the log in user is a root user
		if($item['cat_id'] == '1')
			{
			$root = true;
			$root_id = $item['usr_id'];
			}
		
		# If the log in user is an assigned user
		elseif($item['cat_id'] == '2')
			{
			$user = true;
			$user_id = $item['usr_id'];
			}
		
		# if you log in as a root user or an assigned user, the hidden pages are visible to you, otherwise invisble. 
		# public cannot see the hidden pages, so set the 'AND' condiction to the query.
		if($root || $user) $and = "";
		else $and = "AND root_pages.pg_hide != '1'";
		
		$sql = "SELECT *
		FROM root_pages
		WHERE root_pages.pg_url = '".$this -> _url."'
		".$and."";
		return $this-> _connection -> fetch_assoc($sql);
	}
}

// get the parent page
class _parent  {
	protected $_connection = null;
	protected $_page = null;

	public function __construct($url,$session,$hostname,$username,$password,$database)
    {
		$this -> _connection = new db($hostname,$username,$password,$database);
		$this -> _page = new page($url,$session,$hostname,$username,$password,$database);
    }
	
	public function get_parent() 
	{
		#instantiate new object of page
		$item = $this-> _page -> get_page();
		
		#grab the parent id
		$parent_id = $item['parent_id'];
		
		#send the query
		$sql = "SELECT *
		FROM root_pages
		WHERE pg_id = '$parent_id'";
		
		#return the value of the query
		return $this-> _connection -> fetch_assoc($sql);       
	}
}
and I have a config php file, core.php, something like this,

Code: Select all

/* Start the session !important for CMS */
$root = false;
$user = false;
session_start();

/* Destroy the session and redirect */
if(isset($_GET['logout'])){
	session_destroy();
	header("Location:index.php");
	}	
 
/** 
 * This is all the directories and website paths need to be changed to move the website.
 * 
 */
 
# Configurations starts
 
/* Set http root */
# live server 
#define('HTTP_ROOT', 'http://'.$_SERVER['SERVER_NAME'].'/'); 

# localhost
define('HTTP_ROOT', 'http://localhost/ginger_monkey_2010/');

/* Base directory where everything is located, for uploading images, videos, audios, full path required */
# live server 
#define('DOC_ROOT', $_SERVER['DOCUMENT_ROOT'].'/');

# localhost
define('DOC_ROOT', $_SERVER['DOCUMENT_ROOT'].'ginger_monkey_2010/');

/* Database connection details and params */
# the host used to access DB
define('DB_HOST', 'localhost');

# the username used to access DB
define('DB_USER', 'root');

# the password for the username
define('DB_PASS', 'tklau');

# the name of your databse 
define('DB_NAME', 'ginger_monkey_2010'); 

# Configurations ends 

# session
define('SESSION', $_SESSION['authenticated_gm']);

$session = SESSION;

require_once 'class_lib.php';

/* Make the MySQL connection */
$hostname = DB_HOST;
$username = DB_USER;
$password = DB_PASS;
$database = DB_NAME;
and my index.php will be something like this,

Code: Select all

<?php include("core.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html;
		charset=iso-8859-1" />
		<title>OOP in PHP</title>
	</head>
<body>

<ul>
<?php
$menu = new menu($hostname,$username,$password,$database);
$items = $menu -> get_menu();
//print_r($items);
if(is_array($items)) 
{
	foreach($items as $item) 
	{
?>
		<li><?php echo $item['mnu_name']; ?> </li>   
<?php
	}
}
?>
</ul>
<?php
	$page = new page($_REQUEST['pg'],$session,$hostname,$username,$password,$database);
	$item = $page -> get_page();
	//print_r($item);
	echo $item['pg_content_1'];
?>

<?php
	$_parent = new _parent($_REQUEST['pg'],$session,$hostname,$username,$password,$database);
	$_parent = $_parent -> get_parent();
	//print_r($_parent);
	echo $_parent['pg_id'];
?>
</body>
</html>
I am so excited with OOP - its amazing! loving it! :lol:

thank you :D
Post Reply