【问题标题】:Limit number of records php - flat file限制记录数php - 平面文件
【发布时间】:2011-10-04 22:38:34
【问题描述】:

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

我需要将记录数限制为 1,如果超过 1 则回显其他内容:

$result = $db->getall(lmonth);
foreach($result as $item)
     show_record($item);
}

函数getall()

 /*!
    * @function getall
    * @abstract retrieves all records in the database, each record in an array
        * element.
    * @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 all database records as an array
    */
   function getall($orderby = NULL, $includeindex = false)
   {
      if (!$this->isopen)
      {
         user_error("Database not open.", 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);

         // Add the index field if required
         if ($includeindex)
            $record[FFDB_IFIELD] = $rcount++;

         // Add it to the result
         $result[] = $record;
      }

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

我尝试使用 break,但它返回 0(零)条记录。

我尝试使用$i = 0...但它返回全部或全部返回

有什么想法吗? 谢谢

【问题讨论】:

  • 告诉我们更多关于getall()show_record()的作用?
  • 我刚刚在上面添加了getall()和show_record()...
  • 你为什么不用mySql?

标签: php flat-file


【解决方案1】:

这样的事情呢?

function getall($orderby = null, $includeindex = false, $limit = null) {

    ...

    if ($orderby !== null) {
        $result = $this->order_by($result, $orderby);
    }
    if ($limit) {
        return array_slice($result, 0, $limit);
    }
    return $result;
}

【讨论】:

  • deceze,$limit 返回第一个记录键,而不是 if 语句正在调用的记录
  • @cchap 我忽略了需要预先读取所有记录的顺序,更新了答案。
  • 我刚刚在函数show_record()下面添加了if语句
  • 我必须打印我想要的记录: $result = $db->getall(lp_month); $i = 0; foreach($result as $item){ show_record($item);如果($i>=2)中断; } 但我无法让它在 1 后停止
  • 我进行了更新,但仍然只有第一条记录,当我关闭第一条记录时($status)我得到了空白页......所以它没有移动到下一条记录.我得到的最接近的结果是当我在 foreach() 中使用 $i = 0 时,但它并没有停止在为 true 时显示多条记录
【解决方案2】:

解决方案

Print_r 成功了,我使用的是 echo:

   print_r (show_record($row));

这是最终代码对我的工作方式:

$result = $db->getall(lp_month,lp_year);
$i = 0;
foreach ($result as $row){
   print_r (show_record($row));
if ($i >= 1) 
 break;
$i++;
}

现在期待解决其他小问题,谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多