Sunday, March 22, 2009

ADODB Working and Interfaces

First and foremost, I was finally able to get ADODB working. Now I can connect to my database using adodb and access my database tables using adodb ActiveRecord. This is important because it abstracts away the messy and database-specific SQL code and allows me to use object-oriented programing. I've run multiple test scripts that add, edit, and remove items from tables using ActiveRecord, and they are all functioning properly.

After getting over this hump, I shifted
my focus to the other side of things...the interface. The first step in interface design is to layout a skeleton design for the critical web pages users will see. Thus far I've laid out the interface for the user dashboard and salon (manager) dashboard. In designing these interfaces, I came up with the idea of having a Facebook-style "newsfeed" for salons and users that will write relevant updates and news to the feed by pulling information out of the database based on date. I believe this feature will give my users a more rich, personalized experience. Also, the top navigation bar will feature mouse-over pull down menus so that the interface will be clean and minimal until a user seeks more information. Otherwise the interface will look cluttered. The other major interfaces will be the scheduling interface for the salon and for the user. For the salon, I will use the Google Calendars interface as well as their database functionality to present the full spectrum of calendar viewing and editing. For user scheduling, I will have a form that asks for the user to specify the neighborhood, date, time, service, and salon/stylist if applicable when searching for an appointment. I will display the results with my own interface rather than Google Calendars for this part. It will simply show the available salon(s) and the time(s) available at and near the requested time. One of the main purposes of designing the interfaces was to make sure that my database would be capturing all of the information my interface would need. Once I designed these skeleton interfaces, I realized I would need a "Promotions" table that kept track of new deals and sales salons decide to offer. This information would be needed for the news feed, which could then pull these deals out of the database based on relevance (location, dates, etc) and serve them to users on the news feed. Thus, I created a new table in my salonbook database to support this information.

Sunday, March 1, 2009

ADODB

ADODB is a database abstraction layer that allows developers to write portable code that is not tied down to one particular type of database. It also has an ActiveRecord feature which turns database rows into objects. That lets me work with object-oriented programming rather than tediously writing and rewriting MySQL queries. I've spent the last 10 days familiarizing myself with these tools and experimenting with them.

The first thing I did was research and learn to connect to my database with the tools built into PHP. I've successfully written a couple scripts that can connect to my database, create a PHP object, and perform an insert into the database, using the properties of that object as the field values for the table. One script looked like this:


class stylist{

public $first = 'not set';
public $last = 'not set';
public $email = 'not set';
public $password = 'not set';

function stylist($firstname, $lastname, $emailaddress, $pass){
$this->first = $firstname;
$this->last = $lastname;
$this->email = $emailaddress;
$this->password = $pass;

}

function insert(){
mysql_query("INSERT INTO stylist (first, last, email, password) 
VALUES ('$this->first', '$this->last', '$this->email', '$this->password')");
}
}

$con = mysql_connect("localhost","rjroth","aZsXdCf");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("salonbook", $con);

//Create new stylist objects
$bob = new stylist('Bob', 'Vance', 'Bob@Bob', 'BV');
$jane = new stylist('Jane', 'Dance', 'Jane@Bob', 'JD');
//Call the function that executes the MySQL query
$bob->insert();
$jane->insert();

mysql_close($con);

?>


This was great, but because the insert() function uses a hard-coded MySQL query, this code would be useless if my data were moved to a different database. That's where ADODB comes in. Useful ADODB tutorials and examples were not easy to come by on the internet, but I was able to piece together a cursory understanding that allowed me to use ADODB to perform some simple scripts that can get and set data to MySQL. Here is one example of a script that SELECTS * from my "state" table and prints each element on a separate line and numbers them:


include("/Library/WebServer/Documents/adodb/adodb.inc.php");
 $db = NewADOConnection('mysql');
 $db->Connect("localhost", "rjroth", "aZsXdCf", "salonbook");
 $result = $db->Execute("SELECT * FROM state");
 if ($result === false) die("failed");  
 while (!$result->EOF) {
for ($i=0, $max=$result->FieldCount(); $i < $max; $i++)
  print $result->fields[$i].' ';
$result->MoveNext();
print "
\n";
 } 

?>


This is a step in the right direction, but it still doesn't save me much tedium as a coder and its only a small step in the direction of object-oriented coding. ADODB implements a version of ActiveRecord that can make the entire process of getting and setting data object-oriented. Unfortunately, there are even less examples of code that use this online. Here's the most helpful one I could find: 

http://phplens.com/lens/adodb/docs-active-record.htm

I tried to implement this and integrate it with my database. However, I was unable to get anything to work. When I ran the script no INSERTS would actually be made into database table, and thus far my debugging efforts have been futile. Below is the script that mirrors the example from the above link; it is designed to load a row from the "states" database into a newly created "state" object, and then as a test I want to print out the ID from the state that was just loaded:


include('/Library/WebServer/Documents/adodb/adodb.inc.php');
require_once('/Library/WebServer/Documents/adodb/adodb-active-record.php');


// create an object instance
// configure library for a MySQL connection
$db = NewADOConnection("mysql");

// open connection to database
$db->Connect("localhost", "rjroth", "aZsXdCf", "salonbook") or die("Unable to connect!");
echo 'hello world';
class state extends ADOdb_Active_Record
{
var $_table = 'state';
}

$state = new state();
$state->load("id=5");
echo $state->id;

?> 

Unfortunately, I can't get this to work so far. Hopefully, with help from Jeff, I will turn the corner this week and go on to build all the objects I will need to represent my database.