3 Replies Latest reply on Jan 23, 2014 2:08 PM by jgill@occu-med.com

    Processing getRecords with foreach loop in PHP API

    jgill@occu-med.com

      Hello,

       

      I'm fairly new to PHP so I'm pretty sure I'm missing something easy here. I have a query that is returning 3 records that I want to loop through with a foreach loop and pull contents for each record from a particular field. After reading through the API, it sounds like they want you to assign the results to an array with the getRecords() method and then loop through the records with foreach loop. (At least that's what the API says...)

       

      Anyway, I have a simple query set up below:

      $fm = new FileMaker();

      $query = $fm->newFindAllCommand('myLayout');

      $result = $query->execute();


      $records = $result->getRecords();

      $foundSet = $result->getFoundSetCount();

      $fields = $result->getFields; echo '<p>Records Found: ' .$foundSet.'</br>'; <-- Returns 3 (the correct found set)


      foreach ($records as $record) {

      echo $record->getField('login').'</br>';

      }

      I can see that the $foundset & $fields variables contain the correct properties of the records for my found set, but when I go to loop through the records as outlined in th APU guide, the getField method returns the first record of the found set 3 times. I must be calling it incorrectly, but does anybody have any clue what I'm doing wrong?

        • 1. Re: Processing getRecords with foreach loop in PHP API
          jgill@occu-med.com

          I checked the spelling of the field, it's correct.  The field is coming from the same layout the query is being performed on, so I have no idea why it's returning the first result 4 times.

           

           

          Just to be sure, I changed the query up and with the results I'm getting now I'm just stumped.

           

           

          I made a new layout with the same name of the table that the database is querying, lets call it "Clinic".  This is a fairly large table and the search is being performed on the 'clinicName' field.  I put a die into the script to see what is coming back from the query.

           

           

           

           

              $ProviderName = Input::Get('clinicName');

             

                                  $query = $fm->newFindCommand('Clinic');

                                  $query->addFindCriterion('clinicName', $clinicName);

                                  $result = $query->execute();

             

              if(Filemaker::isError($result)) {

                        echo $result->getMessage();

              } else {

             

                        print_r($result);

                        die();

             

                        $records           = $result->getRecords();

                        $foundSet           = $result->getFoundSetCount();

                        $fields           = $result->getFields();

             

                        foreach ($records as $record) {

                                  echo $record->getField('clinicName') .'</br>';

                        }

              }

              ?>

           

           

          Querying the term "health" returned 783 results.  When I look at the $result variable, it's an array that contains 783 FilerMake Result Objects but each Result Object contains the same information.  Wat?

          • 2. Re: Processing getRecords with foreach loop in PHP API
            databuzz

            Hi,

             

            Try this and see if it makes a difference:

             

            $recnum = 1;

             

                 foreach ($records as $record) {

                 echo $record->getField('clinicName') .'</br>';

             

            $recnum++; } /* foreach record */

             

            ?>

            • 3. Re: Processing getRecords with foreach loop in PHP API
              jgill@occu-med.com

              Just figured it out.  I was using a newer version of PHP than the one officially suported (5.3.28 vs. 5.3.5) and had to replace the deprecated commands used in earlier version of the Filemaker PHP API.  Apparently somewhere along the road a few of the commands hadn't been replaced correctly and the API was not assigning values to the Filemaker Record Object.  I replaced the modified version of the Filemaker PHP API with the original copy and everything started to work as expected.