将一个变量从一页传递到另一页以生成 pdf

passing one variable from one page to another for pdf generation

我有一个页面,我曾使用 php...检索一些 excel 中写入的数据...通过使用 phpexcel...这部分提供我公司信息

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>';

这部分是我获取公司信息的地方...它看起来像下面显示的 table 区域...

我检索信息并能够显示为“$result”变量并使用名为 "convert it into pdf" 的提交按钮我将其发送到其他 php 我使用 TCPDF 的页面....

正常情况下,第二页的这一部分

 $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');

但是,我无法在第二页打印“$result”...你能帮我看看如何在 pdf 上打印这个 table...

PS: 请澄清你的帮助...

试试这个

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

收件人:

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

编辑

将此放在第二页的开头:

var_dump($_POST); 

只是为了检查您从第一页收到的内容。

同时更改:

 $result = $_GET['resName'];

对此:

 $result = $_POST['resName'];

好吧,因为我是一个人……我自己找到了解决方案…… 在 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" />';

            }

所以这里我同时使用了 "POST request" 而不是 "POST"。它发送 "REQUEST" 没有任何 "POST DATA"... 然后为了发送数据我创建了一个名为“$table”的变量我将所有行和单元格信息放入其中并创建了一个会话以便第二个 PHP 可以检索它....

在第二个 PHP 页我回答 "request" 然后打开 "session" 就像那样

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;
        }

就是这样!