Page 1 of 2

php5OO it?

Posted: Thu Mar 30, 2006 5:48 pm
by Frosh
Ummm, Can someone help me transfer this into Object Oriented? or give me a good kick in the right direction, I have read much of php5 OOP tutorials, however little there is? do you know of others? Anything that I can turn this into OO would be much appreciated...
$channel= new channel_oo ($channel, $low_time, $high_time);
Where each database table contains a different channel?
Somewhere to start that off?

Code: Select all

<?php
$host  = "localhost";
$user  = "frosh_user";
$pass  = "frosh_pass";
$db     = "frosh_db";

$TimeDiff 		= date('I');
$TimeZoneEpoc 	= time() - ($TimeDiff*60*60);
$time 		= date("g:i:s A", $TimeZoneEpoc);
$unixtime		= strtotime($time);

if (!isset($_GET['p_time'])) {
	$low_time	= floor($unixtime/1800)*1800;
}
else if (ctype_digit($_GET['p_time']) && strlen($_GET['p_time']) === 10) {
	$low_time 	= $_GET['p_time'];
} else {
	$low_time	= floor($unixtime/1800)*1800;
}

$low_timea 	= $low_time;
$high_time 	= $low_time + 7200;
$interval_time 	= ($high_time - $low_time);
$column_num	= ($high_time - $low_time)/3600;
$column_size 	= 100/$column_num;

echo "Time: $time<BR>";

$dbh		= mysqli_connect ($host, $user, $pass, $db) or die ('I cannot connect to the database because: ' . mysqli_error());
$query	= "SELECT * FROM disney";
$result 	= mysqli_query($dbh, $query) or die ("Error in query: $query. ".mysqli_error());

echo 	"<table width=\"100%\" border=\"1\"><tr>";

for ($i = 0; $i < $column_num; $i++) {
	echo "<td width=\"".$column_size."%\">".date("g:i:s A", $low_timea)."</td>";			
	$low_timea+=3600;
}
echo "</tr></table>";
echo "<table width=\"100%\"  border=\"1\"><tr>";

