分解平面文件结果

发布于 2024-11-23 15:38:43 字数 3545 浏览 1 评论 0原文

我使用的是平面文件数据库,而不是 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;

}

Function 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 天我被关在这里......用尽了所有解决方案。

帮助很高兴赞赏 谢谢

I am using flat file db, not mysql so I can't use $limit

Function 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;

}

Function 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";
 }

Call records - here is the problem - this works except for the break; - I need to explode the flat file to read each record individually, then add to the foreach():

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

Since this is flat file, when calling the function getbyfieldsw() the file comes flat as a single file, no matter how many records on it, records = 1 or records = 100 is the same at this point.

Break; does not work because all records come as single record - therefore nothing to break.

What I want to do is split/explode the records, count them, and based on my if statement post:

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

I got held here for the past 3 days...exhausted all solutions.

HELP glad appreciated
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-11-30 15:38:43

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

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

Sorry for not helping your code directly, but you should consider switching to SQLITE instead of using your own database system. SQLITE is flat, open-source, efficient and easier to work with. PHP's sqlite module has everything you need. You should check it out!

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文