Hallo,
Ik ben hier om m'n stage plek en ik heb niet de beschikking over een php server... kan iemand mijn scripten testen zodat ik weer verder kan? Het is een class die gebruik maakt van een flat file en het idee heeft van een DBMS heel heeeeel simpel... dit is trouwens de eerste keer dat ik OOP programmeer... wil iemand dit doen?
Dit is een voorbeeld voor het gebruik van het script
Voorbeeld databees bestandje: (Moet met tabs gescheiden zijn)
PS: Ik hier niets in de FAQ gelezen over dit, maar ik weet het niet zeker...
Ik ben hier om m'n stage plek en ik heb niet de beschikking over een php server... kan iemand mijn scripten testen zodat ik weer verder kan? Het is een class die gebruik maakt van een flat file en het idee heeft van een DBMS heel heeeeel simpel... dit is trouwens de eerste keer dat ik OOP programmeer... wil iemand dit doen?
PHP:
1
| <?class Flat_DB { var $error_list; // List with errors var $current_db_file; // Curent db file var $fp; // File open 0/1 var $records_arr = array(); // All lines in the file as a array var $record = array(); // Last opend record var $records = array(); // Current records var $result = array(); // Result of a select // Update a record, remember that you include all the fields, alsow the empty. The last three are // the value to compare to, the field number and does it have to be a compair or a like (1,2) If you want to update all the fields in the // database you stil have to use the last fields but ust fill in nothing function Flat_Update_Record() { if(!Flat_DB_Open()) { return false; } else { $record_int = array(); $numargs = func_num_args(); if($numargs > 3) { $arg_list = func_get_args(); for($i=0;$i<=sizeof($arg_list)-3;$i++) { $record_int[] = $arg_list[$i]; } $records_int = Flat_Get_Records(); for($i=0;$i<=sizeof($records_int);$i++) { if($arg_list[$num_args] == 1) { if($record_int[$i][$arg_list[$num_args-1]] == $arg_list[$num_args-2]) { $this->records[$i] = $record_int[$i]; } } elseif($arg_list[$num_args] == 2) { if(ereg($arg_list[$num_args-2],$record_int[$i][$arg_list[$num_args-1]])) { $this->records[$i] = $record_int[$i]; } } } for($i=0;$i<=sizeof($this->records);$i++) { for($i_int=0;$i_int<=sizeof($this->records[$i])-1;$i_int++) { fputs($this->fp,"\""$this->records[$i][$i_int]."\" "); } fputs($this->fp,"\""$this->records[$i][sizeof($this->records[$i])]."\"\n"); } flock($this->fp,3); } else { Flat_Add_Error('You have to give at least 4 arguments (Flat_Update_Record(field,field,etc,value,fieldnumber))'); } } } // Inserts a record, yust give all the fields as an argument like Flat_Inset_Field($field1, $field2, etc, etc) function Flat_Insert_Record() { if(!Flat_DB_Open()) { return false; } else { $numargs = func_num_args(); $arg_list = func_get_args(); for ($i=0;$i<=$numargs;$i++) { $field = "\"".$arg_list[$i]."\""; if($i == sizeof($numargs)) $field .= "\n"; else $field .= " "; if(!fputs($fp,$field)){ Flat_Add_Error('Coudn\'t add record'); return false; } } } } // Returns the records who meet with the specified value in a specified field function Flat_Reverse_Result($resultin = '') { if(!Flat_DB_Open()) { return false; } else { if(empty($resultin)) $resultin = $this->records; for($i=0;$i<=sizeof($resultin);$i++) $this->result[sizeof($resultin)-$i] = $resultin[$i]; return $this->result } } // Returns a array with all the records (Fields seperated by a tab) function Flat_Get_File_Array() { if(!Flat_DB_Open()) { return false; } else { while(!feof($this->fp)) { $this->records_arr[] = fgets($this->fp, 4096); } return $this->records_arr; } } // Returns the records who meet with the specified value in a specified field function Flat_Select_Record($value,$field) { if(!Flat_DB_Open()) { return false; } else { for($i=0;$i<=sizeof($this->records);$i++) { $this->records[$i][$field] = $value; $this->result[] = $this->records[$i]; } return $this->result } } // Returns the records who are like the specified value in a specified field function Flat_Search_Record($value,$field,$case = '1') { if(!Flat_DB_Open()) { return false; } else { for($i=0;$i<=sizeof($this->records);$i++) { if($case) if(ereg($value,$this->records[$i][$field])) $this->result[] = $this->records[$i]; elseif(!$case) if(eregi($value,$this->records[$i][$field])) $this->result[] = $this->records[$i]; } return $this->result; } } // Puts the records in an two demensional array function Flat_Get_Records() { if(!Flat_DB_Open()) { return false; } else { if(sizeof($this->records_arr) <= 0) Flat_Get_File_Array(); for($i=0;$i<=sizeof($this->records_arr);$i++) { $int_arr = split(" ",$this-records_arr[$i]); for($int_i=0;$int_i<=sizeof($int_arr);$int_i) { $this->record[$int_i] = substr(substr($int_arr[$int_i],1),-1); } $this->records[$i] = $this->record; } } } // Checks if DB is open function Flat_DB_Open() { if(!$this->fp) { Flat_Add_Error('DB File not opened'); return false; } else { return true; } } function Flat_Close_DB($fp){ if(!Flat_DB_Open()) { return false; } else { unset($this->fp); fclose($fp); } } // Opens a file for reading and writing function Flat_Open_DB($file) { if(empty($file)) { Flat_Add_Error('You didn\'t specify a file'); return false; } else { $this->fp = fopen($file,'r+'); if(!$this->fp) { Flat_Add_Error('Error opening file'); return false; } else { $this->current_db_file = $file; return true; } } } // Used for adding a error to the error list function Flat_Add_Error($err) { if(!empty($err)) { $this->error_list[] = $err; } } // Returns all errors in a array function Flat_All_Errors() { return $this->error_list; } // Use this function to show error's function Flat_Error($no = '') { if(empty($no)) $no = sizeof($this->error_list); else $no = sizeof($this->error_list) + $no; echo $this->error_list[$no]; }}?> |
Dit is een voorbeeld voor het gebruik van het script
PHP:
1
| <?include("flat_db.php"); // Include the class file$flat = new Flat_DB; // Make a new object$flat->Flat_Open_DB("exampledb.cvs"); // Open the DB file exampledb.cvs$records = $flat->Flat_Get_Records(); // Get the records and put them in a arrayfor($i=0;$i<=sizeof($records);$i++) { // Go with a loop trough the records echo "Id: $records[$i][0]<br>\n"; // Each field has a number, starting with 0 (zero) echo "Name: $records[$i][1]<br>\n"; echo "Adress: $records[$i][2]<br>\n";}$flat->Flat_Close_DB(); // You don't have to but it is better Close the DB file?> |
Voorbeeld databees bestandje: (Moet met tabs gescheiden zijn)
code:
1
2
| "1" "Ate Bontjer" "De Voorreep 48" "2" "Piet Paulusma" "Pieterlaan 12" |
PS: Ik hier niets in de FAQ gelezen over dit, maar ik weet het niet zeker...