PHP文字資料表類 (轉)
剛學習,由於網站空間不支援,就寫了這個類,正好來練習練習.
/*
This TextData Class txtTbl writen by ridge.jiang to:mostone@.com">mostone@hotmail.com and finished in .01.01
Your can copy and use it without agreedment,It's free. But you can't use it for busness using.
You are also wellcome to modify this code in your mind. Thank for your work and tell me.
This class is for small information database so like classmater databse,they are less than 300 recorders.
If your want store and opreat more than 300 recorders,It's recommandly to use SERVER.
Your may acess the Instance of this Class with bellow function,there recommandly Do NOT use
inner var and function,because that's unsafe.
create()
drop()
open()
close()
eof()
bof()
prev()
next()
first()
end()
fieldunt()
getValue()
setValue()
display()
location()
recNO()
recCount()
del()
append()
*/
define ("tblPath",".");
define ("exten",".php");
define ("fileHead"," echo "You are wellcome!"?".">This file only for class txtTbl");
class txtTbl {
var $innerName=""; //資料庫名稱
var $innerCount; //資料庫記錄數目
var $innerFields; //資料庫欄位列表陣列
var $inner_F_Count; //資料庫欄位數目
var $fullName; //完整的名
var $isModify = false; //當前記錄是否被修改
var $fileModify = false; //資料庫是否被修改
var $innerRecorders; //資料庫記錄陣列
var $curLine; //當前記錄號
var $curArray; //當前行陣列
var $stringDel; //儲存被刪除記錄
var $sprt1; //資料庫記錄間的分隔符
var $sprt2; //資料庫欄位間的分隔符
var $innerBof = true;
var $innerEof = false;
function create($tblName,$fields,$sprt1=" if (empty($tblName)){
echo "The textDateBase file name not appoint.";
return false;
}
$fullName = tblPath.$tblName.exten;
if (file_exists($fullName)){
echo "The textDateBase file is already exist.";
return false;
}
if(empty($fields)){
echo "The fields list Array is invalid.";
return false;
}
$cont = implode($sprt2,$fields);
$cont = fileHead."n".$cont;
$fp = fopen($fullName,"w");
fwrite($fp,$cont);
fclose($fp);
return true;
}
function drop($tblName,$sprt1=" if (empty($tblName)){
echo "The textDateBase file name not proveid.";
return false;
}
if (!empty($this->innerName)){
echo "Current file not closed,Please close it and try again.";
return false;
}
$fullName = tblPath.$tblName.exten;
if (!file_exists($fullName)){
echo "The textDateBase file not exist.";
return false;
}
$fp = fopen($fullName,"r");
if (!feof($fp)){
$readFromFile = fgets($fp);
}
if ($readFromFile!=fileHead."n"){
fclose($fp);
echo "not a valid textDataBase file.(the head is invalid.)"."n";
return false;
}
$readFromFile = "";
if (!feof($fp)) $readFromFile.= fgets($fp);
fclose($fp);
$readFromFile = trim($readFromFile);
if (empty($readFromFile)){
echo "not a valid textDataBase file.(can't find fields define.)";
return false;
}
$cont = fileHead."n".$readFromFile;
$fp = fopen($fullName,"w");
fwrite($fp,$cont);
fclose($fp);
return true;
}
function open($tblName,$sprt1=" if (empty($tblName)){
echo "The textDateBase file name not proveid.";
return false;
}
if (!empty($this->innerName)){
echo "Current file not closed,Please close it and try again.";
return false;
}
$this->fullName = tblPath.$tblName.exten;
if (!file_exists($this->fullName)){
echo "The textDateBase file not exist.";
return false;
}
$fp = fopen($this->fullName,"r");
if (!feof($fp)){
$readFromFile = fgets($fp);
}
if ($readFromFile!=fileHead."n"){
fclose($fp);
echo "not a valid textDataBase file.(the head is invalid.)"."n";
return false;
}
$readFromFile = "";
while (!feof($fp)) $readFromFile.= fgets($fp);
fclose($fp);
$readFromFile = trim($readFromFile);
if (empty($readFromFile)){
echo "not a valid textDataBase file.(can't find fields define.)";
return false;
}
$this->innerRecorders = explode($sprt1,$readFromFile);
$this->innerCount = count($this->innerRecorders) - 1;
$this->innerFields = explode($sprt2,$this->innerRecorders[0]);
$this->innerFieldsCount = count($this->innerFields);
$this->innerName = $tblName;
$this->sprt1 = $sprt1;
$this->sprt2 = $sprt2;
if ($this->innerCount==0){
$this->curLine = 0;
$this->innerEof = true;
}else{
$this->curLine = 1;
// if ($this->innerCount==1) $this->innerEof = true;
if (!$this->initRec()) return false;
}
return true;
}
function close(){
if (empty($this->innerName)) return true;
//save modify
$isModify= false;
if ($this->isModify){
$this->saveModify();
$isModify= true;
}
if(isset($this->stringDel)){
$isModify= true;
$delNo= explode(",",$this->stringDel);
foreach($delNo as $no){
$no= (integer) $no;
unset($this->innerRecorders[$no]);
}
}
if ($isModify||$this->fileModify){
$recorders= implode($this->sprt1,$this->innerRecorders);
$recorders= fileHead."n".$recorders;
$fp = fopen($this->fullName,"w");
fwrite($fp,$recorders);
fclose($fp);
}
$this->innerName="";
unset($this->innerRecorders);
unset($this->curArray);
}
function next(){
if ((!$this->innerEof)&&(!empty($this->innerName))){
if($this->curLine==$this->innerCount){
$this->innerEof = true;
return true;
}
$this->saveModify();
$this->curLine++;
if ($this->innerBof) $this->innerBof = false;
$this->initRec();
}
return false;
}
function prev(){
if ((!$this->innerBof)&&(!empty($this->innerName))){
$this->saveModify();
$this->curLine--;
if ($this->curLine == 1)
$this->innerBof = true;
if ($this->innerEof) $this->innerEof = false;
$this->initRec();
}
}
function first(){
if ($this->innerBof||empty($this->innerName))
return false;
$this->saveModify();
$this->curLine = 1;
$this->innerBof= true;
$this->innerEof = false;
$this->initRec();
}
function end(){
if ($this->innerEof||empty($this->innerName))
return false;
$this->saveModify();
$this->curLine = $this->innerCount;
$this->innerEof= true;
$this->innerBof = false;
$this->initRec();
}
function eof(){
if (empty($this->innerName)){
return false;
}else return $this->innerEof;
}
function bof(){
if (empty($this->innerName)){
return true;
}else return $this->innerBof;
}
function recNo(){
return $this->curLine;
}
function recCount(){
return $this->innerCount;
}
function fieldsCount(){
if (empty($this->innerName)){
return false;
}else return $this->inner_F_Count;
}
function getValue($field){
if ($this->curLine==0||empty($this->innerName)){
echo "Can't read current record,maybe not in use or no record.";
return false;
}
$field= $this->chkField($field);
if ($field==-1){
return false;
}
return $this->curArray[$field];
}
function setValue($field,$value){
if ($this->curLine==0||empty($this->innerName)){
echo "Can't read current record,maybe not in use or no record.";
return false;
}
$field= $this->chkField($field);
if ($field==-1){
return false;
}
$this->curArray[$field]= $value;
$this->modify= true;
}
function display($shownon=0,$sprt1="
echo $sprt3;
foreach($this->curArray as $v){
if($shownon==1&&empty($v)) $v= "noValue";
echo $sprt1.$v.$sprt2;
}
echo $sprt4;
}
function location($field,$keyValue){
$field=$this->chkField($field);
if ($field==-1) return false;
for($i=$this->curLine;$i<=$this->innerCount;$i++){
if($this->curArray[$field]==$keyValue){
return true;
}
$this->next();
}
return false;
}
function del($recNo=-1){
if($this->curLine==0) return false;
$vartype= gettype($recNo);
if($vartype!="integer"){
echo "del error:check ur para type.";
return false;
}
if ($recNo==-1){
$recNo=$this->curLine;}
elseif ($recNo>$this->innerCount||$recNo<1){
echo "del error:out over the rang.";
return false;
}
if (!$this->chkDel($recNo)){
if(isset($this->stringDel)){
$this->stringDel.=(','.$recNo);
}else $this->stringDel = (string) $recNo;
}else return false;
}
function append($fields=""){
$this->saveModify();
for($i=1;$i<=$this->innerFieldsCount;$i++)
$newRec[] = "";
if(!empty($fields)){
foreach($fields as $k=>$v){
$k= $this->chkField($k);
if ($k==-1){
return false;
}
$newRec[$k]= $v;
}
}
$this->innerCount++;
$this->curLine = $this->innerCount;
$this->innerBof = false;
$this->innerEof = true;
unset($this->curArray);
$this->curArray = &$newRec;
$this->isModify = true;
}
//儲存修改
function saveModify(){
if($this->isModify){
$this->innerRecorders[$this->curLine]= implode($this->sprt2,$this->curArray);
$this->isModify = false;
$this->fileModify= true;
}
}
//當指標發生變化時,初始化當前記錄陣列
function initRec(){
$this->curArray = explode($this->sprt2,$this->innerRecorders[$this->curLine]);
if (count($this->curArray)!=$this->innerFieldsCount){
echo "The Current Recorder fields count unequal to Table's.n File will close.";
$this->close();
return false;
}
return true;
}
//輸出當前記錄資訊,設計為用
function ddisplay(){
if ($this->innerCount==0) return false;
foreach($this->innerFields as $v) echo $v."----";
echo "
";
foreach($this->curArray as $v) echo $v."---";
}
//檢查記錄是否已被刪除
function chkDel($key){
if (empty($key)&&$key!=0){
echo "the key not appoint.";
return false;
}
if (!isset($this->stringDel)){
return false;
}
if (ereg("(^|,)".$key."(,|$)",$this->stringDel)){
return true;
}
return false;
}
//檢查提交的欄位名是否合法.
function chkField($field){
if (empty($field)&&($field!=0)){
echo "the field not appoint.";
return -1;
}
$vartype = gettype($field);
switch ($vartype) {
case "integer":
if ($field>=$this->innerFieldsCount){
echo "the field is large than fieldscount";
return -1;
}elseif($field<0){
echo "the field is less than 0";
return -1;
}
return $field;
case "string":
foreach ($this->innerFields as $k=>$v) if ($field==$v) return $k;
echo "the field name not found.";
return -1;
default:
echo "the field is invalid.";
return -1;
}
}
}
?>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-996687/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PHP文字資料庫類之管理(txtDB.manager.php) (轉)PHP資料庫
- PHP常用操作類實現——資料庫操作類PHP資料庫
- 【PHP】CI框架原始碼DB.php(資料庫類)PHP框架原始碼資料庫
- 資料類新轉換
- 利用js解析php的表單資料JSPHP
- php簡單操作mysql資料庫的類PHPMySql資料庫
- 類似資料字典的幾個表
- Java 第9 章 : 資料表與簡單Java類對映轉換Java
- PHP中文字串轉陣列PHP字串陣列
- 將資料從文字匯入到mysql(轉)MySql
- (轉)PHP連線資料庫之PHP連線MYSQL資料庫程式碼PHP資料庫MySql
- Django實踐(二)——使用模型類定義資料表,實現表單頁面跳轉Django模型
- 轉向Kotlin——資料類和封閉類Kotlin
- 【PHP資料結構】雜湊表查詢PHP資料結構
- 3. php資料型別、資料型別轉換PHP資料型別
- PHP Oracle 資料庫函式庫(轉)PHPOracle資料庫函式
- 使用FSO把文字資訊匯入資料庫 (轉)資料庫
- PHP根據資料表自動生成CURD操作PHP
- HTML表單與PHP進行資料互動HTMLPHP
- /*列轉行查詢表資料*/
- php之資料型別自動轉換PHP資料型別
- 客戶價格種類設定資料表
- [轉帖]達夢資料庫-統計資料表資料量及空間表大小資料庫
- 使用 Linux 文字工具簡化資料的提取(轉)Linux
- 四十三章 PHP 製作資料透視表PHP
- PHP 表單提交後臺資料驗證 ValidatorPHP
- 常用的 PHP 類庫 , 資源PHP
- 常用的 PHP 類庫 資源PHP
- ADO資料與XML資料間的轉換的類(ASP實現) (轉)XML
- PHP資料型別轉換(字元轉數字,數字轉字元)PHP資料型別字元
- 【PHP資料結構】插入類排序:簡單插入、希爾排序PHP資料結構排序
- NLP相關問題中文字資料特徵表達初探特徵
- 使用PHP向MySQL資料庫匯入資料,中文字元顯示亂碼問題PHPMySql資料庫字元
- PHP文字操作_FILESPHP
- PHP呼叫java類的兩種方法(轉)PHPJava
- PHP中 ADOdb 類庫介紹(轉)PHP
- [PHP]不同作業系統下PHP接收POST資料問題 (轉)PHP作業系統
- WCF使用資料集(DataSet)、資料表(DataTable)、集合(Collection)傳遞資料 (轉)