In this section, we will kill two birds a stone. Not only will we learn how to target object-oriented PHP and MySQL, but we will learn how to group objects. In this case, you will build a first-class usersgroup which will contain a series of user objects. Each user object will be established for continuous use from MySQL query. To try this practical example, you first need to build a user table in the MySQL database. I'm using the MySQL database named ' Kirupa_oop '. Use the following query to create a table and insert sample data.
/* Create Users table */
CREATE TABLE `users` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`user_name` TEXT NOT NULL,
PRIMARY KEY (`user_id`)
);
/* Insert sample data into Users table */
INSERT INTO `users`
( `user_id`, `user_name` )
VALUES
( '', 'kirupa' ),
( '', 'bwh2' );
This very simple form only has two columns: ' user_id ' and ' username '. You may wish to add your own name to this table for spice things up.
As promised, we will create a Usersgroup class. The workgroup will contain a series of user objects, each of which creates a table using data from our MySQL. The database on all my forms is named ' Kirupa_oop '. If your database does not have a name of ' Kirupa_oop ', you must change the setting class in the database name as shown. Here is the action of our Usersgroup class:
$group = new Usersgroup;
/* Loop through our group, echo user names * *
foreach ($group->getusers () as $user) {
echo $user->getname (). ' <br/> ';
}
/* User class, same as before * *
Class User {
Private $name;
function __construct ($attribs) {
$this->name = $attribs [' name '];
}
/* Name Methods * *
function SetName ($val) {
$this->name = $val;
Return
}
function GetName () {
return $this->name;
}
}
/* Contains A group of User objects * *
Class Usersgroup {
Private $name; Name of Group
Private $group = Array ();//Group of User objects
function __construct () {
/* Connect to DB using Settings */
$link = mysql_connect (
Settings:: $DATABASE [' Host '],
Settings:: $DATABASE [' username '],
Settings:: $DATABASE [' Password ']
);
mysql_select_db (Settings:: $DATABASE [' DATABASE '], $link);
/* Get table names from Settings class * *
$tbl _users = Settings:: $TABLES [' tbl_users '];
/* Query * *
$sql = "selectuser_id as ID,
User_name as Name
From $tbl _users ";
$result = mysql_query ($sql) or Die (Mysql_error ());
/* Adds user to group with each row of data */
while ($row = Mysql_fetch_array ($result)) {
$this->adduser ($row);
}
}
/*
Add a user to Group
Does simple check to the if we pass a array (like $attribs)
Or if we pass is object (like a User object)
*/
function AddUser ($user) {
if (Is_object ($user)) {
Array_push ($this->group, $user);
}
if (Is_array ($user)) {
$noob = new User ($user);
Array_push ($this->group, $noob);
}
Return
}
/* Returns the group (which is a array) * *
function Getusers () {
return $this->group;
}
}
* * Holds our Site Settings */
Class Settings {
Static $DATABASE = Array (
Change these as needed ' database ' => ' Kirupa