【问题标题】:Explode Flat File Result展开平面文件结果
【发布时间】:2011-10-05 19:49:39
【问题描述】:

我使用的是平面文件 db,而不是 mysql,所以我不能使用 $limit

函数getbyfieldsw()

  /*!
* @function getbyfieldsw
* @abstract retrieves records in the database whose field matches the
* given regular expression.
* @param fieldname  the field which to do matching on
* @param regex  the regular expression to match a field on.
* Note: you should include the delimiters ("/php/i" for example).
* @param orderby  order the results.  Set to the field name to order by
* (as a string). If left unset, sorting is not done and it is a lot faster.
* If prefixed by "!", results will be ordered in reverse order.  
* If orderby is an array, the 1st element refers to the field to order by,
* and the 2nd, a function that will take two take two parameters A and B 
* - two fields from two records - used to do the ordering.  It is expected 
* that the function would return -ve if A < B and +ve if A > B, or zero 
* if A == B (to order in ascending order).
* @param includeindex  if true, an extra field called 'FFDB_IFIELD' will
* be added to each record returned.  It will contain an int that specifies
* the original position in the database (zero based) that the record is 
* positioned.  It might be useful when an orderby is used, and an future 
* operation on a record is required, given it's index in the table.
* @result matching records in an array, or false on failure
*/
   function getbyfieldsw($fieldname, $orderby = NULL, $includeindex = false)  {
  if (!$this->isopen)
  {
     user_error("Database not open.", E_USER_ERROR);
     return false;
  }

  // Check the field name
  if (!$this->key_exists_array($fieldname, $this->fields))
  {
     user_error(
        "Invalid field name for getbyfield: $fieldname", 
        E_USER_ERROR
     );
     return false;
  }

  // If there are no records, return
  if ($this->records == 0)
     return array();

  if (!$this->lock_read())
     return false;

  // Read the index
  $index = $this->read_index();

  // Read each record and add it to an array
  $rcount = 0;
  foreach($index as $offset)
  {
     // Read the record
     list($record, $rsize) = $this->read_record($this->data_fp, $offset);

     // See if the record matches the regular expression
        // Add the index field if required
        if ($includeindex)
           $record[FFDB_INDEX_RECORDS_OFFSET] = $rcount;

        $result[] = $record;


     ++$rcount;
  }

  $this->unlock();

  // Re-order as required
  if ($orderby !== NULL)
     return $this->order_by($result, $orderby);
  else
     return $result;

}

函数 show_record()

  function show_record($record){
  $month = $record["lmonth"];
  $status = $record["lstatus"];
  $year = $record["lyear"];
  }
 if (($status == ON) && ($month >= $current_month) && ($year >= $current_year)){
 echo "foo";
 }

通话记录 - 问题出在此 - 除了休息时间外,此功能有效; - 我需要分解平面文件以单独读取每条记录,然后添加到 foreach():

 $result = $db->getbyfieldsw(lp_month);
 $i = 0;
 foreach ($result as $item){
      show_record($item);
 if ($i >= 2) 
 break;
 }

由于这是平面文件,所以在调用函数getbyfieldsw()时,文件将作为一个文件平面化,无论有多少条记录,此时records = 1或records = 100都是一样的。

休息;不起作用,因为所有记录都作为单一记录出现 - 因此没有什么可以打破的。

我想要做的是拆分/分解记录,计算它们,并根据我的 if 语句帖子:

if $i == 0 echo "content1";
if $i == 1 echo "content2";
if $i > 1 echo "content 3";

过去 3 天我都被关在这里...用尽了所有解决方案。

帮助很高兴 谢谢

【问题讨论】:

  • 你有一个平面文件布局的例子吗?
  • 你这样做有什么理由吗? (文件与数据库)

标签: php if-statement explode flat-file


【解决方案1】:

很抱歉没有直接帮助您的代码,但您应该考虑切换到 SQLITE 而不是使用自己的数据库系统。 SQLITE 是扁平化、开源、高效且易于使用的。 PHP 的 sqlite 模块有你需要的一切。你应该去看看!

http://www.sqlite.org/about.html http://php.net/manual/en/book.sqlite.php

【讨论】:

    猜你喜欢
    • 2011-11-16
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 2016-04-12
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    相关资源
    最近更新 更多