Jump to content

exeTrix

Members
  • Posts

    53
  • Joined

  • Last visited

Everything posted by exeTrix

  1. Everybody has to start somewhere and as you said the above certainly does the job. That said, writing a script which performs a task is only half of the job. Other questions you need to ask are: what if I needed to change my code? Would it be easy to do? Another important one is am I duplicating code? Or can the script be structured in a way in which commonly used logic/functionality? The answer to all of these questions is objects. Consider this: //function to work out bhp function calculateBhp( $engineSize ){ //logic for working out bhp return 90; } //define our car $car = array( 'make' => 'honda', 'engineSize' => 1.4, 'doors' => 3 ); //echo out to the user echo $car['make']; echo $car['engineSize']; echo calculateBhp( $car['engineSize'] ); ... Cool, so we have a function that works out engine size and we print a few array elements to the user. That's fine, but what if we wanted to use that function in another file? It'd have to be included in. Also, what if certain manufacturers used different values to work out bhp? Then we'd have to have multiple functions which all applied different calculations... this is when code would start to get hard to manage. Here's the same functionality but ported to OO: class Car{ public $make; public $engineSize; public $doors; public function calculateBhp(){ //logic to work out bhp return 90; } public function getMake(){ return $this->make; } } class Honda extends Car{ public $make = 'honda' public function calculateBhp(){ //different logic to work out bhp return 100; } public function getMake(){ //here we're calling the method in Car which will return Honda $str = parent::getMake(); //now we're just appending a space then the engine size return $str . ' ' . $this->engineSize; } } class Ford extends Car{ public $make = 'ford'; } //now we have our classes defined lets get some instances $honda = new Honda(); $honda->engineSize = 1.4; $ford = new Ford(); $ford->engineSize = 1.2; echo $honda->getMake(); //returns honda 1.4 echo $honda->calculateBhp(); //returns 100 echo $ford->getMake(); //returns ford because unlike the Honda class there is no override in the Ford class echo $ford->calculateBhp(); //returns 90 As you can see code is becoming separated out, therefore, any changes that need to be made which as specific to a manufacturer it'd be easy to implement new methods or override existsing ones. Any questions then give me a shout Happy learning One thing I noticed is on your else if you're checking if the string stored within $car is "honda" but you're displaying the ford form.
  2. No MySQL will not define unique indexes without you telling it to do so via ALTER TABLE or CREATE TABLE statements. Also, you're using a depreciated method of connecting to your database. Have you considered using PDO? It's more secure and fun! No need to use mysql_real_escape_string and it'll build on you OO knowledge Sorry if I've missed anything: try{ //get our database handle $db = new PDO( 'mysql:dbname=testdb;host=127.0.0.1', 'Username', 'Password' ); //prepare our statament ready for values to be bound $stmt = $db->prepare(" INSERT INTO `ecmt_memberlist` ( characterID, name, startDateTime, baseID, base, title, logonDateTime, logoffDateTime, locationID, location, shipTypeID, shipType, roles, grantableRoles, last_modified ) VALUES ( :characterID, :name, :startDateTime, :baseID, :base, :title, :logonDateTime, :logoffDateTime, :locationID, :location, :shipTypeID, :shipType, :roles, :grantableRoles, :modifiedTS ) ON DUPLICATE KEY UPDATE name = VALUE(name), startDateTime = VALUE(startDateTime), baseID = VALUE(baseID), base = VALUE($base), title = VALUE(title), logonDateTime = VALUE(logonDateTime), logoffDateTime = VALUE(logoffDateTime), locationID = VALUE(locationID), location = VALUE(location), shipTypeID = VALUE(shipTypeID), shipType = VALUE(shipType), roles = VALUE(roles), grantableRoles = VALUE(grantableRoles), last_modified = VALUE(modifiedTS) "); //create our object for binding $ef = new stdClass(); $ef->characterID; $ef->name; $ef->baseID; $ef->base; $ef->title; $ef->logonDateTime; $ef->logoffDateTime; $ef->locationID; $ef->location; $ef->locationID; $ef->shipTypeID; $ef->shipType; $ef->roles; $ef->grantableRoles; //bind the params to the object properties //the binding is done by reference so when the value of the property changes it will be retrieved when execute is called $stmt->bindParam( ':characterID', $ef->characterID, PDO::PARAM_INT ); $stmt->bindParam( ':name', $ef->name, PDO::PARAM_STR ); $stmt->bindParam( ':baseID', $ef->baseID, PDO::PARAM_INT ); $stmt->bindParam( ':base', $ef->base, PDO::PARAM_STR ); $stmt->bindParam( ':title', $ef->title, PDO::PARAM_STR ); $stmt->bindParam( ':logonDateTime', $ef->logonDateTime, PDO::PARAM_STR ); $stmt->bindParam( ':logoffDateTime', $ef->logoffDateTime, PDO::PARAM_STR ); $stmt->bindParam( ':locationID', $ef->locationID, PDO::PARAM_INT ); $stmt->bindParam( ':location', $ef->location, PDO::PARAM_STR ); $stmt->bindParam( ':locationID', $ef->locationID, PDO::PARAM_INT ); $stmt->bindParam( ':shipTypeID', $ef->shipTypeID, PDO::PARAM_INT ); $stmt->bindParam( ':shipType', $ef->shipType, PDO::PARAM_INT ); $stmt->bindParam( ':roles', $ef->roles, PDO::PARAM_INT ); $stmt->bindParam( ':grantableRoles', $ef->grantableRoles, PDO::PARAM_INT ); //loop through our result set obtained via the API, I've left out the connection stuff foreach ($xml->result->rowset[0] as $value){ //reassign the new set of variables $ef = (object) $value; //execute the query $stmt->execute(); } }catch( PDOException $e ){ echo $e->getMessage(); } It's also worth mentioning that the above is trusting the api to return the correct fields. If you received more then clone the $ef object and reference it with property_exists.
  3. OO solution: //create a new DateTime instance with no value passed through to construct, equiv to date() $dateTimeObj = new DateTime(); //now we need to create an interval object for the number of hours we wish to deduct $intervalObj = DateInterval::createFromDateString( '5 hours' ); //subtract the interval from the DateTime object $dateTimeObj->sub( $intervalObj ); //format and output our date and time with the 5 hours subtracted echo $dateTimeObj->format( 'Y-m-d H:i:s a' ); //and if you want to add same process except you use the add method $dateTimeObj->add( $intervalObj ); //format and output our date and time with the 5 hours added echo $dateTimeObj->format( 'Y-m-d H:i:s a' ); Hope that helps
×
×
  • 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.