2011-07-13 85 views
0

我使用的平面文件數據庫,而不是mysql的,所以我不能用$限制數限制 - 平面文件

我需要的記錄數限制爲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 ...但它返回全有或全無

任何想法? 謝謝

+2

告訴我們更多關於'getall()'和'show_record()'的知識嗎? –

+0

我剛剛在上面添加了getall()和show_record()... – cchap

+1

以及爲什麼不使用mySql? – genesis

回答

0

這樣的事情呢?

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; 
} 
+0

deceze,$ limit返回第一個記錄鍵,而不是if語句正在調用的記錄 – cchap

+0

@cchap我忽略了需要事先閱讀所有記錄的更新答案的排序。 – deceze

+0

我剛剛在函數show_record() – cchap

0

SOLUTION

的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++; 
} 

現在看向前解決其他小問題,謝謝