please criticize my class

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
silverbux
Forum Newbie
Posts: 9
Joined: Mon Aug 14, 2006 8:50 pm

please criticize my class

Post by silverbux »

hello ive wrote a small database framework please help me improve it. please tell me the wrong methods or procedures i made. because i think im using too much arrays and i think i its more on procedural programming than oop. thank you in advance. :)






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>&nbsp;");
			}	
			$disp_current_page = $this->CURRENT_PAGE + 1;
			echo("page $disp_current_page of $this->no_pages");
			if($this->no_pages > $disp_current_page){
				echo("&nbsp;<a href=\"$this->next_page_url\">next page</a>");				
			}
			echo("&nbsp;page&nbsp;<input type=\"text\" value=\"\" name=\"pages\" size=\"5\">&nbsp;<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>&nbsp;</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>&nbsp;</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>");
			}
silverbux
Forum Newbie
Posts: 9
Joined: Mon Aug 14, 2006 8:50 pm

OPS I FORGOT SOMETHING

Post by silverbux »

sorry i forgot something, this is how you generate forms if you want to customize it. by default it generate forms base on table info

Code: Select all

<?php	
		$test = new add();
		$the_forms = array();
		// TEXTBOX
	    $the_forms []= array('label' => 'NAME','field_name' => 'name' , 'form_type' => 'textbox');
		// TEXTAREA		
		$the_forms []= array('label' => 'DESCRIPTION','field_name' => 'description' , 'form_type' => 'textarea');			
		// PASSWORD
	    $the_forms []= array('label' => 'PASSWORD','field_name' => 'password' , 'form_type' => 'password');
		// SELECT
		$options = array();
		$options []= array('label' => "Yes this is an active member", 'values' => "activeyes", 'selected' => '0');
		$options []= array('label' => "No, inactive member", 'values' => "activeno", 'selected' => '0');				
		$the_forms []= array('label' => 'ACTIVE MEMBERS' ,'field_name' => 'active' , 'form_type' => 'select', 'values' => $options);		
		//CHECKBOX
		$check_btn = array();
		$check_btn []= array('name'=> "check_box", 'label' => "alex1", 'values' => "test1");
		$check_btn []= array('name'=> "check_box", 'label' => "alex2", 'values' => "test2");	
		$check_btn []= array('name'=> "check_box", 'label' => "alex3", 'values' => "test3");		
		$the_forms []= array('label' => 'CHECK THIS' , 'field_name' => 'check_box' , 'form_type' => 'checkbox', 'values' => $check_btn);		
		// RADIO BUTTON
		$radio_btn = array();
		$radio_btn []= array('label' => "fishing", 'values' => "fishing");
		$radio_btn []= array('label' => "shopping", 'values' => "shopping");	
		$radio_btn []= array('label' => "basketball", 'values' => "basketball");		
		$the_forms []= array('label' => 'MY HOBBIES','field_name' => 'hobbies' , 'form_type' => 'radio', 'values' => $radio_btn);				
		
		$test->view_forms('members',$the_forms);	
	  ?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Here is what I see..

1. The db.php should be in its own file and not have any includes or defines.
2. The database connection methods should be in a seperate class so you can run multiple database queries simutaneously.
3. Your exec_query die's if there is an error, it should throw an exception or call an error handler. Errors should be handled, rather than just killing the script.
4. The write_td methods, and any other html generation methods, should be in a seperate class.
5. Pagination should be in a seperate class
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

astions pretty much covered everything, so I will just add to his response. Classes should not rely on any environmental factors such as constants. One of the big ideas behind OOP is encapsulation and loose coupling. You want your classes to be as independant on ouside factors as possible. I would set the connection settings in the constructor (host, user, password, etc.). Also... it is best to break down classes to as small and specific tasks as possible. Your class does too many things.
silverbux
Forum Newbie
Posts: 9
Joined: Mon Aug 14, 2006 8:50 pm

Post by silverbux »

thank you for your post, but how about my patterns and structure of the program? ive already separated some classes and function like you told me. im not really familiar with php oop patterns is this really how you do it?.
silverbux
Forum Newbie
Posts: 9
Joined: Mon Aug 14, 2006 8:50 pm

Post by silverbux »

it's surprising that there's not much errors in my method of coding.
or it may be that the code is too long :D
my post is still open for criticisms :D
Post Reply