Показать одну запись с новейшей датой истечения срока действия - PullRequest
1 голос
/ 12 июля 2011

Я использую базу данных FFDB (плоская файловая база данных).

Этот сценарий работает до тех пор, пока поле $ 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 ($ result [0]);он показывает 1 запись, но вместо этого самая последняя дата истечения срока действия показывает самую последнюю дату истечения срока действия:

т.е.: 1 запись истекает 08/01/2011 2 записи истекает 11.01.2011

show_rec ($ результат [0]);показывает запись с датой истечения срока действия 01.11.2011 вместо 08.01.2011


Yegge

show_rec (end ($ result));работал до тех пор, пока только $ vinc == R1, если при добавлении другой записи, где vinc не R1, отображается пустая страница, есть идеи?

Ответы [ 4 ]

0 голосов
/ 15 июля 2011

РЕШЕНИЕ:

Я просто хочу опубликовать решение:

$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));

Спасибо

0 голосов
/ 12 июля 2011
select product, expirationdate from your_table

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

{current_date} - переменная, которая должна быть передана из php, или вместо нее можно использовать функцию mysql

0 голосов
/ 12 июля 2011

Если я правильно понял ваш код, вместо

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 голосов
/ 12 июля 2011
<?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";

  }
  ?>

Я думаю, вам нужно использовать разрыв в цикле, как описано выше.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...