2011-07-11 23 views
1

我正在使用FFDB數據庫(平面文件數據庫)。使用最新的到期日期顯示SINGLE RECORDORD

只要$ vinc字段具有相同的值,但我有5種不同類型的$ vinc值R1,R2,R3,R4,R5 - 如果我添加新記錄,其中$ vinc不是R1,反而會出現空白頁面。

<?php 

    function getbyfunction($selectfn, $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 it to the result if the $selectfn OK's it 
      if ($selectfn($record) == true) 
      { 
       // Add the index field if required 
       if ($includeindex) 
       $record[FFDB_IFIELD] = $rcount; 

       $result[] = $record; 
      } 

      ++$rcount; 
     } 

     $this->unlock(); 

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


    function returnRec($item){ 
      if($item) 
      return true; 
    } 

    $db = new FFDB(); 
    if (!$db->open("foo")) 
    { 
    $schema = array(
     array("key", FFDB_INT, "key"), 
     array("status", FFDB_STRING), 
     array("vinc", FFDB_STRING), 
     array("month", FFDB_STRING), 
     array("day", FFDB_INT), 
     array("year", FFDB_INT) 
    ); 
     // Try and create it... 
    if (!$db->create("foo", $schema)) 
    { 
     echo "Error creating database\n"; 
     return; 
    } 
    } 

    $result = $db->getbyfunction("returnRec", "vinc"); 
     show_rec(end($result)); 

    function show_rec($record){ 
    $number = $record["key"]; 
    $Rvinc = $record["vinc"]; 
    $Rstatus = $record["status"]; 
    $Rday = $record["day"]; 
    $Rmonth = $record["month"]; 
    $Ryear = $record["year"]; 

    $tday = getdate(); 
    $current_year = $tday['year']; 
    $current_month = $tday['month']; 

    if (($status == ON) && ($vinc == R1) && ($month >= $current_month) && ($year ==     current_year)){ 

    echo "myrecord $vinc $status $day $month $year"; 

    } 
    ?> 

有什麼幫助?

由於


Yegge,使用show_rec($結果[0]);這表明1分的記錄,而是最近到期日期顯示最新的到期日期:08/01/2011 2記錄過期11/01/2011

show_rec($結果 1記錄到期:

即[0]);被示出具有到期日期的11/01/2011代替08/01/2011


Yegge

show_rec(端($結果))的記錄;工作只要$ vinc == R1,如果添加另一個記錄,其中vinc不是R1然後顯示一個空白頁,任何想法?

+0

那麼,你使用什麼樣的數據庫? – Charx

回答

0
<?php 

    function getbyfunction($selectfn, $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 it to the result if the $selectfn OK's it 
      if ($selectfn($record) == true) 
      { 
       // Add the index field if required 
       if ($includeindex) 
       $record[FFDB_IFIELD] = $rcount; 

       $result[] = $record; 
      } 

      ++$rcount; 
     } 

     $this->unlock(); 

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


    function returnRec($item){ 
      if($item) 
      return true; 
    } 

    $db = new FFDB(); 
    if (!$db->open("foo")) 
    { 
    $schema = array(
     array("key", FFDB_INT, "key"), 
     array("status", FFDB_STRING), 
     array("vinc", FFDB_STRING), 
     array("month", FFDB_STRING), 
     array("day", FFDB_INT), 
     array("year", FFDB_INT) 
    ); 
     // Try and create it... 
    if (!$db->create("foo", $schema)) 
    { 
     echo "Error creating database\n"; 
     return; 
    } 
    } 

    $result = $db->getbyfunction("returnRec", "vinc"); 
    foreach($result as $item) {show_rec($item);break;} 

    function show_rec($record){ 
    $number = $record["key"]; 
    $Rvinc = $record["vinc"]; 
    $Rstatus = $record["status"]; 
    $Rday = $record["day"]; 
    $Rmonth = $record["month"]; 
    $Ryear = $record["year"]; 

     $tday = getdate(); 
    $current_year = $tday['year']; 
    $current_month = $tday['month']; 

    if (($status == ON) && ($vinc == R1) && ($month >= $current_month) && ($year ==     current_year)){ 

    echo "myrecord $vinc $status $day $month $year"; 

    } 
    ?> 

我認爲你需要在循環中使用break,就像上面一樣。

0
select product, expirationdate from your_table 

where expirationdate > {current_date} order by expirationdate ASC limit 1 

{CURRENT_DATE}是變量,應該從PHP傳遞,或者您可以使用MySQL的功能,而不是

0

如果我理解正確的代碼,而不是

foreach($result as $item) {show_rec($item);break;} 
//use 
show_rec($result[0]); //only show the very first item in the result array 

編輯:然後用這:

show_rec(end($result)); 
0

SOLUTION:

我只是想確保發佈的解決方案:從我最初有

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

不是一個很大的不同,但的print_r的代替呼應的伎倆:

print_r (show_record($row)); 

謝謝