0

Good day,,

I have enabled pagination for my PHP page and it works good i.e. 3 recors from mysql per page showing. But when I click save as PDF BUTTON, it shows me all records from DB not the only 3. Actually, I don't know the logic for that. I tried the same logic I used for pagination in PHP but in vain

Code below

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 I am doing...

<?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();

?>

SAVE as PDF I am applying...

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

}

?>

When I use this PHP page pagination process for my PDF it does not work.. I am using TCPDF for saving records as PDF. Thanks

  • Your question isn't clear, unless you mean that the PDF generation needs to show only the 3 records your pagination displays. If that's the case, you need to modify the select to limit the query to the values in your pagination. – Sloan Thrasher Apr 02 '17 at 05:55
  • not clear : user can export to PDF the 3 results shown on a page using pagination ? if so, as mentionned above, $result1= $link->query("SELECT * FROM employees"); -> needs a LIMIT $i,3; and you need to pass the vars from page to page AND to the query – OldPadawan Apr 02 '17 at 06:29
  • @OldPadawan how will it work ... i am already doing pagination in PHP page but in pdf not getting result though – Mazhar Iqbal Rana Apr 02 '17 at 07:10
  • @OldPadawan have updated my question and mentioned how I am applying pagination in my PHP page... this I am applying for pdf but not working... – Mazhar Iqbal Rana Apr 02 '17 at 07:14
  • you show page=1 : means you want to show records 1;2;3. page=2 -> show 4,5,6. To achieve that, you need to put a LIMIT in you SQL query. You have $start and $end, use them to LIMIT your query such as SELECT * FROM employees LIMIT 0,3 or SELECT * FROM employees LIMIT where_you_want_to_start,limited_number_of_records. The page generating the PDF needs to get some vars $start and $end for the query – OldPadawan Apr 02 '17 at 07:29
  • @OldPadawan you mean I need to have the same process for PDF as am using for pagination on web page? or just start end vars for query by defining them – Mazhar Iqbal Rana Apr 02 '17 at 08:03
  • Page to generate PDF needs to know which records to process, therefore, it needs a LIMIT in the query. To make it more clear (I hope): listing with pagination -> SELECT * FROM employees LIMIT number_of_my_page -> then link to PDF for 3 employees records (ie: #6, #7 and #8)-> click and on print-pdf.php : SELECT * FROM employees LIMIT $start,3 (I would use this kind of workaround, modify it to match your needs according to your code) – OldPadawan Apr 02 '17 at 08:12
  • @OldPadawan I tried this but in vain... $page=3; $result = $link->query("SELECT * FROM employees LIMIT $page"); and as button definition,
    pls guide..
    – Mazhar Iqbal Rana Apr 02 '17 at 09:33
  • OK, say you have
    -> then, in view-paginpdf.php, use $my_value=$_POST['my_value_of_page'] -> SELECT * FROM employees LIMIT $my_value, 3; /* if you want 3 records starting from record number $my_value */ then generate PDF. More clear ?
    – OldPadawan Apr 02 '17 at 09:50
  • @OldPadawan now I have empty tbale showing.. I am doing this as per you said
    and in paginpdf page ....include('connect.php'); $my_value=$_POST['my_value_of_page']; $result = $link->query("SELECT * FROM employees LIMIT $my_value, 3"); But empty table with no data coming
    – Mazhar Iqbal Rana Apr 02 '17 at 10:05
  • have you any error message ? print your post/query/results, look at console. anything that gives a hint : empty, not executing... – OldPadawan Apr 02 '17 at 10:10
  • @OldPadawan This is the whole code. finally modified as per you said.. https://pastebin.com/0ZBNa6p0 pls check at this url... – Mazhar Iqbal Rana Apr 02 '17 at 10:14
  • all the code on the same page IRL ?! you printed as asked ? what does it show ? ie: line 47: $result = $link->query("SELECT * FROM employees LIMIT $my_value, 3"); line 53: while($row =mysqli_fetch_array($result1,MYSQLI_ASSOC)) { /* $result / $result1 ? */ – OldPadawan Apr 02 '17 at 10:21
  • @OldPadawan Console showing no error related to it – Mazhar Iqbal Rana Apr 03 '17 at 04:08
  • @OldPadawan it is coming fine now but for the first page only... when I select page 2, it still brings records of first three rows as it should bring 4 5 6 – Mazhar Iqbal Rana Apr 03 '17 at 04:18
  • @OldPadawan Now used this approach...$sql = "SELECT count(emp_id) FROM employees "; $retval = mysqli_query($link,$sql ); $row = mysqli_fetch_array($retval, MYSQLI_NUM); $rec_count = $row[0]; $left_rec = $rec_count - ($page * $rec_limit); $result = $link->query("SELECT * FROM employees LIMIT $offset, $rec_limit"); but still getting ist three recs only... – Mazhar Iqbal Rana Apr 03 '17 at 05:43
  • can you please post the latest updated code, and, if running on multiple pages, split the pages ? – OldPadawan Apr 03 '17 at 07:13
  • @OldPadawan pls see above on same thread... This heading and code SAVE as PDF I am applying... is latest one – Mazhar Iqbal Rana Apr 03 '17 at 07:36
  • ok, I see a couple of things that has changed, but in order to help (if I still can...) I need to get the logic behind your code -> in the processing order I mean. (because, here, I can't check the values you pass through pages : are they ok when you echo your vars ?) 1/ on a page, you list employees by group of 10 -> 2/ a link / form (where is it ?) triggers the PDF process 3/ you can't get the right values to start/end and limit the SQL query. Right now, I'm lost in your code... – OldPadawan Apr 03 '17 at 08:01
  • @OldPadawan OK have modified and updated as per the logic you said last day.... see the last update for save as pdf above. It is bringing correct ist three results from DB but when I select page 2, it still brings ist three recs... – Mazhar Iqbal Rana Apr 03 '17 at 08:18
  • please check https://pastebin.com/w6pAPJCX and please echo/print all $vars and queries so we have more clues :) – OldPadawan Apr 03 '17 at 10:10
  • @OldPadawan I dint get what is going on.. I run your pastebin code but shows empty page at view-page... – Mazhar Iqbal Rana Apr 04 '17 at 04:58
  • @OldPadawan So pasted as per your instructions and getting empty table with no results Code here https://pastebin.com/2mgc6Kzb – Mazhar Iqbal Rana Apr 04 '17 at 05:19
  • of course you get empty result :) I gave you *only portions* of code, with comments, so you can tell me if I get the proper logic of what your organization is. Then, you shall complete the empty portions by inserting, one step at a time, another piece of code, print results, and see if this work or not. Only way we find out what's wrong. And please, make 3 pages, not one, it'll make things easier to understand, and you can stick all togheter back again after – OldPadawan Apr 04 '17 at 06:45
  • @OldPadawan no no i mean I filled empty portions and result is empty table.... – Mazhar Iqbal Rana Apr 04 '17 at 07:16
  • @OldPadawan ok fine i did it. three files i made and ist two are working like a charm and showing 3 rows data for page 1 and next three for page 2 but when i go at last one print pdf results.. it gives table columns but no data in rows.. – Mazhar Iqbal Rana Apr 04 '17 at 07:31
  • @OldPadawan This is the code of last file i.e. print-the-results-of-this-page-as-pdf.. on below link https://pastebin.com/6cWW7UCf – Mazhar Iqbal Rana Apr 04 '17 at 07:34
  • good point :) then, on last page, you just put a form "print this page", right ? but then, you need to pass $var to self-page ->
    " method="POST"> because for what I see, you don't have a value for 'page' anymore, therefore, query will be empty/false. Also, think about using prepared statements and printing query errors, it helps a lot :)
    – OldPadawan Apr 04 '17 at 07:42
  • @OldPadawan I added this line as u said ..
    " method="POST">... It says Notice: Undefined variable: page
    – Mazhar Iqbal Rana Apr 04 '17 at 07:50
  • @OldPadawan no at the last page in the form i have button save as pdf.... – Mazhar Iqbal Rana Apr 04 '17 at 07:52
  • because you have to set $page = $_GET['page']; at the very top of the page, it comes from previous page. Then, use it in form, then in query. Don't forget to echo 'page' and 'start' at each time and see... – OldPadawan Apr 04 '17 at 07:53
  • @OldPadawan Its done ... :D Thanks a lot – Mazhar Iqbal Rana Apr 04 '17 at 07:54
  • cool :) please don't forget to +1 useful answers. I'll try to post a clear answer grouping all answers so you can accept it – OldPadawan Apr 04 '17 at 07:57
  • @OldPadawan I wish I could give you rank/accepted score here but in comments cant give :) – Mazhar Iqbal Rana Apr 04 '17 at 08:01
  • @OldPadawan I was saying same .. pls do I would surely do it :) – Mazhar Iqbal Rana Apr 04 '17 at 08:02
  • just accept answer then :) ty – OldPadawan Apr 04 '17 at 08:03
  • @OldPadawan have accepted but cant make it as +1 maybe due to my repute :D – Mazhar Iqbal Rana Apr 04 '17 at 08:19
  • TY, and good code for the future ;) – OldPadawan Apr 04 '17 at 08:23
  • @OldPadawan TY :D – Mazhar Iqbal Rana Apr 04 '17 at 09:17
  • @OldPadawan can you please answer me at.. http://stackoverflow.com/questions/43222546/import-from-csv-in-php-mysql-shows-characterses-like – Mazhar Iqbal Rana Apr 05 '17 at 05:31

1 Answers1

0

To put all comments together :

<?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'); */

?>
OldPadawan
  • 1,247
  • 3
  • 16
  • 25