fife Posted September 13, 2013 Share Posted September 13, 2013 And it doesn't work! Im trying to write my first simple object which will just echo out a logo. I don't think the var $IDSystem is being passed through. As its my first attempted I'm struggling to see what I have wrong. class System{public $IDSystem = '1'; public function __construct() {$this->IDSystem = $IDSystem; } public function SystemLogo() { mysql_select_db($database_dbconnect, $dbconnect); $Query = sprintf("SELECT BSLogo, BSName FROM BusinessSettings WHERE IDBusinessSettings = %s", GetSQLValueString($this->IDSystem, "int")); $Query = mysql_query($Query, $dbconnect) or die(mysql_error()); $Result = mysql_fetch_assoc($Query); if(!empty($Result['BSLogo'])){ echo "<img src='/images/uploads/logo/{$Result['BSLogo']} width='220px' height='100px'/>"; } else {echo "<h1>{$Result['BSName']}</h1>"; } } } then on the page I'm writing $logo = new System;echo $logo->SystemLogo(); [/php] Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 13, 2013 Share Posted September 13, 2013 do you have php's error_reporting set to E_ALL and display_errors set to ON in your php,ini to get php to help you? there are some variables in the posted code that simply don't exist where they are being used and would be producing php errors to alert you to their non-existence. without the php detected error messages, you are flying blind. 1) inside the __construct() function, where are you expecting the $IDSystem variable, on the right-hand side in the following statement - $this->IDSystem = $IDSystem; to come from? the php variable $IDSystem that you have in the code inside the __construct() function, is local to and only exists inside the block of code for that function. for it to exist, you must either pass it into the function as a call time parameter or assign a value to it inside that function which would also create that variable. 2) inside the SystemLogo() function, where are you expecting the $database_dbconnect and $dbconnect variables to come from? if your class is dependent on a database connection to function at all, you need to create that database connection in your main code and pass it into the instance of the class, usually in the constructor when you create the instance of the class, and store it as a class property so that you can use it in the class functions/methods that need it. Quote Link to comment Share on other sites More sharing options...
fife Posted September 15, 2013 Author Share Posted September 15, 2013 Thanks for the advice. I have re-written my code and now I know the problem. I have updated the class to include a database connection which has been tested and works. The problem seems to be with my $IDSystem var is completely blank even though I'm setting it to 1 straight away. class System{ protected static $IDSystem = 1;private $db; public function System() { $this->db = new Database(); } public function SystemLogo() { $Query = sprintf("SELECT BSLogo, BSName FROM BusinessSettings WHERE IDBusinessSettings = %s", GetSQLValueString($IDSystem, "int")); $Query = $this->db->query($Query); $Result = $this->db->fetch_assoc($Query); if(!empty($Result['BSLogo'])){ echo "<img src='/images/uploads/logo/{$Result['BSLogo']} width='220px' height='100px'/>"; } else { echo "<h1>{$Result['BSName']}</h1>"; } } } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 15, 2013 Share Posted September 15, 2013 sorry to be blunt (see the disclaimer in my signature). you apparently didn't set php's error_reporting/display_errors as suggested and you are still flying blind and wasting time (rhyme not intentional.) in the first posted code, you correctly accessed the class's IDSystem property when forming the sql query statement - $this->IDSystem. in the last posted code, you randomly changed that so that it no longer is accessing the class's IDSystem property, but is instead assessing a php variable that is local to and only exists inside the the SystemLogo() method/function. you must know what $this->property_name and $this->method_name() means and how that differs from $variable_name and function_name(). Quote Link to comment Share on other sites More sharing options...
fife Posted September 15, 2013 Author Share Posted September 15, 2013 (edited) Hi mac__gyver and thanks for your reply again blunt or not I don't mind its all help. I did turn on error reporting i used ini_set('display_errors',1); error_reporting(E_ALL); which gives the following error Notice: Undefined variable: IDSystem in bla bla Now I've changed my function to include the database I'm struggling where to turn my $IDSystem into $this->IDSystem when I change my code to class System{ protected static $IDSystem = 1;private $db; //connect to databasepublic function System() { $this->db = new Database(); $this->IDSystem = $IDSystem;} //get the logo public function SystemLogo() { $Query = sprintf("SELECT BSLogo, BSName FROM BusinessSettings WHERE IDBusinessSettings = %s", GetSQLValueString($this->IDSystem, "int")); $Query = $this->db->query($Query);$Result = $this->db->fetch_assoc($Query); if(!empty($Result['BSLogo'])){echo "<img src='/images/uploads/logo/{$Result['BSLogo']} width='220px' height='100px'/>";} else {echo "<h1>{$Result['BSName']}</h1>";}}} I get the following: Notice: Undefined variable: IDSystem in which is now referring to: public function System() { $this->db = new Database(); $this->IDSystem = $IDSystem; } I apologies if i'm coming across as slow. This really is my first proper go at writing object. Edited September 15, 2013 by fife Quote Link to comment Share on other sites More sharing options...
Solution jcbones Posted September 15, 2013 Solution Share Posted September 15, 2013 (edited) Edit: this forum hates Chrome!!! Note that you should really pass the database object into the class, instead of creating it inside the class. This is so that you will have access to it inside and outside the class. Also, you should name your function System() to __construct(), as the former is sooooooo PHP4. It will not be treated as a constructor after PHP5.3.3. This function is expecting to see a variable $IDSystem, but you haven't defined it: public function System($IDSystem) { $this->db = new Database(); $this->IDSystem = $IDSystem; } //If you want it to have a default value: [code] public function System($IDSystem = 1) { $this->db = new Database(); $this->IDSystem = $IDSystem; } Edited September 15, 2013 by jcbones Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.