Jump to content

Search the Community

Showing results for tags 'array_shift'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Welcome to PHP Freaks
    • Announcements
    • Introductions
  • PHP Coding
    • PHP Coding Help
    • Regex Help
    • Third Party Scripts
    • FAQ/Code Snippet Repository
  • SQL / Database
    • MySQL Help
    • PostgreSQL
    • Microsoft SQL - MSSQL
    • Other RDBMS and SQL dialects
  • Client Side
    • HTML Help
    • CSS Help
    • Javascript Help
    • Other
  • Applications and Frameworks
    • Applications
    • Frameworks
    • Other Libraries
  • Web Server Administration
    • PHP Installation and Configuration
    • Linux
    • Apache HTTP Server
    • Microsoft IIS
    • Other Web Server Software
  • Other
    • Application Design
    • Other Programming Languages
    • Editor Help (PhpStorm, VS Code, etc)
    • Website Critique
    • Beta Test Your Stuff!
  • Freelance, Contracts, Employment, etc.
    • Services Offered
    • Job Offerings
  • General Discussion
    • PHPFreaks.com Website Feedback
    • Miscellaneous

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests


Age


Donation Link

Found 1 result

  1. Hey, I am new to PHP and I'm trying to understand this tutorial that instanicates a row, and assigns it to a $object[] array through a method called find_by_sql. There is another method that is called find_by_id that uses the find_by_sql method, the difference is it just finds the array through an ID. My question is why does the find_by_id method returns the result set using an array_shift? Below is the code. Any help will be highly appreciated! :happy-04: The Class starts with the user attributes.. they get assigned by the instantiate method below. class User { //Object attributes are below public $id; public $username; public $password; public $first_name; public $last_name; Below is the find_by_sql and find_by_id method. /* 1) The following method below does the SQL finding based on an arguement that i passed in 2) It then preforms a fetch array on the result set and instantiates it with the instantiate method below and assigns it to the $object_array */ public static function find_by_sql($sql="") { global $database; // Brings in the $database instanitated object. $result_set = $database->query($sql); // Runs the query on the $sql argument $object_array = array(); // Initilizes an object_array while ($row = $database->fetch_array($result_set)) { $object_array[] = self::instantiate($row); // Take the instanciated row and assignes it to $object_array } return $object_array; } public static function find_by_id($id=0) { // Takes an ID as an argument $result_array = self::find_by_sql("SELECT * FROM users WHERE id={$id} LIMIT 1"); return !empty($result_array) ? array_shift($result_array) : false; // Why does it take the first element out of the array ??? } Below is the instantiate method that instantiates a row and assigns it to the attriubutes above private static function instantiate($record) { // Could check that $record exists and is an array $object = new self; // Instanciates the user calss to $object // Simple, long-form approach: // $object->id = $record['id']; // $object->username = $record['username']; // $object->password = $record['password']; // $object->first_name = $record['first_name']; // $object->last_name = $record['last_name']; // More dynamic, short-form approach: foreach($record as $attribute=>$value){ if($object->has_attribute($attribute)) { // the has_attribute is a function I had to write below the checks if the attributes are in the object first. $object->$attribute = $value; } } return $object; } private function has_attribute($attribute) { // get_object_vars returns an associative array with all attributes // (incl. private ones!) as the keys and their current values as the value $object_vars = get_object_vars($this); // We don't care about the value, we just want to know if the key exists // Will return true or false return array_key_exists($attribute, $object_vars); } }// end of user class
×
×
  • 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.