This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
kurs:hr-class [2009/12/02 16:00] mh |
kurs:hr-class [2014/09/10 21:22] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <code> | ||
+ | <?php | ||
+ | // get_emp_by_name_obj_session_db.php | ||
+ | session_start(); | ||
+ | include_once "hr/DB/Session.php"; | ||
+ | |||
+ | $session = new Session(); | ||
+ | $session->session_id = session_id(); | ||
+ | |||
+ | if ($_REQUEST['cmd'] != 'getemp') { | ||
+ | |||
+ | $html=<<<BLAB | ||
+ | <form name="get_emp_by_name" | ||
+ | action=$_SERVER[PHP_SELF] | ||
+ | method="POST"> | ||
+ | <input name="cmd" type="hidden" value="getemp"> | ||
+ | First Name:<input name="first_name" type="text" value="%s" size="30"><br> | ||
+ | Last Name: <input name="last_name" type="text" value="%s" size="30"><br> | ||
+ | <input type="submit" value="Get Employee"> | ||
+ | </form> | ||
+ | BLAB; | ||
+ | | ||
+ | $r = $session->Read(); | ||
+ | print_r($r); | ||
+ | $fn = $r['first_name']; | ||
+ | $ln = $r['last_name']; | ||
+ | |||
+ | printf($html, $fn, $ln); | ||
+ | |||
+ | } else { | ||
+ | # include_once "functions.php"; | ||
+ | include_once "hr/DB/Employee.php"; | ||
+ | |||
+ | $employee = new Employee(); | ||
+ | |||
+ | $r = $_REQUEST; | ||
+ | |||
+ | $session->Store($r); | ||
+ | |||
+ | $employee->first_name = $r['first_name']; | ||
+ | $employee->last_name = $r['last_name']; | ||
+ | |||
+ | print_r($r); | ||
+ | |||
+ | $employee->read(); | ||
+ | print "<br>"; | ||
+ | print $employee->dump(); | ||
+ | } | ||
+ | |||
+ | $session->commit(); | ||
+ | |||
+ | ?> | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | // hr/DB.php | ||
+ | <?php | ||
+ | |||
+ | ini_set('display_errors', 1); | ||
+ | |||
+ | class DB { | ||
+ | public $db; | ||
+ | |||
+ | function dbconnect() { | ||
+ | $this->db = oci_connect('hr', 'hr', 'orcl'); | ||
+ | } | ||
+ | |||
+ | function commit() { | ||
+ | oci_commit($this->db); | ||
+ | } | ||
+ | |||
+ | function rollback() { | ||
+ | oci_rollback($this->db); | ||
+ | } | ||
+ | |||
+ | function print_table($table) { | ||
+ | print "<table border=1>\n"; | ||
+ | foreach ($table as $row) { | ||
+ | print "<tr>"; | ||
+ | foreach ($row as $cell) { | ||
+ | if (!isset($cell)) {$cell = 'n/a'; } | ||
+ | print "<td> $cell </td> "; | ||
+ | } | ||
+ | print "</tr>\n"; | ||
+ | } | ||
+ | |||
+ | print "</table>\n"; | ||
+ | } | ||
+ | } | ||
+ | ?> | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | <?php | ||
+ | // hr/DB/employee.php | ||
+ | include_once('hr/DB.php'); | ||
+ | |||
+ | class Employee extends DB { | ||
+ | public $first_name; | ||
+ | public $last_name; | ||
+ | public $salary; | ||
+ | | ||
+ | function read() { | ||
+ | $this->dbconnect(); | ||
+ | $sql3 = | ||
+ | 'select first_name, last_name, salary from employees where | ||
+ | last_name = :last_name and | ||
+ | first_name = :first_name'; | ||
+ | |||
+ | $q = oci_parse($this->db, $sql3); | ||
+ | oci_bind_by_name($q, ":last_name", $_REQUEST['last_name'],-1, | ||
+ | SQLT_CHR ); | ||
+ | |||
+ | oci_bind_by_name($q, ":first_name", $_REQUEST['first_name'],-1, | ||
+ | SQLT_CHR ); | ||
+ | | ||
+ | $r = oci_execute($q, OCI_DEFAULT); | ||
+ | |||
+ | $table = oci_fetch_array($q, OCI_ASSOC+OCI_RETURN_NULLS); | ||
+ | |||
+ | $this->salary = $table['SALARY']; | ||
+ | $this->first_name = $table['FIRST_NAME']; | ||
+ | $this->last_name = $table['LAST_NAME']; | ||
+ | |||
+ | } | ||
+ | |||
+ | function dump() { | ||
+ | $d = sprintf ("First Name: %s <br>\n | ||
+ | Last Name: %s <br>\n | ||
+ | Salary : %s <br>\n", | ||
+ | $this->first_name, | ||
+ | $this->last_name, | ||
+ | $this->salary); | ||
+ | return $d; | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | <?php | ||
+ | |||
+ | // get_emp_by_name_obj.php | ||
+ | |||
+ | print "<h2>request</h2>\n"; | ||
+ | print_r($_REQUEST); | ||
+ | |||
+ | if ($_REQUEST['cmd'] != 'getemp') { | ||
+ | $html=<<<BLAB | ||
+ | <form name="get_emp_by_name" | ||
+ | action=$_SERVER[PHP_SELF] | ||
+ | method="get"> | ||
+ | <input name="cmd" type="hidden" value="getemp"> | ||
+ | First Name:<input name="first_name" type="text" size="30"><br> | ||
+ | Last Name:<input name="last_name" type="text" size="30"><br> | ||
+ | <input type="submit" value="Get Employee"> | ||
+ | </form> | ||
+ | BLAB; | ||
+ | |||
+ | print $html; | ||
+ | |||
+ | } else { | ||
+ | include_once "functions.php"; | ||
+ | include_once "hr/DB/Employee.php"; | ||
+ | |||
+ | $employee = new Employee(); | ||
+ | |||
+ | $employee->first_name = $first_name; | ||
+ | $employee->last_name = $last_name; | ||
+ | $employee->read(); | ||
+ | print "<br>"; | ||
+ | print $employee->dump(); | ||
+ | } | ||
+ | |||
+ | ?> | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | <?php | ||
+ | // hr/DB/Session.php | ||
+ | include_once('hr/DB.php'); | ||
+ | |||
+ | class Session extends DB { | ||
+ | public $session_id; | ||
+ | public $data; | ||
+ | | ||
+ | function __construct() { | ||
+ | $this->dbconnect(); | ||
+ | } | ||
+ | | ||
+ | function Store($data) { | ||
+ | $data = serialize($data); | ||
+ | $sql = | ||
+ | 'merge into session_data | ||
+ | using (select 1 from dual) | ||
+ | on (session_id = :session_id) | ||
+ | when matched then update | ||
+ | set data = :data, change_date=sysdate | ||
+ | when not matched then | ||
+ | insert ( | ||
+ | id, | ||
+ | session_id, | ||
+ | data, | ||
+ | change_date | ||
+ | ) values ( | ||
+ | hr_seq.nextval, | ||
+ | :session_id, | ||
+ | :data, | ||
+ | sysdate)'; | ||
+ | |||
+ | $q = oci_parse($this->db, $sql); | ||
+ | oci_bind_by_name($q, ":session_id", $this->session_id,-1, | ||
+ | SQLT_CHR ); | ||
+ | oci_bind_by_name($q, ":data", $data,-1, | ||
+ | SQLT_CHR ); | ||
+ | | ||
+ | $r = oci_execute($q, OCI_DEFAULT); | ||
+ | | ||
+ | return 0; | ||
+ | } | ||
+ | | ||
+ | function Read() { | ||
+ | $sql = | ||
+ | 'select data from session_data where | ||
+ | session_id = :session_id'; | ||
+ | |||
+ | $q = oci_parse($this->db, $sql); | ||
+ | oci_bind_by_name($q, ":session_id", $this->session_id,-1, | ||
+ | SQLT_CHR ); | ||
+ | | ||
+ | $r = oci_execute($q, OCI_DEFAULT); | ||
+ | |||
+ | $table = oci_fetch_array($q, OCI_ASSOC+OCI_RETURN_NULLS); | ||
+ | |||
+ | $d = $table['DATA']; | ||
+ | $this->data = unserialize($d); | ||
+ | return $this->data; | ||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </code> |