Solved

Update FileMaker Database  API PHP Code to Retrieve Query String

Posted on 2014-01-08
9
1,102 Views
Last Modified: 2014-01-15
Hello,

I have a little php site created by a wizard software that pulls data out of FileMaker.  I have a current page that currently retrieves the record id out of the url to look up the database record and display the fields from the record.  I want to make a slight modification that will allow me to instead send a PO parameter that would instead look up the record by the PO number instead of the record id when PO=12345 is sent in the query string.  I still want the existing functionality but need to know how to make it either look up record id or po number.  Here is the existing php section that pulls out the record id.

<?php

    /**
    * FileMaker PHP Site Assistant Generated File
    */

    require_once 'fmview.php';
    require_once 'FileMaker.php';
    require_once 'error.php';

    $cgi = new CGI();
    $cgi->storeFile();
   
    $databaseName = 'SalesFM7';
    $layoutName = 'XSLEditLayout';

    $userName = $cgi->get('userName');
    $passWord = $cgi->get('passWord');

    $fm = & new FileMaker();
    $fm->setProperty('database', $databaseName);
    $fm->setProperty('username', $userName);
    $fm->setProperty('password', $passWord);
   
    ExitOnError($fm);
    $layout = $fm->getLayout($layoutName);
    ExitOnError($layout);

    // formats for dates and times
    $displayDateFormat = '%m/%d/%Y';
    $displayTimeFormat = '%I:%M:%S %P';
    $displayDateTimeFormat = '%m/%d/%Y %I:%M:%S %P';
    $submitDateOrder = 'mdy';

    $recid = $cgi->get('-recid');
    if (!isset($recid))
        $recid = 1;
    $record = $fm->getRecordById($layoutName, $recid);
    ExitOnError($record);
   
    class EmptyRecord {
        function getRelatedSet($relationName) {
            return array(new EmptyRecord());
        }

        function getField($field, $repetition = 0) {
        }

        function getRecordId() {
        }
    }
   
?>

Open in new window

In slight testing on a test page it seems like the following type of code is needed to pull a po number:

$cmd =& $fm->newFindCommand('XSLLayout');
$cmd->addFindCriterion('PO', $_REQUEST['PO']);
$result = $cmd->execute();
if (FileMaker::isError($result)) {
    echo 'unable to find PO: ' . $result->message . '(' . $result->code . ')';
    die();
}
$records = $result->getRecords();

Open in new window

Does anyone know how to tweak these two pieces of code to work together to make my page a little more dynamic in allowing me either url search?

http://myipaddress/Folder/editrecord.php?PO=12346

or

as is working currently only:

http://myipaddress/Folder/editrecord.php?-action=browse&-recid=5138

Thanks in advance,
Shawn
0
Comment
Question by:smower
  • 4
  • 4
9 Comments
 
LVL 11

Expert Comment

by:jausions
Comment Utility
Do something like this around line 35 in the first block:

$recid = $cgi->get('-recid');
$recpo = $cgi->get('PO');
if (!isset($recid) && !isset($recpo)) {
   $recid = 1;
}
if (isset($recid) {
    $record = $fm->getRecordById($layoutName, $recid);
    ExitOnError($record);
} else {
    $cmd =& $fm->newFindCommand('XSLLayout');
    $cmd->addFindCriterion('PO', $recpo);
    $result = $cmd->execute();
    if (FileMaker::isError($result)) {
        echo 'unable to find PO: ' . $result->message . '(' . $result->code . ')';
        die();
    }
    $records = $result->getRecords();
    $record = array_pop($records);
}

Open in new window

0
 

Author Comment

by:smower
Comment Utility
Thanks, I tried it but it doesn't find the record when searching by po still.  It does still find the recid.
0
 
LVL 11

Expert Comment

by:jausions
Comment Utility
It depends if you have both -recid and PO parameters in your URLs. You may want to reverse the code to have PO takes precedence over -recid when testing with isset(). Also, maybe you're missing some parameters in the URL to trigger the code, have you tried adding the "-action=browse" parameter? For instance:

http://myipaddress/Folder/editrecord.php?-action=browse&PO=12346
0
 

Author Comment

by:smower
Comment Utility
Thank you.  You may be onto something.  I just am not that proficient with php code.  I did the -action=browse and then put the po code in both places and removed the recid code and then it searches by po.  I tried to put it back and now it is giving me php errors on the recid version saying undefined variable.  Do you know precisely how the code should look to just expect one or the other but not both?  I assume the code  with the && is looking for both at the same time?

Thanks,
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Assisted Solution

by:jausions
jausions earned 350 total points
Comment Utility
Depending on how your PHP is configured you may see notices which inform you of potential errors in your code. When the code tries to read / access a variable that hasn't been already assigned, PHP will output such notice. That's probably what you saw.

For the code, try something like this:

$recid = $cgi->get('-recid');
$recpo = $cgi->get('PO');
if (!isset($recid) && !isset($recpo)) {
   $recid = 1;
}
if (isset($recpo) {
    $cmd =& $fm->newFindCommand('XSLLayout');
    $cmd->addFindCriterion('PO', $recpo);
    $result = $cmd->execute();
    if (FileMaker::isError($result)) {
        echo 'unable to find PO: ' . $result->message . '(' . $result->code . ')';
        die();
    }
    $records = $result->getRecords();
    $record = array_pop($records);
} else {
    $record = $fm->getRecordById($layoutName, $recid);
    ExitOnError($record);
}

Open in new window


Then use http://myipaddress/Folder/editrecord.php?-action=browse&PO=12346 to access the page.

If no "PO" argument is passed, then the page will look for the "-recid" argument. If no "-recid" is passed either, then it defaults to a record ID of 1. That was the default previous behavior.

If you want to use a default PO instead of a default ID when nothing is passed, you may change that on line 4 above by setting the proper $recpo value instead of $recid.
0
 

Author Comment

by:smower
Comment Utility
Thank you.  Now I finally understand what the exclamation point is doing.  It is a negative operator saying this "is not".  Correct?

Anyway I updated the code and now it partially works.  If I start with a rec id it will pull up the item by recid. If I then change it to a po it will pull up the po which is good.  However if I then try to pull up something by recid again it just defaults to the last po everytime and will no longer let me look up something by recid unless I log out and then log back in and search by recid again?  Any ideas how to fix that?

Thanks,
Shawn
0
 
LVL 11

Accepted Solution

by:
jausions earned 350 total points
Comment Utility
Hmmm... it seems like it could be a separate issue higher up in the application. Maybe some caching of the URL parameters in a session or cookie. Since the "PO" parameter now has precedence over the "-recid" one, it's possible that the application remembers the previous "PO".

I don't know what line 12 of your original code is doing... maybe saving the URL parameters for reuse.

Unfortunately, you will have to investigate further. At least your original question was answered, and you seem on the good way.

There is one thing you could try, but it's a wild guess:

Instead of my line 2:
$recpo = $cgi->get('PO');

Open in new window


Use this:
$recpo = (isset($_REQUEST['PO'])) ? $_REQUEST['PO'] : null;

Open in new window


PS: By the way, my code before was missing a closing parens at line 6, but you probably fixed that on your own already.

Cheers,
 -Philippe
0
 

Author Closing Comment

by:smower
Comment Utility
It seems to be working with those changes.

Thank you!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now