Jump to content
Server Maintenance This Week. ×

Processing getRecords() with foreach Loop


This topic is 3485 days old. Please don't post here. Open a new topic instead.

Recommended Posts

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?

Link to comment
Share on other sites

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?
Link to comment
Share on other sites

Assuming that you have set up the newFilemaker() command correctly (your first post did not show it), the only thing I could find wrong was the position of the closing "}" for the else statement (and some assorted additional comments as indicated below):

   
$ProviderName = Input::Get('clinicName');// ?? this is not valid php. Is it just explanatory?
    
    $query = $fm->newFindCommand('Clinic');
    $query->addFindCriterion('clinicName', $clinicName);
    $result = $query->execute();
    
    if(Filemaker::isError($result)) {
    echo $result->getMessage();
    } else {
    
     //print_r($result); This provides TMI and is hard to decipher
    die();
      } //Closing brace for else belongs here... 
    $records = $result->getRecords();
    $foundSet = $result->getFoundSetCount();;//I've never used this command; I usually use count($records)
    $fields = $result->getFields();// This just returns a list of the field names in the target layout.     
// It's not necessary for your current situation. 
    
    foreach ($records as $record) {
    echo $record->getField('clinicName') .'</br>';
     // } Not Here
    }
    ?>

 

Link to comment
Share on other sites

Doughemi - no, the braces are in the right places as far as I can see...

 

Properly formatted, it looks like:

<?php

	$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(); //Commented - the script would halt here under production
		
		$records = $result->getRecords();
		$foundSet = $result->getFoundSetCount();
		$fields = $result->getFields();
		
		foreach ($records as $record) {
			echo $record->getField('clinicName') .'</br>';
		}
    }
    ?>
Link to comment
Share on other sites

  • 8 months later...

Hello,

Hopefully you found a solution to your problem. I'm experiencing the exact same problem.

My result set has 42 elements in it but when I foreach through it and echo the results I get 42 lines of the same thing (products from the stock items table in my case).

Is there an answer?

Thanks

P.s. I could view a member's profile the minute I found this site but when I register and sign in I get told I can't view member's profiles yet??? is that a known issue?

Can you post your code please?

I started a new thread with my query and code. http://fmforums.com/forum/topic/93804-php-api-result-set-contains-multiple-versions-of-the-first-result/

Thank you.

Link to comment
Share on other sites

  • 2 weeks later...

This topic is 3485 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.