限制页面以保存为PDF .. PHP

时间:2017-04-02 05:37:21

标签: php mysqli pdf-generation

美好的一天,

我为我的PHP页面启用了分页,它运行良好,即每页显示来自mysql的3个recor。但是当我点击另存为PDF按钮时,它会向我显示来自DB的所有记录而不仅仅是3.实际上,我并不知道其逻辑。我尝试使用与PHP中的分页相同的逻辑但是徒劳无功

以下代码

View.php

<?php 
error_reporting(E_ALL ^ E_DEPRECATED);
?>
<?php

//if($_POST['submit']){

if (isset($_POST['submit'])){
    require_once 'includes/practice.php';
 $pdf->SetFont('times', '', 11);
 $pdf->SetFont('aealarabiya', '', 11);
 $tb1 = '</br></br>


                            <table cellspacing="0" cellpadding="5" border="1"> 

                                <tr style="background-color:#f7f7f7;color:#333; ">

                                    <td width="60">First Name</td>

                                    <td width="60">Last Name</td>

                                      <td width="80">Employee Number</td>

                                      <td width="80">Department</td>

                                    <td width="60">Email</td>

                                    <td width="80">Total Printers</td>
                                     <td width="80">Total Scanners</td>
                                      <td width="60">Total PCs</td>
                                      <td width="60">Total Phones</td>
                                       <td width="60">Other Equipments</td>



                                </tr>

                            </table>';


include('connect.php');
$result1= $link->query("SELECT * FROM employees");

        while($row = mysqli_fetch_array($result1,MYSQLI_ASSOC)){
            $tb2 = '<table cellspacing="0" cellpadding="5" border="1"> 
                <tr> 
                    <td width="60">'.$row['first_name']. '</td>
                    <td width="60">'.$row['last_name'] . '</td>
                    <td width="80">'.$row['emp_number'] .'</td>
                    <td width="80">'.$row['department'].'</td>
                    <td width="60">'.$row['email'].'</td>
                    <td width="80">'.$row['total_printers'].'</td>
                    <td width="80">'.$row['total_scanners'].'</td>
                    <td width="60">'.$row['total_pc'].'</td>
                    <td width="60">'.$row['total_phones'].'</td>
                    <td width="60">'.$row['other_equips'].'</td>

                </tr>
            </table>';
            $tb1 = $tb1.$tb2;
        }


       $pdf->writeHTML($tb1, true, false, false, false, '');
       ob_end_clean();
       $pdf->Output('Report.pdf', 'D');

}

?>

Practice.php

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<?php

require_once('tcpdf.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);


// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_RIGHT);
//$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------
// set font

//$pdf->SetFont('helvetica', 'B', 20);
//$pdf->SetFont('andlso', '', 18);
$pdf->SetFont('dejavusans', '', 12);
$pdf->SetMargins(10, 30, 10, true);
//$pdf = new TCPDF('P','mm','A4');
// add a page
$pdf->AddPage();
//echo '<br /><br /><br /><br /><br />';
//$pdf->Write(0, '', '', 0, 'C', true, 0, false, false, 0);
//$your_margin=200;

$pdf->SetFont('times', '', 11);
//$this->SetTopMargin(200);
// -----------------------------------------------------------------------------


?>

PHP Page Pagination我正在做......

<?php
// connect to the database
//include('connect-db.php');
$mysqli = new mysqli("localhost", "root", "", "mydb");

// number of results to show per page
$per_page = 3;

// figure out the total pages in the database
if ($result = $mysqli->query("SELECT * FROM employees ORDER BY emp_id"))
{
if ($result->num_rows != 0)
{
$total_results = $result->num_rows;
// ceil() returns the next highest integer value by rounding up value if necessary
$total_pages = ceil($total_results / $per_page);

// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];

// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p><a href='view.php'>View All</a> | <b>View Page:</b> ";

for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo $i . " ";
}
else
{
echo "<a href='view-pagin.php?page=$i'>$i</a> ";
}
}
echo "</p>";

// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>First Name</th> <th>Last Name</th><th>Employee Number</th><th>Department</th><th>Email</th><th>Total Printers</th><th>Total Scanners</th><th>Total PCs</th><th>Total Telephones</th><th>Other Equipments</th></tr>";

// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }

// find specific row
$result->data_seek($i);
$row = $result->fetch_row();

// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row[1] . '</td>';
echo '<td>' . $row[2] . '</td>';
echo '<td>' . $row[3] . '</td>';
echo '<td>' . $row[4] . '</td>';
echo '<td>' . $row[5] . '</td>';
echo '<td>' . $row[6] . '</td>';
echo '<td>' . $row[7] . '</td>';
echo '<td>' . $row[8] . '</td>';
echo '<td>' . $row[9] . '</td>';
echo '<td>' . $row[10]. '</td>';
echo '<td class="forscreen"><a href="view-one.php?emp_id=' . $row[0] . '">View</a></td>';
echo '<td class="forscreen"><a href="edit.php?emp_id=' . $row[0] . '">Edit</a></td>';
echo '<td class="forscreen"><a href="delete.php?emp_id=' . $row[0] . '">Delete</a></td>';
echo '<td class="forscreen"><a href="view-aceessory.php?emp_number='.$row[3].'">View Accessories</a></td>';
//echo '<td class="forscreen"><a href="equipments.php?emp_number=' .$row[3].'">Add Accessories</a></td>';
echo '<td class="forscreen"><a href="equipments.php?emp_number=' .$row[3] .'&first_name='.$row[1] . '">Add Accessories</a></td>';
echo "</tr>";
}

