Iv been trying to get my head around object orientation and I think iv started to get some of the concepts, but im not sure. Making a google search that answers if my train of thought is correct proved to be quite hard so I decided to ask here really quick, please tell me if the question is against any rules.
Im I thinking correctly in regards to messagepassing? What are the obviously bad things? How should I think while going forward with my learning?
Like getpagecontent($page, $connection);
etc
Atm im reading [Oreilly - Learning php and mysql][1] and [Programming in an Object-Oriented Environment][2] And some books on UML
Here is the code.
dbfunctions.php
<?php
class dbconnect {
function dbconnect() {
$this->dbhost = 'xx';
$this->dbuser = 'xx';
$this->dbpass = 'xx';
$this->dbdatabase = 'xx';
}
function createdbconnection() {
require_once('DB.php'); // pear
$this->connection = DB::connect("mysql://$this->dbuser:$this->dbpass@$this->dbhost/$this->dbdatabase");
if (DB::isError($this->connection)) {
die("Could not connect (connection)<br>" . DB::errorMessage($this->connection));
}
}
function closedbconnection(){
$this->connection->disconnect();
}
}
class dbinteractions {
function dbinteractions($connection) {
$this->connection = $connection;
}
function searchdb($qstring) {
if (get_magic_quotes_gpc()) {
$qstring = stripslashes($qstring);
}
$qstring = mysql_real_escape_string($qstring);
$query = "SELECT content FROM site_content WHERE content LIKE '%$qstring%'";
$result = $this->connection->query($query);
if(DB::isError($result)) {
die("Could not connect (query)<br>" . DB::errorMessage($result));
}
while($result_row = $result->fetchRow()) {
echo("<h2>Resultat:</h2>");
foreach($result_row as $out)
echo($out . "<p>");
}
}
function getpagecontent($page) {
$query = "SELECT * FROM site_content WHERE (page_id = \"" . $page . "\")";
$result = $this->connection->query($query);;
while($result_row = $result->fetchRow()) {
echo "<h1>" . $result_row[0] . "</h1>"; //Echo page_id
echo $result_row[1]; //Echo content
echo "<h2>" . $result_row[2] . "</h2>"; //Echo timestamp
}
}
}
?>
search.php
<?php
function displaysearchform()
{
echo("<p></p>");
if($_GET["search"] == '') { //Display search box if no search ?>
<form method="GET" action="<?php echo(htmlentities($_SERVER['PHP_SELF'])); ?>">
<label>
Search: <input type="text" name="search" />
</label>
<input type="submit" value="Go!">
</form>
<?php
return false;
}
else
{
return true;
}
}
?>
index.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<link rel="stylesheet" href="style.css" type="text/css">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<div id="container">
<div id="sidebar">
<a href="?id=1">1</a><br>
<a href="?id=2">2</a><br>
</div>
<div id="main">
<?php
include("dbfunctions.php");
include("search.php");
$dbconnect = new dbconnect();
$dbconnect->createdbconnection();
$dbinteractions = new dbinteractions($dbconnect->connection);
if(!$_GET["id"] && $_GET["search"] == "") { //Check if direct site hit and no search query
$dbinteractions->getpagecontent("1");
}
else {
$page = $_GET["id"];
$dbinteractions->getpagecontent($page); //Get and display page content
}
if (displaysearchform() == true){ //If search was made don't display searchform
$dbinteractions->searchdb($_GET["search"]);
}
$dbconnect->closedbconnection(); //Close connection to db
?>
</div>
</div>
</body>
</html>