6 Replies Latest reply on Dec 16, 2015 2:14 AM by nicolai

    Help with using filemaker api for php?

    rj.rijo

      hello my name is rijo, and im new to filemaker.

      im referring this book - filemaker api for php 13 by Todd Duell.

      i need help with the line of code which is given on page number 46,

       

      PHP Script

      <?php include ("../../dbaccess.php");

      $request = $fm->newFindAllCommand('Demo php');

      $result = $request->execute();

      if (FileMaker::isError($result))

      {

      echo "<p>Error: " . $result->getMessage() . "</p>";

      exit;

      }

      $records = $result->getRecords();

      echo '<table border="1">

      <tr>

      <th>First Name</th>

      <th>Last Name</th>

      <th>Number Number</th>

      <th>Date</th>

      <th>Time Stamp</th>

      <th>Calculation</th>

      <th>Record ID</th>

      </tr>';

      foreach ($records as $record)

      {

      echo '<tr>';

      echo '<td>' . $record->getField('FirstName') .'</td>';

      echo '<td>' . $record->getField('LastName') . '</td>';

      echo '<td>' . $record->getField('Number') . '</td>';

      echo '<td>' . $record->getField('Date') . '</td>';

      echo '<td>' . $record->getField('TimeStamp') .'</td>';

      echo '<td>' . $record->getField('Calculation') .

      '</td>';

      echo '<td>' . $record->getField('RecordID') . '</td>';

      echo '</tr>';

      }

      echo '</table>';

      ?>

       

      i have some basic knowledge of php but im not able to understand the following line of code...

       

      $request = $fm->newFindAllCommand('Demo php');

      $result = $request->execute();

      if (FileMaker::isError($result))

      {

      echo "<p>Error: " . $result->getMessage() . "</p>";

      exit;

      }

      $records = $result->getRecords();

        • 1. Re: Help with using filemaker api for php?
          hbrendel

          I also have a very basic understanding of php, but I cannot imagine what it is you don't understand.

           

          There's a request to find all records, if there would be an error this would be displayed, otherwise those records are loaded in a variable...

           

          Please clarify.

          • 2. Re: Help with using filemaker api for php?
            nicolai

            I will try:

             

            Running execute() method on a  newFindAllCommand and then getRecords() on resulting object will return all records from the newFindAllCommand context.  You specify the context by passing the FileMaker layout to the object - ('Demo php').

             

            Open Filemaker and navigate to "Demo php" layout, you can check context there. Executing newFindAllCommand is the same as running "Show All Records" from Records menu in FIleMaker.

            • 3. Re: Help with using filemaker api for php?
              Devon Braun

              //$fm is a variable

              //-> for variables containing objects, there may be methods (functions) you can access.

              //newFindAllCommand() a call to a specific method in the object

               

              $request = $fm->newFindAllCommand('Demo php');       // Create a find object based on "Demo php" layout, store in $request

              $result = $request->execute();        //Execute find, returns an object, stored in variable $result

              if (FileMaker::isError($result)){         //Check if there's an error in the result.  N.B. Different way of accessing a method than the $var->method() approach

                echo "<p>Error: " . $result->getMessage() . "</p>"; //echo the error message in the $result object plus some text around it.

                exit; // End script.  i.e. no need to continue if you got a find error

              }

              $records = $result->getRecords(); //From the $result object, get the contained records -- also an object.  From this point you still have to process the $records object into usable text, as it contains (I think) self-referential sub objects that create infinite loops that trip up "foreach" and "print_r" commands.  It also contains the current user id and password, which no doubt you don't want to be echoing.

              • 4. Re: Help with using filemaker api for php?
                rj.rijo

                im not able to understand what this -> do exactly.

                 

                and why cant we declare it like this..$request = newFindAllCommand('Demo php');, i mean why we used $fm-> ?

                • 5. Re: Help with using filemaker api for php?
                  Devon Braun

                  The declaration you would prefer to use would require that newFindAll had been declared globally, but the FM developers, for a variety of reasons, chose not to.  Instead it's a function/method within the scope of the $fm object, i.e. a method contained within / private to that object.  The admittedly awkward looking  $VariableThatContainsAFunction->FunctionYouWantToAccess() technique is a PHP syntax for accessing private functions.

                   

                  Hope that helps.

                  • 6. Re: Help with using filemaker api for php?
                    nicolai

                    Good explanation from databoom . I would just add that it is standard PHP code and not a FileMaker specific syntax. You can use it as it is, or if you want to understand all details you will need to read up a bit more on PHP.