// close table>
echo "</table>";
}
else
{
echo "No results to display!";
}
}
// error with the query
else
{
echo "Error: " . $mysqli->error;
}

// close database connection
$mysqli->close();

?>

保存为PDF我正在申请......

<?php
//if($_POST['submit']){

if (isset($_POST['submit'])){
require_once 'includes/practice.php';
 $pdf->SetFont('times', '', 11);
 $pdf->SetFont('aealarabiya', '', 11);
    //require_once 'includes/practice.php';
 //$pdf->SetFont('times', '', 11);
 // number of results to show per page
$rec_limit = 3;
if( isset($_GET{'page'} ) ) {
    $page = $_GET{'page'} + 1;
    $offset = $rec_limit * $page ;
 }else {
    $page = 0;
    $offset = 0;
 }


//$abpage=$this->Cell(0, 3, "Page " . $this->PageNo() . "/{nb}", 0, 0, "C");
  $tb1 = '</br></br> 


                            <table cellspacing="0" cellpadding="5" border="1"> 

                                <tr style="background-color:#f7f7f7;color:#333; ">

                                    <td width="80">Full Name</td>

                                    <td width="80">Employee Number</td>

                                      <td width="80">Department</td>

                                    <td width="60">Email</td>
                                    <td width="70">Total Printers</td>
                                     <td width="70">Total Scanners</td>
                                      <td width="60">Total PCs</td>
                                      <td width="60">Total Phones</td>
                                       <td width="60">Other Equipments</td>



                                </tr>

                            </table>';

//include('connect-db.php');
//$start=1;
include('connect.php');

$my_value=$_POST['my_value_of_page'];
$result = $link->query("SELECT * FROM employees LIMIT $my_value, 3");

//$result1= mysql_query("SELECT * FROM employees LIMIT $per_page");


       // while($row = mysql_fetch_assoc($result1)){
        while($row =mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            $tb2 = '<table cellspacing="0" cellpadding="5" border="1"> 
                <tr> 
                    <td width="80">'.$row['first_name'] . '&nbsp;&nbsp;'.$row['last_name'].'</td>
                    <td width="80">'.$row['emp_number'] .'</td>
                    <td width="80">'.$row['department'].'</td>
                    <td width="60">'.$row['email'].'</td>
                    <td width="70">'.$row['total_printers'].'</td>
                    <td width="70">'.$row['total_scanners'].'</td>
                    <td width="60">'.$row['total_pc'].'</td>
                    <td width="60">'.$row['total_phones'].'</td>
                    <td width="60">'.$row['other_equips'].'</td>

                </tr>
            </table>';
            $tb1 = $tb1.$tb2;
        } 

       $pdf->writeHTML($tb1, true, false, false, false, '');
       ob_end_clean();
       $pdf->Output('Report.pdf', 'D');

}

?>

当我对我的PDF使用此PHP页面分页过程时,它不起作用.. 我使用TCPDF将记录保存为PDF。 感谢

1 个答案:

答案 0 :(得分:0)

将所有评论放在一起:

<?php
/**** display-all-employees.php ***/
error_reporting(E_ALL);
ini_set('display_errors', 1);

// connect to the database
// number of results to show per page
$per_page = 3;

// figure out the total pages in the database
$result = $mysqli->query("SELECT * FROM employees ORDER BY emp_id"))

if ($result->num_rows != 0)
{
/* calculate and display pagination */
/** do stuff here to set $i **/
echo "<a href='view-page.php?page=$i'>$i</a> ";
}
else
{
echo "No results to display!";
} /* end query */

/**** jump to another page : display-all-employees.php -> view-page.php ***/

/********** view-page.php **********/

// connect to the database

// number of results to show per page
$rec_limit = 3;
$start = $_GET['page'];

/* check values here */
echo"[ $start / $rec_limit ]";

$result = $link->query("SELECT * FROM employees LIMIT $start, $rec_limit");

/* do stuff here */

echo "<a href='print-the-results-of-this-page-as-pdf.php?page=$start'>Print PDF with reulsts of this page #$start</a> ";

/**** jump to another page : view-page.php -> print-the-results-of-this-page-as-pdf.php ***/

/**** print-the-results-of-this-page-as-pdf.php ***/

error_reporting(E_ALL);
ini_set('display_errors', 1);

// connect to the database

$rec_limit = 3;
$start = $_GET['page'];

/* check value here */
echo"[ $start ]";

$result = $link->query("SELECT * FROM employees LIMIT $start, $rec_limit");

/* do stuff here with your PDF -> $pdf->Output('Report.pdf', 'D'); */

?>