以最新的截止日期显示SINGLE RECORD

时间:2011-07-11 20:34:46

标签: php if-statement record

我正在使用FFDB数据库(平面文件数据库)。

只要$ vinc字段具有相同的值,此脚本就可以工作,但我有5种不同类型的$ vinc值R1,R2,R3,R4,R5 - 如果我添加新记录,其中$ vinc不是R1,a而是显示空白页。

  <?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($ result [0]);它显示1条记录,但最近的到期日期显示最新的到期日期:

即: 1记录到期08/01/2011 2记录到期11/01/2011

show_rec($结果[0]);显示截止日期为11/01/2011而不是08/01/2011的记录


Yegge

show_rec(端($结果));工作只要$ vinc == R1,如果添加另一条记录,其中vinc不是R1然后显示一个空白页,任何想法?

4 个答案:

答案 0 :(得分: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 in循环,如上所述。

答案 1 :(得分:0)

select product, expirationdate from your_table

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

{current_date}是应该从php传递的变量,或者你可以使用mysql函数

答案 2 :(得分: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));

答案 3 :(得分:0)

解决方案:

我只想确保发布解决方案:

$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 而不是 echo 可以解决问题:

   print_r (show_record($row));

由于