passing one variable from one page to another for pdf generation

1.1k views Asked by At

I have a page that I have used to retrieve some excel data written in php... by using phpexcel... this part provides me company information

echo '<form action="final.php" method="post">';
            echo "<table border='1'>";



            for ($rowcount = $rowCompanyInfoStart; $rowcount <= $rowCompanyInfoEnd; $rowcount++)
            { 

            //$data = $objWorksheet->rangeToArray('A1:' . $maxCell['column'] . $maxCell['row']);
            $rangeCoordinates = $colCompanyInfoStart . $rowcount . ':' . $colCompanyInfoEnd . $rowcount;
            $rowData = $sheet->rangeToArray($rangeCoordinates, NULL, TRUE, FALSE);
                    echo "<tr>";

              $companyname=$worksheet->getCell($column.$row)->getValue();
            //  echo $companyname;

                foreach($rowData[0] as $result) 
                                                {

            echo "<td>".$result." </td>";

                            }


                    echo "</tr>";
            }
                echo "</table>";

            echo "<br />";
        echo    '<input type="submit" name="sub" value="Convert into PDF" />';
    //  echo '<input type="text" name="resName" value="$result">';

        function getdatan() 
{ 
   global $result; // declare as global
   return $result;    
} 


    echo '</form>';

this part is where I get company information... it looks like the table area shown down part...

enter image description here

I retrieve info and able to show as "$result" variable and with submit button named "convert it into pdf" I send it into other php page where I use TCPDF ....

Normally, this part of second page

 $pdf->SetFont('times', 'BI', 12);

    // add a page
    $pdf->AddPage('L', 'A4');

    if(isset($_POST['submit']))
            {
    $result = $_GET['resName'];
    $pdf->Write(20, $result, '', 0, 'C', true, 0, false, false, 0);
          }

          // set some text to print
    $txt = <<<EOD
    TCPDF Example 003
    Custom page header and footer are defined by extending the TCPDF class and overriding the Header() and Footer() methods.
    EOD;

    // print a block of text using Write()

   // $pdf->Write(20, $resultt, '', 0, 'C', true, 0, false, false, 0);
    // ---------------------------------------------------------
     ob_end_clean();
    //Close and output PDF document
    $pdf->Output('example.pdf', 'I');

However, I am unable to print "$result" in the second page... can you help me about how to print this table on pdf...

PS: please clarify your help...

2

There are 2 answers

0
gobo On BEST ANSWER

ok since I was on my own... I found solution by myself .... on the first page of PHP...

session_start();
//rest of your code and then...
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
                echo '<form action="final.php" method="POST">';
                $tablo="<br />";
                $tablo = $tablo."<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\" align='center'>";
                for ($rowcount = $rowCompanyInfoStart; $rowcount <= $rowCompanyInfoEnd; $rowcount++)
                { 
                    //$data = $objWorksheet->rangeToArray('A1:' . $maxCell['column'] . $maxCell['row']);
                    $rangeCoordinates = $colCompanyInfoStart . $rowcount . ':' . $colCompanyInfoEnd . $rowcount;
                    $rowData = $sheet->rangeToArray($rangeCoordinates, NULL, TRUE, FALSE);
                    //fazla bosluk olursa bunları aç ya da hucre bos mu kontrol et (cellExists ile) 
                    //rowData = array_map('array_filter', $rowData);
                    //$rowData = array_filter($rowData);    
                    $tablo= $tablo."<tr >";
                    $companyname=$worksheet->getCell($column.$row)->getValue();
                    //  echo $companyname;              
                    foreach($rowData[0] as $result) 
                    {
                        $tablo= $tablo. "<td>".$result. " </td>";
                    }
                    $tablo= $tablo. "</tr>";
                }
                $tablo= $tablo. "</table>";
                echo $tablo;
                echo "<br />";

                if($_SERVER['REQUEST_METHOD'] != 'POST') {
                    echo "SESSION Not AVAIL. first run.";
                }


                else{   
                    $_SESSION['varname'] = $tablo;
                    }

                echo    '<input type="submit" name="resName" value="Convert into PDF" />';

            }

so here I used both "POST request" instead of just "POST". It sends "REQUEST" without any "POST DATA"... and then for sending the data I created a a variable called "$table" I put all my rows and cell info into that and created a session so that second PHP can retrieve it....

ON the second PHP page I answer "request" and open "session" just like that

session_start();

//rest of your code and then...

   if($_SERVER['REQUEST_METHOD'] != 'POST') {
            echo "SESSION Not AVAIL. first run.";
            $tablo = $_SESSION['varname'];
            echo "SESSION now set.";
        }
        else{   
            $tablo = $_SESSION['varname'];
            //echo "SESSION SET, value: " .$_SESSION['varname']. " and ". $_SESSION['color'];
        }
        if (isset($_SESSION['varname'])){
            $tablo = $_SESSION['varname'];
            // print_r($_SESSION);
            //echo "Session Set: <br/>" . $tablo;
        }
        else{
            echo "Session not set. Tablo is empty";
        //  echo "Session Set: <br/>" . $tablo;
        }

so that's it!

1
Alex Coloma On

Try this

Change : echo '<input type="submit" name="sub" value="Convert into PDF" />';

To:

echo    '<input type="submit" name="resName" value="Convert into PDF" />';

EDIT

Put this at the begining of your second page:

var_dump($_POST); 

just to check what you are receiving from the first page.

Also change :

 $result = $_GET['resName'];

to this:

 $result = $_POST['resName'];