Page 2 of 2
Posted: Wed Apr 05, 2006 7:41 am
by John Cartwright
Why do you have a query property? No need for this..
And $dbh should be set to private/protected since this should not be accessed outside the classes scope.
feyd wrote: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.
Oops missed that.

Posted: Wed Apr 05, 2006 9:51 am
by Frosh
ok, I change $dbh to private...anymore fixes?
I'm starting to get the idea!

Posted: Sun Apr 09, 2006 1:24 pm
by Frosh
How can I fix line 70?
Fatal error: Call to undefined function fetch_object() in /home/frosh/public_html/projects/tv/oo/mysql_class.php on line 70
Code: Select all
<?php
class mysql_class {
public $query;
public $dbh;
public function __construct() {
$this->host = "localhost";
$this->user = "frosh_rss";
$this->pass = "rss";
$this->db = "frosh_sandbox";
}
public function connect() {
$this->dbh = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die("Unable to connect");
}
public function query($query) {
$this->query_result = mysqli_query($this->dbh, $query) or die("Error in $query". mysqli_error());
return $this->query_result;
}
public function fetch_object() {
$this->query_fetch_object = mysqli_fetch_object($this->query_result);
return $this->query_fetch_object;
}
public function __destruct() {
mysqli_close($this->dbh);
}
}
class math_class {
public $something;
public function __construct() {
$this->TimeDiff = date('I');
$this->TimeZoneEpoc = time() - ($this->TimeDiff*60*60);
$this->time = date("g:i:s A", $this->TimeZoneEpoc);
$this->unixtime = strtotime($this->time);
if (!isset($_GET['p_time'])) {
$this->low_time = floor($this->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($this->unixtime/1800)*1800;
}
$this->low_timea = $this->low_time;
$this->high_time = $this->low_time + 7200;
$this->interval_time= ($this->high_time - $this->low_time);
$this->column_num = ($this->high_time - $this->low_time)/3600;
$this->column_size = 100/$this->column_num;
}
public function tv_display($something) {
echo "<table width=\"100%\" border=\"1\"><tr>";
for ($i = 0; $i < $this->column_num; $i++) {
echo "<td width=\"".$this->column_size."%\">".date("g:i:s A", $this->low_timea)."</td>";
$this->low_timea+=3600;
}
echo "</tr></table>";
echo "<table width=\"100%\" border=\"1\"><tr>";
while ($obj = fetch_object($something)) {
$this->item_uid = $obj->tv_uid;
$this->item_title = $obj->tv_title;
$this->item_desc = $obj->tv_desc;
$this->item_pub = $obj->tv_pub;
$this->item_upubs = $obj->tv_unixtimestamp_start;
$this->item_upube = $obj->tv_unixtimestamp_end;
if ($this->item_upubs < $this->low_time && $this->item_upube > $this->high_time) {
$width = (($this->high_time-$this->low_time)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title. " bstart ". $this->item_pub ."</td>";
}
if ($this->item_upubs < $this->low_time && $this->item_upube > $this->low_time && $this->item_upube < $this->high_time) {
$width = (($this->item_upube-$this->low_time)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title ."</td>";
}
if ($this->item_upubs == $this->low_time) {
$width = (($this->item_upube-$this->item_upubs)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title. "</td>";
}
if ($this->item_upubs > $this->low_time && $this->item_upube < $this->high_time) {
$width = (($this->item_upube-$this->item_upubs)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title. "</td>";
}
if ($this->item_upubs < $this->high_time && $this->item_upube == $this->high_time) {
$width = (($this->item_upube-$this->item_upubs)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title. "</td>";
}
if ($this->item_upubs > $this->low_time && $this->item_upubs < $this->high_time && $this->item_upube > $this->high_time) {
$width = (($this->item_upube-$this->item_upubs)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title. "</td>";
}
}
echo "</tr></table>";
echo "<a href=\"".$_SERVER['PHP_SELF']."?p_time=".($this->low_time-7200)."\">PREVIOUS</a>";
echo " || ";
echo "<a href=\"".$_SERVER['PHP_SELF']."?p_time=".($this->high_time)."\">NEXT</a>";
}
}
$query = "SELECT * FROM disney";
$hi = new mysql_class;
$bye = new math_class;
$hi->connect();
$hi->query($query);
var_dump($hi->fetch_object());
$s1 = $hi->fetch_object();
$bye->tv_display($s1);
?>
Posted: Sun Apr 09, 2006 1:42 pm
by feyd
what are you trying to use fetch_object() from? mysql_class? tv_display() doesn't know an object of that type.
Posted: Sun Apr 09, 2006 2:00 pm
by Frosh
Does't line 70 call up to line 23? and get the results?
Posted: Sun Apr 09, 2006 2:05 pm
by Chris Corbyn
Frosh wrote:Does't line 70 call up to line 23? and get the results?
You missed the $this:
Code: Select all
while ($obj = fetch_object($something)) {
Posted: Sun Apr 09, 2006 2:07 pm
by feyd
nope, it will not call into mysql_class, nor will using $this (it's not a method of the object)
tv_display() does not have a reference to any instances of mysql_class therefore cannot call the method.
Posted: Sun Apr 09, 2006 2:10 pm
by Chris Corbyn
feyd wrote:nope, it will not call into mysql_class, nor will using $this (it's not a method of the object)
tv_display() does not have a reference to any instances of mysql_class therefore cannot call the method.
Oops I totally didn't notice there were two separate classes

In that case what I said above would only apply if you used the "extends" keyword to make math_class inherit from mysql_class.
Posted: Sun Apr 09, 2006 2:34 pm
by Frosh
Ok, I extended it, i think, still can't call into mysql_class?
Am I doing this wrong?
Any more advice?
Code: Select all
<?php
class mysql_class {
public $query;
public $dbh;
public function __construct() {
$this->host = "localhost";
$this->user = "frosh_rss";
$this->pass = "rss";
$this->db = "frosh_sandbox";
}
public function connect() {
$this->dbh = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die("Unable to connect");
}
public function query($query) {
$this->query_result = mysqli_query($this->dbh, $query) or die("Error in $query". mysqli_error());
return $this->query_result;
}
public function fetch_object($this->query_result) {
$this->query_fetch_object = mysqli_fetch_object($this->query_result);
return $this->query_fetch_object;
}
public function __destruct() {
mysqli_close($this->dbh);
}
}
class math_class extends mysql_class{
public $something;
public function __construct() {
$this->TimeDiff = date('I');
$this->TimeZoneEpoc = time() - ($this->TimeDiff*60*60);
$this->time = date("g:i:s A", $this->TimeZoneEpoc);
$this->unixtime = strtotime($this->time);
if (!isset($_GET['p_time'])) {
$this->low_time = floor($this->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($this->unixtime/1800)*1800;
}
$this->low_timea = $this->low_time;
$this->high_time = $this->low_time + 7200;
$this->interval_time= ($this->high_time - $this->low_time);
$this->column_num = ($this->high_time - $this->low_time)/3600;
$this->column_size = 100/$this->column_num;
}
public function tv_display($something) {
echo "<table width=\"100%\" border=\"1\"><tr>";
for ($i = 0; $i < $this->column_num; $i++) {
echo "<td width=\"".$this->column_size."%\">".date("g:i:s A", $this->low_timea)."</td>";
$this->low_timea+=3600;
}
echo "</tr></table>";
echo "<table width=\"100%\" border=\"1\"><tr>";
while ($obj = fetch_object($something)) {
$this->item_uid = $obj->tv_uid;
$this->item_title = $obj->tv_title;
$this->item_desc = $obj->tv_desc;
$this->item_pub = $obj->tv_pub;
$this->item_upubs = $obj->tv_unixtimestamp_start;
$this->item_upube = $obj->tv_unixtimestamp_end;
if ($this->item_upubs < $this->low_time && $this->item_upube > $this->high_time) {
$width = (($this->high_time-$this->low_time)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title. " bstart ". $this->item_pub ."</td>";
}
if ($this->item_upubs < $this->low_time && $this->item_upube > $this->low_time && $this->item_upube < $this->high_time) {
$width = (($this->item_upube-$this->low_time)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title ."</td>";
}
if ($this->item_upubs == $this->low_time) {
$width = (($this->item_upube-$this->item_upubs)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title. "</td>";
}
if ($this->item_upubs > $this->low_time && $this->item_upube < $this->high_time) {
$width = (($this->item_upube-$this->item_upubs)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title. "</td>";
}
if ($this->item_upubs < $this->high_time && $this->item_upube == $this->high_time) {
$width = (($this->item_upube-$this->item_upubs)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title. "</td>";
}
if ($this->item_upubs > $this->low_time && $this->item_upubs < $this->high_time && $this->item_upube > $this->high_time) {
$width = (($this->item_upube-$this->item_upubs)/($this->high_time-$this->low_time))*100;
echo "<td width=\"".$width."%\">". $this->item_title. "</td>";
}
}
echo "</tr></table>";
echo "<a href=\"".$_SERVER['PHP_SELF']."?p_time=".($this->low_time-7200)."\">PREVIOUS</a>";
echo " || ";
echo "<a href=\"".$_SERVER['PHP_SELF']."?p_time=".($this->high_time)."\">NEXT</a>";
}
}
$query = "SELECT * FROM disney";
$hi = new mysql_class;
$bye = new math_class;
$hi->connect();
$hi->query($query);
var_dump($hi->fetch_object());
$s1 = $hi->fetch_object();
$bye->tv_display($s1);
?>
Re: How about now?
Posted: Sun Apr 09, 2006 5:13 pm
by Chris Corbyn
Frosh wrote: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);
}
}
Silly mistake I guess but you have the parameters for mysqli_query() back-to-front. The query should be first, not second
PS: Youre extension won't run the constructor in the parent since __construct() exists in both classes it only runs for the one you instantiate. The practise to solve that is to manually call the parent constructor using the "parent::" syntax.
Code: Select all
class class1
{
public function __construct()
{
//This would only run if we instantiate *this* class
}
}
class class2 extends class1
{
public function __construct()
{
parent::__construct(); //Run the parent constructor
//Other logic here
}
}
$obj = new class2;
Posted: Sun Apr 09, 2006 5:23 pm
by Chris Corbyn
... and you've still left out this $this:
Code: Select all
while ($obj = fetch_object($something)) {
//Should be:
while ($obj = $this->fetch_object($something)) {
Posted: Sun Apr 09, 2006 6:25 pm
by Frosh
Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ')' in /home/frosh/public_html/projects/tv/oo/mysqlclass.php on line 23
I search and something about $this-> is incorrect? yet I don't see where it would be wrong? I don't think I'm missing some other marks like )
Posted: Sun Apr 09, 2006 8:45 pm
by Christopher
This is a good example where composition would be better than inheritance. Something like:
Code: Select all
$sql = "SELECT * FROM disney";
$db = new mysql_class();
$db->connect();
$db->query($sql);
$math = new math_class();
$math->tv_display($db);
The code inside math_class would be something like:
Code: Select all
#
public function tv_display($db) {
...
while ($obj = $db->fetch_object($something)) {
Although you are spreading dependencies all over the place. Better would be to make a Gateway class that fetched all of the records and then pass that to your math class, like:
Code: Select all
public function tv_display($model) {
...
$objects = $model->fetchAll()) {
foreach ($objects as $obj) {