9 Replies Latest reply on Feb 9, 2016 5:31 AM by KevinMortimer

    fpdf php library. Trying to display a table of found records.

    KevinMortimer

      Hi

       

      I have been struggling for a few days now to figure out how to show a found set of records using the fpdf php library. The tutorial shows how to create a table but i am just unable to convert it so it works with a Filemaker found set. The example code that works great is attached

       

      Here is my code snippet which produces no error but also no results for the foreach looping function. I know that there are records as i have used the getFoundSetCount()  and 10 records were found. SO i am trying to put my $data variable into the "function FancyTable($header, $data)". I would be great full if someone could point out where i am going wrong. The fpdf library is attached. Many thanks

       

      $findRequest = $fm->newFindCommand('Discharges: Inpatient list');
      $findRequest->addFindCriterion('id_Hospital', "==".$idHospital );

      $findRequest->addFindCriterion('dateDischarge', "=" );

      $result = $findRequest->execute();

       

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

      $error = $result->getMessage();
      $pdf->WriteHTML('Unable to find Discharge records. ERROR: '.$error);
      }
      else{

          $records = $result->getRecords();

          $foundCount = $result->getFoundSetCount() ;

          $fields = $result->getFields();

          $header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');

       

          foreach($records as $record)

              $data = $record->getField("ward");

        }

       

          $pdf->SetFontSize(9);

      $pdf->SetY(40 ) ;

      $pdf->function FancyTable($header, $data)

        • 1. Re: fpdf php library. Trying to display a table of found records.
          tcfitzgerald

          I'm not familiar with the output of the PHP API, but what does $data look like in this instance?


          When you do this:


          foreach($records as $record)

                  $data = $record->getField("ward");

            }

           

          You are setting $data to the value of $record->getField("ward") over and over again.


          $data is only going to be the last value for $record->getField("ward").


          Can you do a print_r($data) and show the output?  and also a print_r($records)?

          • 2. Re: fpdf php library. Trying to display a table of found records.
            KevinMortimer

            Hi

             

            Thanks for the reply. print_r($data) produces the value 1. The foreach loop is supposed to produce a list of the data in the ward field across all the 10 found records. Looking at other filemaker API for PHP examples this is should work but i think i am missing something.

            • 3. Re: fpdf php library. Trying to display a table of found records.
              beverly

              This is the FPDF tutorial for FancyTable():

                   http://www.fpdf.org/en/tutorial/tuto5.htm

              Perhaps OP needs the print_r  to show what's returned.

               

              This is a link to the API tutorial:

                   http://www.fruitadvisor.info/filemakerphp/

              (and there are other places to help you with the API for PHP, including a few books)

               

              beverly

              • 4. Re: fpdf php library. Trying to display a table of found records.
                tcfitzgerald

                The output of print_r($data) being 1 doesn't seem right.  What about var_dump($data)?  Also, print_r($records) (or at least a sample of it)?


                If you want data to be an array, you're going to want to do something like this:


                $data = array();

                foreach($records as $record)

                        $data[] = $record->getField("ward");

                  }


                or this:


                $data = array();

                foreach($records as $record)

                        array_push($record->getField("ward"), $data);

                  }



                • 5. Re: fpdf php library. Trying to display a table of found records.
                  KevinMortimer

                  Hi Beverly

                   

                  Thanks....Yes i have already added the tuto5.php which i renamed as example.php on my first post. I do have other resources for the Filemaker API for PHP and my code lines up with those examples which is why i am stumped at the moment. I get no error which makes things hard to figure out.

                   

                  The tutorials do not show you how to get Filemaker data into the pdf table and when i try and rehash the code with Filemaker api for php code i get no error or result.

                   

                  Many Thanks

                  • 6. Re: fpdf php library. Trying to display a table of found records.
                    KevinMortimer

                    Hi

                     

                    Thanks....yes i tried the following suggestion with the following error.

                     

                    $data = array();

                        foreach($records as $record){

                           $data[] = $record->getField("ward");

                            }

                     

                    PHP Warning:  strlen() expects parameter 1 to be string, array given in fpdf.php on line 686

                    PHP Warning:  substr() expects parameter 1 to be string, array given in fpdf.php on line 787

                    • 7. Re: fpdf php library. Trying to display a table of found records.
                      tcfitzgerald

                      Your error messages and the files you attached aren't really matching up for me.  Line 686 of the attached fpdf.php is a closing curly brace and 787 is an open curly brace.

                       

                      How about you take FM out of the equation for a bit and just try to get fpdf working?

                       

                      Create an array: $data = array('one', 'two', 'three');

                      Then pass it to $pdf->FancyTable($header, $data);


                      Also, I just noticed you had this line in your OP $pdf-> function FancyTable($header, $data), is this what is actually in your code, or was that just a pasting error?

                      • 8. Re: fpdf php library. Trying to display a table of found records.
                        KevinMortimer

                        Ok I have made some progress. I am now able to view the $data array produced by the foreach loop but I have to convert it to a string to be able to view it in fpdf as fpdf does not seem to accept the data in arrays when displaying the data. You will see in code below I have disabled the lines that convert the code to strings as these must be disabled I think when running the function fancyTable ($header,$data );

                         

                                        $findRequest = $fm->newFindCommand('Discharges: Inpatient list');

                                        $findRequest->addFindCriterion('id_Hospital', "==".$idHospital );

                                        $findRequest->addFindCriterion('dateDischarge', "=" );

                                        $result = $findRequest->execute();

                         

                         

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

                                        $error = $result->getMessage();

                                        $pdf->WriteHTML('Unable to find Discharge records. ERROR: '.$error);

                                        }

                                        else{

                            $records = $result->getRecords();

                            $foundCount = $result->getFoundSetCount() ;

                            $fields = $result->getFields();

                            $header = array('Ward','Hospital' );

                            $data = array();

                            foreach($records as $record){

                               $data[] = $record->getField("ward");

                               $data[] = $record->getField("id_Hospital");

                             // $data[] = '|';

                                }

                           // $data = implode  (';',$data) ;

                           // $data = str_replace(";|;", "|", $data);

                           // $data = str_replace(";|", "", $data);     

                          }

                                         $pdf->SetFontSize(9);

                                         $currentY = $pdf->GetY();

                        $pdf->SetY($currentY + $rh) ;

                                        //$pdf->MultiCell(0,$rh,$data ,1,'L',0);

                                        $currentY = $pdf->GetY();

                                        $pdf->SetY($currentY + $rh) ;

                                        $pdf->fancyTable ($header,$data );

                         

                        -----------------------------------------------------------------------------------------------------------------------------

                         

                        In the tutorial the data is read from a text file which is being read as a string then converted into an array. I think my problem is that I am unable to get my $data array (of Filemaker found records) in the same format this function below gives. The file data looks like this

                         

                        Austria;Vienna;83859;8075

                        Belgium;Brussels;30518;10192

                        Denmark;Copenhagen;43094;5295

                         

                        function LoadData($file)

                        {

                                        // Read file lines

                                        $lines = file($file);

                                        $data = array();

                                        foreach($lines as $line)

                                                        $data[] = explode(';',trim($line));

                                        return $data;

                        }

                         

                        Below is the code for the function fancyTable

                         

                        function FancyTable($header, $data)

                        {

                                        // Colors, line width and bold font

                                        $this->SetFillColor(255,0,0);

                                        $this->SetTextColor(255);

                                        $this->SetDrawColor(128,0,0);

                                        $this->SetLineWidth(.3);

                                        $this->SetFont('','B');

                                        // Header

                                        $w = array(40, 35, 40, 45);

                                        for($i=0;$i<count($header);$i++)

                                                        $this->Cell($w[$i],7,$header[$i],1,0,'C',true);

                                        $this->Ln();

                                        // Color and font restoration

                                        $this->SetFillColor(224,235,255);

                                        $this->SetTextColor(0);

                                        $this->SetFont('');

                                        // Data

                                        $fill = false;

                                        foreach($data as $row)

                                        {

                                                        $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);

                                                        $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);

                                                        $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);

                                                        $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);

                                                        $this->Ln();

                                                        $fill = !$fill;

                                        }

                                        // Closing line

                                        $this->Cell(array_sum($w),0,'','T');

                        }

                        • 9. Re: fpdf php library. Trying to display a table of found records.
                          KevinMortimer

                          Hi all....ok i managed to work it out. Below is the code that now works. Thanks for all your input.

                           

                              $records = $result->getRecords();

                              $foundCount = $result->getFoundSetCount() ;

                              $fields = $result->getFields();

                              $header = array('Ward','Burn Date','First Name','Last Name','TBSA %','Open Burn','Comments' );

                             

                              $data = array();

                              foreach($records as $record){

                                 $data[] = $record->getField("ward");

                                 $data[] = $record->getField("DischargesFK_CasesID::dateBurn");

                                 $data[] = $record->getField("DischargesFK_CasesID::nameFirst");

                                 $data[] = $record->getField("DischargesFK_CasesID::nameLast");

                                 $data[] = $record->getField("DischargesFK_CasesID::TBSA")*100;

                                 $data[] = $record->getField("DischargesFK_CasesID::openBurnPHP");

                                 $data[] = $record->getField("DischargesFK_CasesID::Comments");

                                 $data[] = '|';

                              }

                           

                              $data = implode  (';',$data) ;

                              $data = str_replace(";;", ";-;", $data);

                              $data = str_replace(";|;", "|", $data);

                              $data = str_replace(";|", "", $data);

                              $data = explode('|',trim($data));

                              $lines = $data;

                             

                              $test = array();

                             foreach($lines as $line)

                              $test[] = explode(';',trim($line));

                                  

                            }

                              $pdf->SetFontSize(9);

                              $currentY = $pdf->GetY();

                            $pdf->SetY($currentY + $rh) ;

                              $pdf->fancyTable ($header,$test );