2024 年 PHP 日本研討會

dBase 函式

範例

此參考中的許多範例都需要一個 dBase 資料庫。我們將使用 /tmp/test.dbf,它將在 dbase_create() 的範例中建立。

目錄

新增註解

使用者貢獻的註解 2 則註解

9
bi.idan AT gmail.com
17 年前
我知道你們很多人並不真的使用 dbase,但我建立了一個類別來幫助那些使用它的人。
(抱歉,我的英文不好)

- dbase.php

<?php

set_time_limit
(0);
// site_path defined by parent
require_once (SITE_PATH. '/server/php/libs/dbase/handler.php');

/* DBase (dbf)
* manage dbf files, exports and search functionality
* with buildin optimizers for fast performance
*/

class DBase
{
private
$handler = false;
private
$searchopt = array (); // Search optimizer

private function unload ()
{
if (
$this-> handler !== false)
unset (
$this-> handler);
}

public function
__construct ($file = false)
{
if (
$file !== false)
$this-> load ($file);
}

public function
__destruct ()
{
$this-> unload ();
}

public function
load ($file)
{
$resource = dbase_open ($file, 0);
$this-> handler = new DBase_Handler ($resource);

return
$this-> handler;
}

/* Search
* search for string inside header
* returns record number
* false returned if not found or error occurred
* limit_results gets int or false, limit_results equels one will limit the
* search results for one result only, false for no limit
*/
public function search ($headerText, $string, $limit_results = false, $handler = false)
{
if (
$handler === false)
$handler = $this-> handler;

if (
$this-> searchopt [$headerText][$string])
return
$this-> searchopt [$headerText][$string];
else
{
$size = $handler-> getSize ();
if ( (
$headerNumber = $handler-> getHeaderNumber ($headerText) ) !== false)
{
$results = array ();
for (
$i = 1; $i < $size; $i++)
{
$record = $handler-> getRecord ($i, false); // Disabled optimizer to prevent memory overflow
if (trim ($record [$headerNumber]) == $string)
{
$results[] = $i;

if ( (
$limit_results !== false) && (sizeof ($results) == $limit_results) )
break;
}
}

if (
sizeof ($results) > 0)
{
$this-> searchopt [$headerText][$string] = $results;
return
$this-> search ($headerText, $string, $handler);
}

return
false;
} else
return
false;
}
}
}

?>

- dbase_handler.php

<?php

/* DBase Handler (dbf)
* handles dbase resource
*/

class DBase_Handler
{
private
$resource;
private
$size; // Records Count
private $header = array ();
private
$dataopt = array (); // Data optimizer

private function setHeader ()
{
$this-> header = dbase_get_header_info ($this-> resource);
}

public function
__construct ($resource)
{
$this-> resource = $resource;
$this-> setHeader ();
$this-> size = dbase_numrecords ($this-> resource);
}

public function
__destruct ()
{
dbase_close ($this-> resource);
}

public function
getRecord ($record_number, $dataopt = true)
{
if (
$record_number > $this-> size)
return
false;
else
{
if (
$this-> dataopt [$record_number])
return
$this-> dataopt [$record_number];
else
{
$record = dbase_get_record ($this-> resource, $record_number);
if (
$dataopt === true) // Data saving optimizer
{
$this-> dataopt [$record_number] = $record;
return
$this-> getRecord ($record_number);
} else
return
$record;
}
}
}

public function
getHeaderNumber ($headerText)
{
foreach (
$this-> header as $index => $header)
{
if (
$header ['name'] == $headerText)
{
return
$index;
break;
}
}

return
false;
}

public function
getHeader ($headerNumber)
{
if (
$headerNumber <= sizeof ($this-> header))
return
$this-> header [$headerNumber];
else
return
false;
}

public function
getSize ()
{
return
$this-> size;
}
}

?>
-1
bi.idan [at] gmail.com
15 年前
有些人聯繫我關於備忘錄欄位和我的腳本,所以我會在這裡發布我對常見問題的解答。

(注意:這不是任何程式的廣告,只是我自己使用的,而且它是免費的)

那麼,如果您想建立/讀取備忘錄欄位,有兩個選項

1. 使用 Java/C++/C# 程式庫在 PHP 和 dbf 檔案之間建立橋樑。我使用了 dbf2java。雖然它不像我的其他選項那麼完整,但這是一個好的開始。(http://code.google.com/p/dbf2java-library/)

2. 使用帶有管道的外部程式來擷取輸出。我使用了 cdbflite,(http://www.whitetown.com/cdbflite/)。它是免費的,而且幾乎提供了您處理這些 dbf 檔案所需的一切。我不確定大型資料庫的情況,而且您似乎需要註冊/購買它,但這又是一個開始。

希望它能幫助一些需要處理這些資料庫檔案的人,

祝好運,
idan
To Top