php5OO it?

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

Frosh
Forum Newbie
Posts: 12
Joined: Thu Mar 30, 2006 5:36 pm

php5OO it?

Post 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>"; 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
Frosh
Forum Newbie
Posts: 12
Joined: Thu Mar 30, 2006 5:36 pm

Why?

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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());
(#10850)
Frosh
Forum Newbie
Posts: 12
Joined: Thu Mar 30, 2006 5:36 pm

Lost?

Post 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...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: php5OO it?

Post 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.
(#10850)
Frosh
Forum Newbie
Posts: 12
Joined: Thu Mar 30, 2006 5:36 pm

Thanks...

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Thanks...

Post 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.
Last edited by Christopher on Fri Mar 31, 2006 1:33 pm, edited 1 time in total.
(#10850)
Frosh
Forum Newbie
Posts: 12
Joined: Thu Mar 30, 2006 5:36 pm

SitE?

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: SitE?

Post 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.
(#10850)
Frosh
Forum Newbie
Posts: 12
Joined: Thu Mar 30, 2006 5:36 pm

VAlid?

Post 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);
	}
}
Last edited by Frosh on Wed Apr 05, 2006 1:52 am, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

__construct not __constructor

and your query method has magically used $dbh, a nonexistant variable.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

your constructor and query methods should not be private, since they are going to be called from outside the class scope.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Frosh
Forum Newbie
Posts: 12
Joined: Thu Mar 30, 2006 5:36 pm

How about now?

Post 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);
    }
}
Last edited by Frosh on Wed Apr 05, 2006 9:49 am, edited 1 time in total.
Post Reply