爆炸平面文件结果

时间:2011-07-15 01:52:48

标签: php if-statement explode flat-file

我使用的是平面文件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()时,文件作为单个文件变为平面,无论其上有多少记录,记录= 1或记录= 100在此时都是相同的。 / p>

歇;不起作用,因为所有记录都是单一记录 - 因此没有什么可以破解。

我想要做的是拆分/分解记录,计算记录,并根据我的if语句发布:

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

我在这里待了3天......用尽所有解决方案。

帮助很高兴 感谢

1 个答案:

答案 0 :(得分:0)

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

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