-1

I know how to give the command for print in PHP file but it prints the whole page.

I only need to print some text from that page, only See Image from my webpage.

This is how I echo the result:

<table id="myHeader" class="table table-striped table-hover table-bordered table-responsive"><thead>
            <tr>
                <th>Sr.</th>
                <th>Question</th>
                <th class="mobiile">Your Answer</th>
                <th>Right Answer</th>
                <th>Marks</th>
              </tr>
</thead><tbody>           
<?php   
    $i=0;
        while($lrow=$lstmt->fetch(PDO::FETCH_ASSOC))
        {
            extract($lrow);
            $i++;

?>
<tr>
<td><?php echo $i; ?></td>
<td><?php $qrn = $QRN; ?><?php 
$qilstmt = $user->runQuery("SELECT * FROM mcq WHERE QRN=:lrn ");
$qilstmt->bindparam(":lrn",$qrn);
$qilstmt->execute();
$qilr=$qilstmt->fetch(PDO::FETCH_ASSOC);
$qiilstmt = $user->runQuery("SELECT * FROM tf WHERE QRN=:lrn ");
$qiilstmt->bindparam(":lrn",$qrn);
$qiilstmt->execute();
$qiilr=$qiilstmt->fetch(PDO::FETCH_ASSOC); 
$qiiilstmt = $user->runQuery("SELECT * FROM fb WHERE QRN=:lrn ");
$qiiilstmt->bindparam(":lrn",$qrn);
$qiiilstmt->execute();
$qiiilr=$qiiilstmt->fetch(PDO::FETCH_ASSOC);
if($qilstmt->rowCount() == 1){
echo $qilr['Question']; 
}
elseif($qiilstmt->rowCount() == 1){
echo $qiilr['Question']."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(True OR False)"; 
}
 else { 
echo $qiiilr['Question'];  }?></td>
<td><?php 
if($Ans == ""){
    echo "No Answer";
}
else {echo $Ans;} ?></td>
<td><?php 
if($qilstmt->rowCount() == 1){
echo $rans = $qilr['RO']; 
}
elseif($qiilstmt->rowCount() == 1){
echo $rans = $qiilr['RO']; 
}
 else { 
echo $rans = $qiiilr['RO'];  }?></td>
<td><?php
if($rans == $Ans){
    $calc = $user->runQuery("SELECT * FROM students_records WHERE ARN=:lrn ");
$calc->bindparam(":lrn",$arn);
$calc->execute();
$calcRow=$calc->fetch(PDO::FETCH_ASSOC);
$fcalc = $calcRow['Marks'];
$lcalc = $fcalc + 1;
if($calcRow['Fixed'] == "N"){
$arecord = $user->runQuery("UPDATE students_records SET Marks=:user_arn WHERE ARN=:user_qrn");
$arecord->bindparam(":user_qrn",$arn);
$arecord->bindparam(":user_arn",$lcalc);
$arecord->execute();
}
    echo $mci = "1";
}
else {echo "0";}?></td></tr>
    <?php
    }
    $llcalc = "Y";
    $updatel = $user->runQuery("UPDATE students_records SET Fixed=:user_larn WHERE ARN=:user_lqrn");
$updatel->bindparam(":user_lqrn",$arn);
$updatel->bindparam(":user_larn",$llcalc);
$updatel->execute();
    ?>
<tr>
<td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td>
<td><strong>Total:</strong></td>
<td><strong><?php
$tcalc = $user->runQuery("SELECT * FROM students_records WHERE ARN=:lrn ");
$tcalc->bindparam(":lrn",$arn);
$tcalc->execute();
$tcalcRow=$tcalc->fetch(PDO::FETCH_ASSOC);
echo $total = $tcalcRow['Marks'];
?></strong></td>
</tr>
</tbody></table>
<?php
}
?>

So, I want to know what command should I give to print button, so that it prints the above code only and not the whole webpage....

Thanks in Advance

teeyo
  • 3,665
  • 3
  • 22
  • 37
MKJ
  • 133
  • 1
  • 1
  • 11

1 Answers1

1

As @CBroe said, this has nothing to do with PHP, you need to use CSS for this.

There are a lot of ways to make a CSS for print, IMHO the best way is to use HTML:

<link rel="stylesheet" type="text/css" href="print.css" media="print">

where media="print" is the part that tells the browser which CSS to use for printing.

In your print.css you can customize your page the way you want, for instance, if you don't want to display images in your printed page, just added it in there :

/* for example you don't need a navbar on a printed page. */
.navbar {
    display: none;
}

And by looking at your print button, it does not seem to work, add the following code to it :

<button class="btn btn-large btn-success" onClick="window.print()">Print</button>
teeyo
  • 3,665
  • 3
  • 22
  • 37
  • As what to print, is defined by `php` & `MySQL` so, `PHP` code in `css` file? – MKJ Nov 06 '17 at 09:19
  • No, you need to separate the content of your page, from what's going to be printed, I mean with print CSS your page style only changes on print, and you keep the good-looking page on website as it is. – teeyo Nov 06 '17 at 09:22
  • So, I should just add `` at my http://whspr.epizy.com/result.php ? – MKJ Nov 06 '17 at 09:23
  • Yes, and add the code for printing in your button (I edited my answer) and define how your page should appear in the print, I'll update my answer with a more concrete example – teeyo Nov 06 '17 at 09:26
  • Thanks, and how to remove `http://whspr.epizy.com/result.php?arn=ZM2017AS11D568` written in bottom-left corner? – MKJ Nov 06 '17 at 09:29
  • Date,Title also are visible, how to remove them? – MKJ Nov 06 '17 at 09:30
  • Those are added by the browsers, each browser add those data in some format, I'll look it up if there's a way to remove them – teeyo Nov 06 '17 at 09:34
  • Check these answers : https://stackoverflow.com/questions/1960939/disabling-browser-print-options-headers-footers-margins-from-page and https://stackoverflow.com/questions/585254/how-to-remove-the-url-from-the-printing-page – teeyo Nov 06 '17 at 09:36
  • Thanks a lot, and the last question, if my paper width is just 2-5 inch (thermal roll) then I have to set all my content within 2-5 inch of the `HTML` page, Am I **Right**? – MKJ Nov 06 '17 at 09:39
  • No, I think that's configured before printing, it's like printing settings, just like the page view: portrait or landscape! – teeyo Nov 06 '17 at 09:41