while ($obj = mysqli_fetch_object($result)) {
	$item_uid		= $obj->tv_uid;
	$item_title  	= $obj->tv_title;
	$item_desc 	= $obj->tv_desc;
	$item_pub		= $obj->tv_pub;
	$item_upubs	= $obj->tv_unixtimestamp_start;
	$item_upube 	= $obj->tv_unixtimestamp_end;


	if ($item_upubs < $low_time && $item_upube > $high_time) {
		$width = (($high_time-$low_time)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title. " bstart ". $item_pub ."</td>";
	}
	if ($item_upubs < $low_time && $item_upube > $low_time && $item_upube < $high_time) {
		$width = (($item_upube-$low_time)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title ."</td>";
	}
	if ($item_upubs == $low_time) {
		$width = (($item_upube-$item_upubs)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title. "</td>";
	}
	if ($item_upubs > $low_time && $item_upube < $high_time) {
		$width = (($item_upube-$item_upubs)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title. "</td>";
	}
	if ($item_upubs < $high_time && $item_upube == $high_time) {
		$width = (($item_upube-$item_upubs)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title. "</td>";
	}
	if ($item_upubs > $low_time && $item_upubs < $high_time && $item_upube > $high_time) {
		$width = (($item_upube-$item_upubs)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title. "</td>";
	}
}
echo "</tr></table>";
echo "<a href=\"".$_SERVER['PHP_SELF']."?p_time=".($low_time-7200)."\">PREVIOUS</a>";
echo " || ";
echo "<a href=\"".$_SERVER['PHP_SELF']."?p_time=".($high_time)."\">NEXT</a>"; 
?>

Posted: Thu Mar 30, 2006 6:05 pm
by feyd
is there a reason you need this in object oriented? If no real reason, why not start with baby steps in learing OOP? Or maybe make this a function?

Why?

Posted: Thu Mar 30, 2006 8:22 pm
by Frosh
Because I plan to incorporate it with other stuff, that will be OO, at least I hope to, so why not start here?
What baby step?

Posted: Thu Mar 30, 2006 8:44 pm
by Christopher
I shouldn't be too difficult. The simplest would be be two classes: a Model and a View, and the script itself with be a Page Controller. You could also add a Data Access Object (DAO) class to wrapper MySQL (so you wouldn't have direct mysql_*() calls in the Model. Here is my idea:

Code: Select all

$disney = new DisneyModel();
$view = new ListView();
echo $view->render($disney->findAll());

Lost?

Posted: Thu Mar 30, 2006 10:26 pm
by Frosh
You just lost me???
Hmm...That reminds me I should have a separate database class, what do you mean by a model?, what is DAO?, Page Controller?
Haven't heard of either one of these terms, could you elaborate?
Is the DisneyModel the part where it SELECT * from table?
and $view is the math to display it?

Trying to understand...

Re: php5OO it?

Posted: Thu Mar 30, 2006 10:48 pm
by Christopher
This stuff goes in the DAO

Code: Select all

$host  = "localhost";
$user  = "frosh_user";
$pass  = "frosh_pass";
$db     = "frosh_db";

$obj = mysqli_fetch_object($result)
$result 	= mysqli_query($dbh, $query) or die ("Error in query: $query. ".mysqli_error());
This stuff goes in your Model (I'd recommend passing $_GET['p_time'] to it):

Code: Select all

$TimeDiff 		= date('I');
$TimeZoneEpoc 	= time() - ($TimeDiff*60*60);
$time 		= date("g:i:s A", $TimeZoneEpoc);
$unixtime		= strtotime($time);

if (!isset($_GET['p_time'])) {
	$low_time	= floor($unixtime/1800)*1800;
}
else if (ctype_digit($_GET['p_time']) && strlen($_GET['p_time']) === 10) {
	$low_time 	= $_GET['p_time'];
} else {
	$low_time	= floor($unixtime/1800)*1800;
}

$low_timea 	= $low_time;
$high_time 	= $low_time + 7200;
$interval_time 	= ($high_time - $low_time);
$column_num	= ($high_time - $low_time)/3600;
$column_size 	= 100/$column_num;

echo "Time: $time<BR>";

$dbh		= mysqli_connect ($host, $user, $pass, $db) or die ('I cannot connect to the database because: ' . mysqli_error());
$query	= "SELECT * FROM disney";
$resultset = $dao->fetch($query)
while ($row = $resultset ->fetch()) {
}
This stuff goes in your View:

Code: Select all

echo 	"<table width=\"100%\" border=\"1\"><tr>";

for ($i = 0; $i < $column_num; $i++) {
	echo "<td width=\"".$column_size."%\">".date("g:i:s A", $low_timea)."</td>";			
	$low_timea+=3600;
}
echo "</tr></table>";
echo "<table width=\"100%\"  border=\"1\"><tr>";

$entries = $model->findAll();
foreach ($entries as $obj) {
	$item_uid		= $obj->tv_uid;
	$item_title  	= $obj->tv_title;
	$item_desc 	= $obj->tv_desc;
	$item_pub		= $obj->tv_pub;
	$item_upubs	= $obj->tv_unixtimestamp_start;
	$item_upube 	= $obj->tv_unixtimestamp_end;


	if ($item_upubs < $low_time && $item_upube > $high_time) {
		$width = (($high_time-$low_time)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title. " bstart ". $item_pub ."</td>";
	}
	if ($item_upubs < $low_time && $item_upube > $low_time && $item_upube < $high_time) {
		$width = (($item_upube-$low_time)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title ."</td>";
	}
	if ($item_upubs == $low_time) {
		$width = (($item_upube-$item_upubs)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title. "</td>";
	}
	if ($item_upubs > $low_time && $item_upube < $high_time) {
		$width = (($item_upube-$item_upubs)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title. "</td>";
	}
	if ($item_upubs < $high_time && $item_upube == $high_time) {
		$width = (($item_upube-$item_upubs)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title. "</td>";
	}
	if ($item_upubs > $low_time && $item_upubs < $high_time && $item_upube > $high_time) {
		$width = (($item_upube-$item_upubs)/($high_time-$low_time))*100;
		echo "<td width=\"".$width."%\">". $item_title. "</td>";
	}
}
echo "</tr></table>";
echo "<a href=\"".$_SERVER['PHP_SELF']."?p_time=".($low_time-7200)."\">PREVIOUS</a>";
echo " || ";
echo "<a href=\"".$_SERVER['PHP_SELF']."?p_time=".($high_time)."\">NEXT</a>";
Try to make classes out of them.

You will notice that all the database specific calls are in the DAO, but no SQL. The application specific SQL, database access and calculations are in the Model. And the View only deals with variables, like the array of objects it gets from the Model.

Thanks...

Posted: Fri Mar 31, 2006 10:43 am
by Frosh
Thanks...for giving me a good kick in the right direction, I'll look into doing something, I'll post the statues as I move toward completing it...Hopefully I'll complete it...

Just a quick question, should I have a lot of $this-> inside my MODEL? for each of my variable?

Have a Nice DAy!

Re: Thanks...

Posted: Fri Mar 31, 2006 11:49 am
by Christopher
Frosh wrote:Thanks...for giving me a good kick in the right direction, I'll look into doing something, I'll post the statues as I move toward completing it...Hopefully I'll complete it...

Just a quick question, should I have a lot of $this-> inside my MODEL? for each of my variable?

Have a Nice DAy!
I don't think you need as many as you think. For the Model, I would put this in the constructor:

Code: Select all

if (!isset($_GET['p_time'])) {
    $this->low_time    = floor($unixtime/1800)*1800;
}
else if (ctype_digit($_GET['p_time']) && strlen($_GET['p_time']) === 10) {
    $this->low_time     = $_GET['p_time'];
} else {
    $this->low_time    = floor($unixtime/1800)*1800;
}
And then create an accessor function (e.g. getTimeDiff() ) for each of these:

Code: Select all

$TimeDiff         = date('I');
$TimeZoneEpoc     = time() - ($TimeDiff*60*60);
$time         = date("g:i:s A", $TimeZoneEpoc);
$unixtime        = strtotime($time);

$low_timea     = $this->low_time;
$high_time     = $this->low_time + 7200;
$interval_time     = ($high_time - $this->low_time);
$column_num    = ($high_time - $this->low_time)/3600;
$column_size     = 100/$column_num;
This is still fairly procedural use of OO, but getting there. After you get this working, You might want to search for "OOP Tell, Don't Ask" to learn a little more.

SitE?

Posted: Fri Mar 31, 2006 12:39 pm
by Frosh
Do you happen to mean this site?
http://www.pragmaticprogrammer.com/ppll ... 98_05.html

Haven't read it yet, but it's bookmarked :D

Re: SitE?

Posted: Fri Mar 31, 2006 1:35 pm
by Christopher
Frosh wrote:Do you happen to mean this site?
Yes. If you can make an effort to even start to understand the ideas in that article -- it will help you with OOP.

VAlid?

Posted: Tue Apr 04, 2006 1:12 pm
by Frosh
Is this class valid, like does it work, for some reason I'm not sure if $query would work, since I did not pass it through the constructor, should I.
I was thinking

$query="SELECT * FROM DISNEY";
$a = new mysql_class();
$a->query($query);

Would this class work?
ANy other ideas, tips, advice would be greatly appreciated.

Code: Select all

class mysql_class {
	private $host  = "localhost";
	private $user  = "frosh_user";
	private $pass  = "frosh_pass";
	private $db    = "frosh_db";
	public $query;
        public $dbh;

	public function __construct($host, $user, $pass, $db) {
		$this->host = $host;
		$this->user = $user;
		$this->pass = $pass;
		$this->db	= $db;
		$this->$dbh = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die("Unable to connect");
	}

	public function query($query) {
		return mysqli_query($dbh, $query) or die("Error in $query". mysqli_error());
	}
	
	public function __destruct() {
	mysqli_close($this->dbh);
	}
}

Posted: Tue Apr 04, 2006 1:17 pm
by feyd
__construct not __constructor

and your query method has magically used $dbh, a nonexistant variable.

Posted: Tue Apr 04, 2006 1:32 pm
by John Cartwright
your constructor and query methods should not be private, since they are going to be called from outside the class scope.

Posted: Tue Apr 04, 2006 1:35 pm
by feyd
Jcart wrote:your constructor and query methods should not be private, since they are going to be called from outside the class scope.
the destructor too.

How about now?

Posted: Wed Apr 05, 2006 1:56 am
by Frosh
How about now? Is the $dbh in the right way?

Code: Select all

class mysql_class {
    private $host  = "localhost";
    private $user  = "frosh_user";
    private $pass  = "frosh_pass";
    private $db    = "frosh_db";
    public $query;
    private $dbh;

    public function __construct($host, $user, $pass, $db) {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->db    = $db;
        $this->$dbh = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die("Unable to connect");
    }

    public function query($query) {
        return mysqli_query($this->dbh, $query) or die("Error in $query". mysqli_error());
    }
    
    public function __destruct() {
    mysqli_close($this->dbh);
    }
}