I am using a FFDB database (Flat File Database).

This script works but it only shows first record (#1), if I delete the 1st record I get a blank page. I want to call all records and find 1 (one) that matches the criteria(s) independent of its record (key) number.

I want to load this file when the HTML page loads (with or w/o iframe):

<?php
   /*!
    * @function getall
    * @abstract retrieves all records in the database, each record in an array
    * element.
    * @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($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();
      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->getall();
foreach($result as $item){
     show_record($item);
     break;
}


function show_record($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";

  }
  ?>

I may need to improve this is order to get the result, but I tried and it doesn't work, so here is the basic for help:

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

or perhaps improvement here:

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

Any help PLEASE?!

Thanks

Recommended Answers

All 5 Replies

If I remove 'break;' from line 80, then all R1 records are displayed but sorted by key, listing record #1 as 1st one, so apparently it is ignoring the conditions set on line 96.

I am trying to get 1 record where expiration date is the closest from today's date, vinc == R1 and status == ON

Why is it ignoring the conditions set on line 96?

Thanks

ON LINE 90 check variable names properly

if (($Rstatus == ON) && ($Rvinc == R1) && ($Rmonth >= $current_month) && ($Ryear == $current_year))


on line 80 you must keep break under some condition. otherwise it will go out of loop after one record

I posted incorrectly here...it is correct on my script...

I am still getting either 1st, all or none.

ON LINE 90 check variable names properly

if (($Rstatus == ON) && ($Rvinc == R1) && ($Rmonth >= $current_month) && ($Ryear == $current_year))


on line 80 you must keep break under some condition. otherwise it will go out of loop after one record

I posted incorrectly here...it is correct on my script...sorry

I am still getting either 1st, all or none.

SOLUTION

This is the solution for printing array:

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

Print_r did the trick while echo was printing all records as single record:

print_r (show_record($row));

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.