精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● PHP>>荷音 PHP 库>>HEYIN PHP扩展库 (5)

主题:HEYIN PHP扩展库 (5)
发信人: bb_rain()
整理人: tang(2001-01-31 09:00:37), 站内信件
<?

/*********************************************************************
*********

Data Module for PHP3 & PHP4 ( File, v 1.00 )

Copyright (C) 2000 heyin.com, All Rights Reserved.
http://www.heyin.com
Rain ( Email: [email protected] Oicq: 1235686 )

cl_datafile.php, 2000/8/26

**********************************************************************
*********/


// Data Module Class

class DataModule {
var $FILE_NAME;
var $FD_NAME;

var $handle;

var $IN;
var $SUB;

var $WHERE;
var $ORDER_BY;
var $ORDER;
var $LIMIT;

// Create Data Module
function Create($FileName) {
global $DATA_PATH;

$this->FILE_NAME = $DATA_PATH.$FileName;
  }

  //  Set Value
  function Set($varname, $value) {
    $this->$varname = $value;
  }

  // open datafile
  function Open($type="", $Op="") {
    switch ($type) {
      case "HEAD_FILE":
        $this->handle=fopen($this->FILE_NAME.".000", $Op);
        break;
      case "DATA_FILE":
        $this->handle=fopen($this->FILE_NAME.".dat", $Op);
        break;
    }
  }

  // close datafile
  function Close($type="") {
    if (!empty($type)) fclose($this->handle);
  }

  //  Read Field Name
  function FieldName() {
    unset($this->FD_NAME);               //  Clear Field Name Array
    $this->Open("HEAD_FILE","r");
    $this->FD_NAME = fgetcsv($this->handle, 1000, ",");
    $this->Close("HEAD_FILE");
    $count=sizeof($this->FD_NAME);
    return $count;
  }

  //  Insert Data
  function Add() {
    $count = $this->FieldName();

    for ($i=0; $i<$count; $i++) { $FD_VALUE[$i]="''"; }

while ( list($key, $val) = each($this->IN) ) {
      for ($i=0; $i<$count; $i++) {
if ($this->FD_NAME[$i]==$key) $FD_VALUE[$i]="'".$val."'";
      }
    }

    $VALUE_LIST = implode(',', $FD_VALUE);

    $this->AutoSave($VALUE_LIST."\n", "a+");
  }

  //  Update Data
  function Edit() {
    $count = $this->FieldName();
    $this->Open("DATA_FILE", "r");
    $this->Sub();
    while ( $FD_VALUE = fgetcsv($this->handle, 100000, ",") ) {
      $result = $this->WhereSub($FD_VALUE, $count);
      if ($result) {
        reset($this->IN);
        while ( list($key, $val) = each($this->IN) ) {
          for ($i=0; $i<$count; $i++) {
if ($this->FD_NAME[$i]==$key) $FD_VALUE[$i]="'".$val."'";

          }
        }
      }
      $VALUE_LIST = implode(',', $FD_VALUE);
      $VALUE_LIST.= "\n";
      $NEW.= $VALUE_LIST;
    }
    $this->Close("DATA_FILE");
    $this->AutoSave($NEW, "w");
  }

  //  Read Data
  function Read() {
    global $OUT,$COUNT,$LINES;

    $count = $this->FieldName();
    $this->Open("DATA_FILE", "r+");

    $size=0;
    $this->Sub();
    while ( $FD_VALUE = fgetcsv($this->handle, 100000, ",") ) {
      $result = $this->WhereSub($FD_VALUE, $count);
      if ($result) {
        for ($i=0;$i<$count;$i++) {
$LINES[$size][$this->FD_NAME[$i]] = str_replace("'", "", $FD
_VALUE[$i]);
          //  Return LINES
        }
        $size++;
      }
    }
    $COUNT = $size-1;              //  Return $COUNT
    $OUT = $LINES[0];              //  Return $OUT

    $this->Close("DATA_FILE");
  }

  //  Delete Data
  function Del() {
    $count = $this->FieldName();
    $this->Open("DATA_FILE", "r");

    $this->Sub();
    while ( $FD_VALUE = fgetcsv($this->handle, 100000, ",") ) {
      $result = $this->WhereSub($FD_VALUE, $count);
      if (!$result) {
        $VALUE_LIST = implode(',', $FD_VALUE);
        $VALUE_LIST.= "\n";
        $NEW.= $VALUE_LIST;
      }
    }
    $this->Close("DATA_FILE");
    $this->AutoSave($NEW, "w");
  }

  //  Return $TOTAL
  function Total() {
    global $TOTAL;

    $count = $this->FieldName();
    $this->Open("DATA_FILE", "r+");

    $size=0;
    $this->Sub();
    while ( $FD_VALUE = fgetcsv($this->handle, 100000, ",") ) {
      $result = $this->WhereSub($FD_VALUE, $count);
      if ($result) $size++;
    }
    $TOTAL = $size-1;

    $this->Close("DATA_FILE");
  }

  //  Sub Parse
  function Sub() {
    $S = $this->SUB;
    $S = str_replace("=", "==", $S);
    $S = str_replace(">==", ">=", $S);
    $S = str_replace("<==", "<=", $S);
$S = str_replace("WHERE", "|", $S);
$S = str_replace("ORDER BY", "|", $S);
$S = str_replace("LIMIT", "|", $S);
$SUB = explode("|", $S);

$this->WHERE = $SUB[1];
    $var = explode(' ', $SUB[2]);
    $this->ORDER_BY = $var[0];
    $this->ORDER = $var[1];
    $this->LIMIT = str_replace(" ", "", $SUB[3]);

    unset($this->SUB);                   //  Clear Sub
  }

  //  Return Where
  function WhereSub($Fields,$Count) {
    $S = $this->WHERE;
    for ($i=0; $i<$Count; $i++) {
$S = str_replace($this->FD_NAME[$i], $Fields[$i], $S);
    }
    eval("\$RESULT = $S;");
    return $RESULT;
  }

  //  Auto Save
  function AutoSave($Data, $Op) {
    $this->Open("DATA_FILE", $Op);
    flock($this->handle, 2);
    fwrite($this->handle, $Data);
    $this->Close("DATA_FILE");
  }
}

?>

--
HEYIN 软件 http://www.heyin.com  
虚拟社区,聊天室,交友程序,网上超市,电子商务 ...

※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 61.133.178.72]

[关闭][返回]