DB.PHP
Code: Select all
<?php
// DB.PHP
require_once('forms.php');
ini_set("display_errors",1);
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'hosp_db');
// MAIN FUNCTION
class db{
var $filter_user_inputs = true;
var $paging_set; // trigger to call paging
// CONNECT THE DB
function dbconnect(){
$the_conn = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$db = mysql_select_db(DB_DATABASE) or die("Could not select database");
return array('db' => $db, 'conn' => $the_conn);
}
// CLOSE CONNECTION
function db_close($link){
if(is_resource($link)){
mysql_close($link);
}
}
// PROCESS QUERY CHECK IF USER SET VALUES FOR PAGING
function query($QUERY){
// IF PAGING IS SET CALLS PAGING FUNCTION
if($this->paging_set){
$exec_query = $this->paging_a($QUERY);
// ELSE EXECUTE PASSED QUERY
}else{
$exec_query = $this->exec_query($QUERY);
}
return $exec_query;
}
// EXECUTE QUERIES PASSED
function exec_query($QUERY){
$QUERY = mysql_query(trim($QUERY)) or die(mysql_error());
return $QUERY;
}
// FILTER THE DATA
function filter_query($VALUE){
if($this->filter_user_inputs){
if (get_magic_quotes_gpc()){
$VALUE = stripslashes($VALUE);
}
$VALUE = htmlentities($VALUE,ENT_COMPAT);
$VALUE = addslashes($VALUE);
if (!is_numeric($VALUE)){
$VALUE = "'".mysql_real_escape_string($VALUE)."'";
}
}else{
if (!is_numeric($VALUE)){
$VALUE = "'".$VALUE."'";
}
}
return $VALUE;
}
// check field type
// DAPAT PANG PAG ISIPAN KUNG ANONG MAGIGING FORM NG MGA DATA TYPE
// from add class * fix me
function field_type($type,$the_value){
$Gen_Forms = new Forms();
if(isset($this->table_values)){
$table_values = $this->table_values[0][$the_value];
}else{
$table_values = '';
}
if(preg_match("/(.+)\((.+)\)/",$type)){
$option_values = $this->extract_fields($type);
$type = $option_values['data_type'];
}
switch ($type) {
case 'text':
$OUTPUT = $Gen_Forms->textarea($the_value,$table_values, 'cols="35" rows="10"', false);
break;
case 'mediumtext':
$OUTPUT = $Gen_Forms->textarea($the_value,$table_values, 'cols="35" rows="10"', false);
break;
case 'longtext':
$OUTPUT = $Gen_Forms->textarea($the_value,$table_values, 'cols="35" rows="10"', false);
break;
case 'enum':
$x_option_values = array();
foreach($option_values['values'] as $xela){
if($xela == $table_values){
$check_this = 1;
}else{
$check_this = 0;
}
if($check_this){
$x_option_values []= array('values' => $xela, 'checked' => 1);
}else{
$x_option_values []= array('values' => $xela, 'checked' => 0);
}
}
$options = $Gen_Forms->select_values($x_option_values);
$OUTPUT = $Gen_Forms->select_options($the_value,$options, '', false);
break;
case 'set':
// EXPLODE TABLE VALUES WHICH IS DELIMITED BY ' , '
$array_table_values = explode(',',$table_values);
$x_option_values = array();
//
foreach($option_values['values'] as $xela){
foreach($array_table_values as $l_array_table_values){
if($xela == $l_array_table_values){
$check_this = 1;
break;
}else{
$check_this = 0;
}
}
if($check_this){
$x_option_values []= array('values' => $xela, 'checked' => 1);
}else{
$x_option_values []= array('values' => $xela, 'checked' => 0);
}
}
$options = $Gen_Forms->checkbox_values($the_value,$x_option_values);
$OUTPUT = $Gen_Forms->checkbox($the_value,$options,'vertical', '', false);
break;
default:
$OUTPUT = $Gen_Forms->textbox($the_value,$table_values,'',false);
}
return $OUTPUT;
}
// EXTRACT DATABASE FIELD TYPE AND VALUES
function extract_fields($field_type){
preg_match("/(.+)\((.+)\)/",$field_type,$matches);
$data_type = $matches[1];
$values = $matches[2];
preg_match_all("/\'(.+?)\'/",$values,$values);
$values = array('data_type'=> $data_type, 'values' => $values[1]);
return $values;
}
// SIMPLY RETURN DATA WITH TD
function write_td($td_value){
$td_value = sprintf('<td valign="top">%s:</td>', $td_value);
return $td_value;
}
// FUNCTION FOR MANUAL CUSTOMIZATION
// ----------->>>>>>> CONTINUE THIS
function manual_forms($row_type, $row_field){
$hidden = 0;
$found_match = 0;
$manual_forms = '';
// LOOP USER CUSTOMIZATION THEN FILTERING BEGINS
foreach($this->user_forms as $loop_user_forms){
// IF USER CUSTOMIZATION IS EQUAL TO THE PASSED FIELD WICH CAME FROM DATABASE INFO
if($loop_user_forms['field_name'] == $row_field){
// FLAG IF A MATCH WAS FOUND
$found_match = 1;
switch($loop_user_forms['form_type']){
case 'textbox':
$manual_forms .= $this->write_td($loop_user_forms['label']);
$manual_forms .= $this->write_td(Forms::textbox($row_field,'','', false));
break;
case 'password':
$manual_forms .= $this->write_td($loop_user_forms['label']);
$manual_forms .= $this->write_td(Forms::password($row_field,'','', false));
break;
case 'textarea':
$manual_forms .= $this->write_td($loop_user_forms['label']);
$manual_forms .= $this->write_td(Forms::textarea($row_field,'', 'cols="35" rows="10"', false));
break;
case 'hidden':
$manual_forms = Forms::hidden($row_field,'', 'size="25"', false);
$hidden = 1;
break;
case 'select':
$manual_forms .= $this->write_td($loop_user_forms['label']);
$manual_forms .= $this->write_td(Forms::select_options($row_field,$loop_user_forms['values'],'', false));
break;
case 'checkbox':
$manual_forms .= $this->write_td($loop_user_forms['label']);
$manual_forms .= $this->write_td(Forms::checkbox($row_field,$loop_user_forms['values'],'vertical', '', false));
break;
case 'radio':
$manual_forms .= $this->write_td($loop_user_forms['label']);
$manual_forms .= $this->write_td(Forms::radio_buttons($row_field,$loop_user_forms['values'],'horizontal','', false));
break;
default:
$manual_forms .= $this->write_td($loop_user_forms['label']);
$manual_forms .= $this->write_td(Forms::textbox($row_field,'','', false));
}
}
}
// IF THE USER DID NOT SET CUSTOMIZATION FOR A SPECIFIC FIELD
if(!$found_match){
$manual_forms .= $this->write_td(ucfirst($row_field));
$manual_forms .= $this->write_td($this->field_type($row_type,$row_field));
}
return $manual_forms;
}
}
// DISPLAY DATABASE VALUES / LISTING
class display extends db{
var $ROW_START = 0;
var $ROW_END = 10; // default, can be set by user
var $query_limit;
var $total_rows;
var $no_pages;
var $row_perpage;
var $CURRENT_PAGE = 0;
var $next_page;
var $previous_page;
var $next_page_url; // URL FOR NEXT PAGE
var $previous_page_url; // URL FOR PREVIIOUS PAGE
// SET THE PAGING
function paging($row_perpage){
// SETS PAGING FLAG ON
$this->paging_set = 1;
// CHECK USER SETTINGS OF ROW ELSE USE DEFAULT VALUES
// default row per page = 10
if(isset($row_perpage) && $row_perpage != ''){
$this->ROW_END=$row_perpage;
$this->row_perpage = $row_perpage;
}
}
// RETURN EXECUTED QUERY
function paging_a($QUERY){
// RUN CALCULATION OF PAGING
$this->paging_b($QUERY);
$QUERY = sprintf("$QUERY order by id LIMIT %s, %s", $this->ROW_START,$this->ROW_END);
$exec_query = $this->exec_query($QUERY);
return $exec_query;
}
// PAGES AND ROWS ARE CALCULATED HERE
function paging_b($QUERY){
// EXECUTE THE QUERY WITHOUT LIMIT TO GET NO OF ROWS
$no_limit_query = $this->exec_query($QUERY);
$this->total_rows = mysql_num_rows($no_limit_query);
mysql_free_result($no_limit_query);
// GET TOTAL NUMBER OF PAGES
$this->no_pages = ceil($this->total_rows / $this->row_perpage);
// CHECK IF THERE ARE PATTER /PAGES/#NUM
if(preg_match("/\/pages\/([0-9]+)/",$_SERVER['REQUEST_URI'],$uri_pages)){
// GET THE PAGES FROM URI
$this->CURRENT_PAGE=$uri_pages[1];
if($this->CURRENT_PAGE >= $this->no_pages){
$this->CURRENT_PAGE = 0;
}
// CAPTURE THE FORM PAGING
if(isset($_GET['pages']) && is_numeric($_GET['pages'])){
if($_GET['pages'] >= $this->no_pages){
$this->CURRENT_PAGE = $this->no_pages - 1;
}else{
if($_GET['pages'] <= 0){
$this->CURRENT_PAGE = 0;
}else{
$this->CURRENT_PAGE = $_GET['pages'] - 1;
}
}
}
$this->next_page = $this->CURRENT_PAGE + 1;
$this->previous_page = ceil($this->CURRENT_PAGE - 1);
if($this->previous_page < 0){
$this->previous_page = 0;
}
if(!$this->CURRENT_PAGE <= 0){
$this->ROW_START = $this->row_perpage * $this->CURRENT_PAGE;
// I THINK THIS IS A BUG
// $this->ROW_END = ($this->row_perpage * $this->CURRENT_PAGE) + $this->row_perpage;
$this->ROW_END = $this->row_perpage;
}
// REPLACE THE VALUE OF pages/#num FROM THE URI
$this->next_page_url = preg_replace("/\/pages\/[0-9]+/","/pages/$this->next_page",$_SERVER['PHP_SELF']);
$this->previous_page_url = preg_replace("/\/pages\/[0-9]+/","/pages/$this->previous_page",$_SERVER['PHP_SELF']);
}else{ // ELSE OF PREGMATCH CHECK IF THERE IS A PATTERN PAGES/#NUM IN URI
// CAPTURE THE FORM PAGING
if(isset($_GET['pages']) && is_numeric($_GET['pages'])){
if($_GET['pages'] >= $this->no_pages){
$this->CURRENT_PAGE = $this->no_pages;
}else{
$this->CURRENT_PAGE = $_GET['pages'] - 1;
}
}
$this->next_page = $this->CURRENT_PAGE + 1;
$this->previous_page = ceil($this->CURRENT_PAGE - 1);
if($this->previous_page < 0){
$this->previous_page = 0;
}
// WHEN NO PAGES/#NUM URI ADD /PAGES/#NUM
$this->next_page_url = $_SERVER['PHP_SELF']."/pages/$this->next_page";
$this->previous_page_url = $_SERVER['PHP_SELF']."/pages/$this->previous_page";
}// END OF PREGMATCH URI PATTERN MATCHING
}// function paging_b
// DISPLAY THE PAGING
function paging_show(){
echo("<form action=\"$_SERVER[PHP_SELF]\" method=\"GET\">");
if($this->CURRENT_PAGE > 0){
echo("<a href=\"$this->previous_page_url\">previous page</a> ");
}
$disp_current_page = $this->CURRENT_PAGE + 1;
echo("page $disp_current_page of $this->no_pages");
if($this->no_pages > $disp_current_page){
echo(" <a href=\"$this->next_page_url\">next page</a>");
}
echo(" page <input type=\"text\" value=\"\" name=\"pages\" size=\"5\"> <input type=\"submit\" value=\"go\"></form>");
}
// WHERE COMPARISON OF FIELD AND VALUES
function where($table, $db_fieldname, $v_value){
$query = sprintf("select * from %s where %s = %s",
$table,
$db_fieldname,
$this->filter_query($v_value));
$DB_CONNECTION = $this->dbconnect();
$exec_query = $this->query($query);
$query_rs = mysql_fetch_assoc($exec_query);
$RETURN_ARRAY = array();
$ctr = 0;
do{
if(mysql_num_rows($exec_query) > 0)
{
foreach($query_rs as $key => $val)
{
$RETURN_ARRAY[$ctr][$key]= $val;
}
}else{
$RETURN_ARRAY[$ctr][$key]= $val;
}
$ctr++;
}while($query_rs = mysql_fetch_assoc($exec_query));
mysql_free_result($exec_query);
$this->db_close($DB_CONNECTION['conn']);
return $RETURN_ARRAY;
}
// IF CALLED RETURN ALL ROWS IN A TABLE
function get($table){
$DB_CONNECTION = $this->dbconnect();
$exec_query = $this->query("select * from $table");
$query_rs = mysql_fetch_assoc($exec_query);
$RETURN_ARRAY = array();
$ctr = 0;
do{
foreach($query_rs as $key => $val)
{
$RETURN_ARRAY[$ctr][$key]= $val;
}
$ctr++;
}while($query_rs = mysql_fetch_assoc($exec_query));
mysql_free_result($exec_query);
$this->db_close($DB_CONNECTION['conn']);
return $RETURN_ARRAY;
}
// IF CALLED EXECUTE THE PASSED QUERY
function run_query($query){
$DB_CONNECTION = $this->dbconnect();
$exec_query = $this->query($query);
$query_rs = mysql_fetch_assoc($exec_query);
$RETURN_ARRAY = array();
$ctr = 0;
do{
foreach($query_rs as $key => $val)
{
$RETURN_ARRAY[$ctr][$key]= $val;
}
$ctr++;
}while($query_rs = mysql_fetch_assoc($exec_query));
mysql_free_result($exec_query);
$this->db_close($DB_CONNECTION['conn']);
return $RETURN_ARRAY;
}
}// CLASS display
// CLASS FOR GENERATING FORM (MANUAL / AUTO)
class add extends db{
var $user_forms;
// SAVE THE FORM
function save($TABLE_name){
$THE_field_lists = '';
$THE_field_values = '';
foreach($_POST as $field_name => $field_value)
{
if($field_name != 'phunc_add')
{
$THE_field_lists .= $field_name.',';
if(is_array($field_value)){
$x_field_value = '';
foreach($field_value as $l_field_value){
$x_field_value .= $l_field_value.',';
}
$field_value = substr($x_field_value,0,-1);
$THE_field_values .= $this->filter_query($field_value).',';
}else{
$THE_field_values .= $this->filter_query($field_value).',';
}
}
}
$THE_field_lists = substr($THE_field_lists,0,-1);
$THE_field_values = substr($THE_field_values,0,-1);
$QUERY_form = sprintf('insert into %s(%s)values(%s)',$TABLE_name,$THE_field_lists,$THE_field_values);
// echo($QUERY_form);
$execute_query = $this->query($QUERY_form);
}
//DISPLAY THE FORM
function show_forms($TABLE_name, $user_forms='auto'){
$this->user_forms = $user_forms;
$OUTPUT = '';
$DB_CONNECTION = $this->dbconnect();
// IF FORM phunc_add EXISTS SAVE THE FORM
if(isset($_POST['phunc_add'])){
$this->save($TABLE_name);
}
// GET TABLE INFO
$result = $this->query("SHOW COLUMNS FROM $TABLE_name");
if (mysql_num_rows($result) > 0){
$OUTPUT.= '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
while ($row = mysql_fetch_assoc($result)){
if($row['Extra'] != 'auto_increment'){
$OUTPUT.='<tr>';
// CHECK IF THE USER SETS AUTO FORM GENERATION
if($user_forms == 'auto'){
$OUTPUT .= $this->write_td(ucfirst($row['Field']));
$OUTPUT .= sprintf('<td valign="top">%s</td>', $this->field_type($row['Type'],$row['Field']));
}else{// CALL FUNCTION FOR MANUAL CUSTOMIZATION
$OUTPUT .= $this->manual_forms($row['Type'],$row['Field']);
}
$OUTPUT .='</tr>';
}
}
$OUTPUT .='<td> </td><td><input type="submit" name="phunc_add" value="Submit" /></td></form>';
echo($OUTPUT);
}
mysql_free_result($result);
$this->db_close($DB_CONNECTION['conn']);
}
}// end add class
class edit extends db{
//DISPLAY THE FORM
var $table_values;
function update($TABLE_name, $unique_id, $unique_value){
$THE_field_lists = '';
$THE_field_values = '';
$PAIR_edit = '';
// GENERATE UPDATE QUERY BASE ON $_POST
foreach($_POST as $field_name => $field_value)
{
// ESCAPE THE TRIGGER
if($field_name != 'phunc_edit')
{
// CHECK IF ITS AN ARRAY, IF YES COMBINE FORMS DELIMITED BY ','
if(is_array($field_value)){
$x_field_value = '';
foreach($field_value as $l_field_value){
$x_field_value .= $l_field_value.',';
}
$field_value = substr($x_field_value,0,-1);
$PAIR_edit .= sprintf("%s = %s, ",$field_name, $this->filter_query(trim($field_value)));
}else{
$PAIR_edit .= sprintf("%s = %s, ",$field_name, $this->filter_query(trim($field_value)));
}
}
}
// REMOVE , IN THE END OF THE VARIABLE
$PAIR_edit = substr(trim($PAIR_edit),0,-1);
$QUERY_form = sprintf('update %s set %s where %s=%s',$TABLE_name,$PAIR_edit,$unique_id,$this->filter_query($unique_value));
$execute_query = $this->query($QUERY_form);
}
// GENERATE FORMS BASE ON THE DATABSAE
function show_forms($TABLE_name, $user_forms='auto', $unique_id, $unique_value){
$this->user_forms = $user_forms;
$OUTPUT = '';
$DB_CONNECTION = $this->dbconnect();
// IF FORM HAS BEEN SUBMITTED
if(isset($_POST['phunc_edit'])){
$this->update($TABLE_name, $unique_id, $unique_value);
}
// GET TABLE INFO
$result = $this->query("SHOW COLUMNS FROM $TABLE_name");
$this->table_values = display::where($TABLE_name, $unique_id, $unique_value);
if (mysql_num_rows($result) > 0){
$OUTPUT.= '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
while ($row = mysql_fetch_assoc($result)){
if($row['Extra'] != 'auto_increment'){
$OUTPUT.='<tr>';
// CHECK IF THE USER SETS AUTO FORM GENERATION
if($user_forms == 'auto'){
$OUTPUT .= sprintf('<td valign="top">%s:</td>', ucfirst($row['Field']));
$OUTPUT .= sprintf('<td valign="top">%s</td>', $this->field_type($row['Type'],$row['Field']));
}else{// CALL FUNCTION FOR MANUAL CUSTOMIZATION
$OUTPUT .= $this->manual_forms($row['Type'],$row['Field']);
}
$OUTPUT .='</tr>';
}
}
$OUTPUT .='<td> </td><td><input type="submit" name="phunc_edit" value="Submit" /></td></form>';
echo($OUTPUT);
}
mysql_free_result($result);
$this->db_close($DB_CONNECTION['conn']);
}
}// end edit class
?>FORMS.PHP
Code: Select all
<?php
// FORMS.PHP
class Forms{
var $OUTPUT;
function return_display($display=true, $OUTPUT){
if($display){
echo($OUTPUT);
}else{
return $OUTPUT;
}
}
function textarea($name, $value=null, $attributes=null, $display=true){
$this->OUTPUT = sprintf('<textarea name="%s" %s>%s</textarea>',$name, $attributes, $value);
return Forms::return_display($display, $this->OUTPUT);
}
function textbox($name, $value=null, $attributes=null, $display=true){
$this->OUTPUT = sprintf('<input type="textbox" name="%s" value="%s" %s/>',$name, $value, $attributes);
return Forms::return_display($display, $this->OUTPUT);
}
function password($name, $value=null, $attributes=null, $display=true){
$this->OUTPUT = sprintf('<input type="password" name="%s" value="%s" %s/>',$name, $value, $attributes);
return Forms::return_display($display, $this->OUTPUT);
}
function hidden($name, $value=null, $attributes=null,$display=true){
$this->OUTPUT = sprintf('<input type="hidden" name="%s" value="%s" %s/>',$name, $value, $attributes);
return Forms::return_display($display, $this->OUTPUT);
}
function select_options($name, $values, $attributes=null, $display=true){
$this->OUTPUT = sprintf('<select name="%s" id="%s" %s>', $name, $name, $attributes);
foreach($values as $sel_values){
if($sel_values['selected']){
$this->OUTPUT .= sprintf('<option value="%s" selected="selected">%s</option>',
$sel_values['values'],
htmlentities($sel_values['label']));
}else{
$this->OUTPUT .= sprintf('<option value="%s">%s</option>',
$sel_values['values'],
htmlentities($sel_values['label']));
}
}
$this->OUTPUT .= '</select>';
return Forms::return_display($display, $this->OUTPUT);
}
function radio_buttons($name, $values, $position='horizontal', $attributes, $display=true){
$this->OUTPUT = '<table class="'.$name.'" border="0" cellpadding="1" cellspacing="0">';
$rid = 1;
foreach($values as $rad_values){
$this->OUTPUT .= ($position == 'horizontal') ? '<tr>' : '';
$this->OUTPUT .= sprintf('<td><input name="%s" type="radio" value="%s" id="%s" %s/></td>',
$name,
$rad_values['values'],
$name.$rid,
$attributes);
$this->OUTPUT .= sprintf('<td><label for="%s">%s</label>',$name.$rid,htmlentities($rad_values['label']));
$rid++;
$this->OUTPUT .= ($position == 'horizontal') ? '</tr>' : '';
}
$this->OUTPUT .= '</table>';
return Forms::return_display($display, $this->OUTPUT);
}
function checkbox($name, $values, $position='horizontal', $attributes, $display=true){
$this->OUTPUT = '<table class="'.$name.'" border="0" cellpadding="1" cellspacing="0">';
$cid = 1;
foreach($values as $c_values){
$this->OUTPUT .= ($position == 'horizontal') ? '<tr>' : '';
if($c_values['checked']){
$this->OUTPUT .= sprintf('<td><input type="checkbox" name="%s[]" value="%s" id="%s" %s checked="checked" /></td>',
$c_values['name'],
$c_values['values'],
$c_values['name'].$cid,
$attributes);
}else{
$this->OUTPUT .= sprintf('<td><input type="checkbox" name="%s[]" value="%s" id="%s" %s /></td>',
$c_values['name'],
$c_values['values'],
$c_values['name'].$cid,
$attributes);
}
$this->OUTPUT .= sprintf('<td><label for="%s">%s</label>',$c_values['name'].$cid,htmlentities($c_values['label']));
$cid++;
$this->OUTPUT .= ($position == 'horizontal') ? '</tr>' : '';
}
$this->OUTPUT .= ('</table>');
return Forms::return_display($display, $this->OUTPUT);
}
// USE FOR EXTRACTING 1 DIMENSION ARRAY
function select_values($option_values){
$options = array();
foreach($option_values as $default_values){
$options []= array('label' => $default_values['values'], 'values' => ' '.$default_values['values'], 'selected' => $default_values['checked']);
}
return $options;
}
// USE FOR EXTRACTING 1 DIMENSION ARRAY
function checkbox_values($the_value,$option_values){
$check_btn = array();
foreach($option_values as $default_values){
$check_btn []= array('name'=> $the_value, 'label' => $default_values['values'], 'values' => $default_values['values'], 'checked' => $default_values['checked']);
}
return $check_btn;
}
}
?>SAMPLE USAGE
Code: Select all
SAMPLE USAGE
UPDATING RECORDS
require_once('db.php');
$test = new edit();
// table name / auto / unique id/ id value
$test->show_forms('members','auto','id','145');
ADDING RECORDS
require_once('db.php');
$test = new add();
$test->show_forms('members');
LISTING OF RECORDS
require_once('db.php');
$test = new display();
// MORE EXAMPLE
// TABLE, FIELDNAME, VALUE
// $result = $test->where('members','id', '7');
// GET ALL DATA
// $result = $test->get('members');
// RUN A QUERY
// $result = $test->run_query('select * from members where id > 7');
$paging = $test->paging('10');
$result = $test->get('members');
//RETURNED DATA
foreach($result as $xster){
echo('username:'.$xster['username']."<br>");
echo('password:'.$xster['password']."<br>");
echo("<hr>");
}