Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

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

Recommended Posts

Posted

I feel like there is going to be a simple answer to this question but I'm not sure what to do. Is there some issue with using Filemaker objects in functions I create? Here is my issue. I have created a function in my PHP file so I can do some recursive operations. But when I try to do a new search in this function I am getting the error that my filemaker file is an undefined variable.


<?php

    require_once ('FileMaker.php');

    $fm = new FileMaker();

    $fm->setProperty('database', 'flow');

    $fm->setProperty('hostspec', 'http://localhost');    

    $fm->setProperty('username', 'admin');

    $fm->setProperty('password', 'admin');	





function notWorking($StaffID){

    $levelFind =& $fm->newFindCommand('Staff');

    $levelFind->addFindCriterion('relatedID', $StaffID);

    $search = $levelFind->execute();

    $thisLevel = $search->getRecords();

}



$findCommand =& $fm->newFindCommand('Staff');

$findCommand->addFindCriterion('relatedID', "=");

$result = $findCommand->execute();

$topLevel = $result->getRecords();

$test=$topLevel[0]->getField('StaffID');

notWorking($test); ?>







When the function is called I get the error about fm not being a defined variable. 





Notice: Undefined variable: fm 

Fatal error: Call to a member function newFindCommand() on a non-object 

If I try and put a new FileMaker object in the function it gives me an error that getField is not a defined function. Any thoughts?

Posted

I'm thinking that it's not grabbing FileMaker.php properly, so it doesn't know what a new "FileMaker()" is.

How are the permissions on FileMaker.php? Does "Everyone" have at least "read" access? This has bitten me before when hosting on OSX - if you paste the FileMaker.php file into your web directory, it won't have the right permissions by default; you have to set them. You'll want to do that on the FileMaker directory, too.

Posted (edited)

unless otherwise specified, variables inside of a php function have local scope .

i.e.


$var = "one";

function foo(){

	$var = "two";

	echo $var;  

}

function bar(){

	global $var;

	echo $var;  

	$var = "three";

}



echo $var; /* "one" */

foo(); /* "two" */

bar();/* "one" */

echo $var; /* "three"*/





you could just declare $fm global inside of notWorking().  A better way  might be to just pass the Filemaker object to notWorking()




function notWorking($StaffID, $fm)




if you're getting an error on 


$test=$topLevel[0]->getField('StaffID');

that states getField is not a function that means that $topLevel is not an array of record objects.

Also error checking ( Filemaker::isError() ) after every call to Filemaker will make troubleshooting much easier.

Edited by Guest
my grammer is aweful
Posted

<?php

require_once ('FileMaker.php');

$fm = new FileMaker();

$fm->setProperty('database', 'flow');

$fm->setProperty('hostspec', 'http://localhost');

$fm->setProperty('username', 'admin');

$fm->setProperty('password', 'admin');

function getNextLevel($StaffID, $fm)

{

$levelFind =& $fm->newFindCommand('Staff');

$levelFind->addFindCriterion('zk_previous_f', $StaffID);

$search = $levelFind->execute();

$thisLevel = $search->getRecords();

if (FileMaker::isError($thisLevel)) {

$errorCode = $thisLevel->code;

if ( $errorCode == 401 ) {

return null;

} else {

echo "

Error: Level $StaffID - " . $thisLevel->getMessage() . "

";

exit;

}

} else {

return $thisLevel;

}

}

function printLevel($CurrentLevel, $fm)

{

$names = "

foreach ($CurrentLevel as $Person)

{

$name = $Person->getField('Name First') . " " . $Person->getField('Name Last');

$names .= "

" . $name . "";

$next = getNextLevel($Person->getField('zk_StaffID_p'), $fm);

}

echo $names . "

";

foreach ($next as $current){

if($current!=null){

printLevel($current, $fm);

}

}

}

$findCommand =& $fm->newFindCommand('Staff');

$findCommand->addFindCriterion('zk_previous_f', "=");

$result = $findCommand->execute();

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

echo "

Error: " . $result->getMessage() . "

";

exit;

}

$topLevel = $result->getRecords();

printLevel($topLevel, $fm);

?>

Posted

This part is a little weird it gets to $thisLevel = $search->getRecords(); and tells me this

Fatal error: Call to undefined method FileMaker_Error::getRecords() in /Library/WebServer/Documents/flow.php on line 17

That's not weird at all. Your find is throwing an error and your error checking is just a bit off. Try this




function getNextLevel($StaffID, $fm) {

	$levelFind =& $fm->newFindCommand('Staff'); 

    $levelFind->addFindCriterion('zk_previous_f', $StaffID);

    $search = $levelFind->execute();

    /*execute() will either return a Filemaker_Result Object or Filemaker_Error object*/

    if (FileMaker::isError($search)) {  

    	if ($search->getCode() == 401 ) {

        	return null;

        } else {

            echo "
Error: Level $StaffID - " . $search->getMessage() . "";

            return; /*just leave the function.  exit stops the script in it's tracks*/

        }

     } else { 

          return $search->getRecords(); /*just grabs the array or Filemaker_Record objects from the Filemaker Result Object*/

     }

        

}



This topic is 5791 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.