is script code and will // be executed, sending only the resulting HTML to the web browser // PHP Website: // Start out with some fairly static variables $database = "timesheet"; // database name $user = ""; // database user id $passwd = ""; // database password $table = "EMP"; // employee data table name $order_by = "lname"; // field to order results by $blanks = 3; // number of blank employee fields to display // The following class definition utilizes PHP's object oriented // features in an attempt to make a simple, database independent // object. This will allow switching between databases without // any modification required in the actual PHP script. The idea is // that a library of such database objects can be readily available // and one could simply 'include' the class for the particular // database being used. Just one way to "database independence". // // The class definition is 'simple' in that it is designed to // utilize features common to all databases in a fashion that is // heavily utilized by web pages. The class will allow you to // make a connection, execute SQL statments and iterate through // what could be considered a forward-only cursor of results. // // We've attempted to use a syntax that would be semi-familiar // to both a DBI and ADO (Microsoft's DB access tech.) user. // // If more advanced or database-specific features are required, // this class object is still very usable because it makes // available it's connection identifier and result identifier // as properties, allowing them to be used directly in any // database function that requires it. class db { var $EOF = ""; // flag to signify end of result set var $result_id; // result set identifier (int) var $conn_id; // connection id identifier (int) //********************************************************* // Purpose: Establishes a connection to the database // Arguments: $database (string) - name of database to query //           $user (string)    - database user name //              $passwd (string) - database user password // Returns: The connection identifier (int) //********************************************************* function Connect($database,$user,$passwd) { return $this->conn_id = odbc_connect($database,$user,$passwd); } //********************************************************* // Purpose: Execute a SQL statment on the connection // Arguments: $query_string (string) - SQL statement to execute // Returns: 0 - query failed // 1 - query succeeded //********************************************************* function Execute($query_string) { return $this->result_id = odbc_exec($this->conn_id, $query_string); } //********************************************************* // Purpose: Proceeds to the next row in the result set. // If the end of the result set is reached, sets the // $EOF variable to true. // Returns: false if reached the end of the recordset. //********************************************************* function MoveNext() { $test = odbc_fetch_row($this->result_id); if (!$test) { $this->EOF = 1; } return $test; } //********************************************************* // Purpose: Retrieves a value for a specified field/column // Arguments: $fieldname (string) - name of field to retrieve // Returns: the value of the field (string) //********************************************************* function Get($fieldname) { return odbc_result($this->result_id, $fieldname); } //********************************************************* // Purpose: Frees result set // Returns: 0 - failure // 1 - success //********************************************************* function FreeResult() { return odbc_free_result($this->result_id); } //********************************************************* // Purpose: Closes database connection // Returns: 0 - failure // 1 - success //********************************************************* function Close() { return odbc_close($this->conn_id); } }; ?> Timesheet example application - Employee Modification

   Timesheet Application Example

Connect($database,$user,$passwd); // Establish a connection // When the user hits the 'Update' button for the form to be displayed // below, $action will be true and the following 'if' statement block will // be executed. It will update any values the user changed. // // If you are looking at this for the first time, you might want to skip // this 'if' block and see the form below to actually see what this does // with the data from that form. if ($action) { // The user hit the update button. // Now go through every field and perform neccessary actions $i = 0; // counter for while statement $count = Count($lname); // number of form fields while ($i < $count) { if ($emp_id[$i] && !$lname[$i] && !$fname[$i]) { // The user erased the name, so delete the record $sql = "DELETE FROM $table WHERE emp_id = $emp_id[$i]"; $mydb->Execute($sql); } elseif ($emp_id[$i]) { // Update the record $sql = "UPDATE $table SET lname='$lname[$i]', "; $sql .= "fname='$fname[$i]' WHERE emp_id = $emp_id[$i]"; $mydb->Execute($sql); } elseif ($lname[$i] || $fname[$i]) { // User added a new name, so insert $sql = "INSERT INTO $table (fname, lname) VALUES "; $sql .= "('$fname[$i]', '$lname[$i]')"; $mydb->Execute($sql); } // If none of the above match, ignore it $i++; } ?> Employees Succesfully Updated!

Current Exployees:
Execute($sql); while (!$mydb->EOF) { $emp_id = $mydb->Get("emp_id"); $fname = $mydb->Get("fname"); $lname = $mydb->Get("lname"); // Below we use empty array elements for the form field name. // (i.e. emp_id[] is an array element in PHP) // By doing so, we can use the same name for the fields and // iterate through the results by determining how many array // elements there are. Easy! ?> MoveNext(); } $mydb->FreeResult(); // Free up result set, not really necessary $mydb->Close(); // Close the database connection // Now we list some blank fields, in case the user wants to add // any addtional employees. The number of blank fields is determined // by the variable $blanks defined at the top of this script. $i = 0; while ($i < $blanks) { ?>
Last Name First Name

Options
Main Page

Modify Jobs List

View Source