Jump to content

andy_b42

Members
  • Posts

    34
  • Joined

  • Last visited

    Never

Everything posted by andy_b42

  1. <?php $userInput = "123456789"; // make the input a string if it is not already function checkDivisibleByEleven($userInput, $leftToRight){ // $leftToRight is a boolean variable $sum = 0; $count = 0; if($leftToRight){ $oddMultiplier = 1; $evenMultipler = 2; }else{ $oddMultiplier = 2; $evenMultipler = 1; } while($userInput[$count] != ""){ $currentVal = $userInput[$count]; if($count%2 == 1){ $toAdd = $currentVal * $evenMultiplier; if($toAdd > 9){ $toAddString = $toAdd; $i = 0; while($toAddString[$i] != ""){ $toAdd += $toAddString[$i]; $i++; } } $sum += $toAdd; echo "ODD current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; }else{ $toAdd = $currentVal * $oddMultiplier; if($toAdd > 9){ $toAddString = $toAdd; $i = 0; while($toAddString[$i] != ""){ $toAdd += $toAddString[$i]; $i++; } } $sum += $toAdd; echo "EVEN current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; } $count++; } if($sum%11 == 0){ echo $userInput . ": Sum's to " . $sum . ": is divisible by 11"; }else{ echo $userInput . ": Sum's to " . $sum . ": is <b>NOT</b> divisible by 11"; } } checkDivisibleByEleven($userInput, true); checkDivisibleByEleven($userInput, false); ?>
  2. I think this will do it <?php $userInput = "123456789"; // make the input a string if it is not already function checkDivisibleByEleven($userInput){ $sum = 0; for($count = 0; $count < 9; $count++){ $currentVal = $userInput[$count]; if($count%2 == 1){ $toAdd = $currentVal * 2; if($toAdd > 9){ $toAddString = $toAdd; $i = 0; while($toAddString[$i] != ""){ $toAdd += $toAddString[$i]; $i++; } } $sum += $toAdd; //echo "ODD current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; }else{ $toAdd = $currentVal * 1; if($toAdd > 9){ $toAddString = $toAdd; $i = 0; while($toAddString[$i] != ""){ $toAdd += $toAddString[$i]; $i++; } } $sum += $toAdd; //echo "EVEN current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; } } if($sum%11 == 0){ echo $userInput . ": Sum's to " . $sum . ": is divisible by 11"; }else{ echo $userInput . ": Sum's to " . $sum . ": is <b>NOT</b> divisible by 11"; } } checkDivisibleByEleven($userInput); ?>
  3. my sincere apologies for wasting everyones time... I fell like a complete idiot, in the User constructor i had called the load() before creating the $db object Original: public function User($intID = 0){ if($intID > 0){ $this->load($intID); }else{ //echo "No " . $this->currentClass . "ID information supplied"; } try{ $this->db = new Database(); } catch(Exception $exc){ echo "Error creating database object"; } } Should be: public function User($intID = 0){ try{ $this->db = new Database(); } catch(Exception $exc){ echo "Error creating database object"; } if($intID > 0){ $this->load($intID); }else{ //echo "No " . $this->currentClass . "ID information supplied"; } }
  4. i was under the impression that global variables are not very secure. Also, is there a chance that if i use 2 or more objects on the same page, the $db variables (for simplicity sake i would like to call it the same thing in every object) would get mixed up? It is quite important that each instance of the $db variable is unique to the object becasue the Database class holds the results of the query which are then used by the object if they are required.
  5. Thanks, that line was like that becuase the only way i can make this work was to create the $db object outside the calss and then inside the function, use global $db and the call it as though $db were a variable local to that method.
  6. <?php $userInput = "123456789"; // make the input a string if it is not already function checkDivisibleByEleven($userInput){ $sum = 0; for($count = 0; $count < 9; $count++){ $currentVal = $userInput[$count]; if($count%2 == 1){ $toAdd = $currentVal * 2; if($toAdd > 9){$toAdd = 1;} $sum += $toAdd; //echo "ODD current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; }else{ $toAdd = $currentVal * 1; if($toAdd > 9){$toAdd = 1;} $sum += $toAdd; //echo "EVEN current val = " . $currentVal . " to add = " . $toAdd . " sum = " . $sum . "<br/>"; } } if($sum%11 == 0){ echo $userInput . ": Sum's to " . $sum . ": is divisible by 11"; }else{ echo $userInput . ": Sum's to " . $sum . ": is <b>NOT</b> divisible by 11"; } } checkDivisibleByEleven($userInput); ?> I think that should do it all, i havent fully tested it, buy i think its ok
  7. Fatal error: Call to a member function Query() on a non-object
  8. Thanks for the quick replies The objects i am actually using are a lot more complex than what i have posted here: below are the database and user objects http://andyb42.servebeer.com:4567/phpfreaks/database.txt http://andyb42.servebeer.com:4567/phpfreaks/user.txt The user is instantiated like this "$user = new User($intID);", the constructor then does all of the work filling in the users data from the database.
  9. I am fairly new to using objects in php. I have made a fairly simple database object so i can just call one of a few of my own methods to automatically make changes to the database when i change the relevant object. Below is a very simplified version of what i am trying to accomplish: Class Database{ public function Query($query){ //connect to database, run query and return result } } Class User{ private $username; private $db; public function __construct($intID){ $this->db = new Database(); if($intID > 0){ getUserData($intID); } } public function getUserData($intID){ $sql = "select * from users where intID = $intID"; return $this->db->Query($sql); } } The problem i get is when "$this->db->Query($sql);" is executed and the error i get is: "Fatal error: Call to a member function Query() on a non-object" According to the IDE i am using, the function is accessible, but it seems when running the code, the parser cannot see that $this->db is an object with functions. My question is, is what i am attempting to do possible or am I missing something very basic to make this work? Thanks
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.