hi. i have a problem in printing the report to printer. Now i can print the php reports, but it will open both printer dialog and page together. But I need only one to printer dialog box. Pls help me

Below my code
Connection database
`

<?php


  try {
        $hostname = "sqlserver2005";            //host
        $dbname = "dbdata";            //db name
        $username = "sa";            // username like 'sa'
        $pw = "pass5";                // password for the user
        $dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");
    } 

  catch (PDOException $e) 
    {
        echo "cannot find the server. pls check your administrator " . $e->getMessage() . "\n";
        file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
        exit;
    }

?>

`
Index file

<?php
include_once '../inc/connection.inc.php';
?>
<?php
try {
       $stmt = $dbh->prepare('SELECT * FROM StudentRecord order by Name');
       $stmt->execute();
    } 
catch (PDOException $e) 
    {
       $output = 'Error fetching authors from database!';
       include '../errormsg.php';
       exit();
    }

foreach ($stmt as $row) 
    {
       $sid[] = array('SID' => $row['SID'], 'Name' => $row['Name']);
    }

    include 'searchform.html.php';

?>

searchform.html.php

<?php
include '../templete/header.php';
?>
<Script Language="javascript">
function change_action()
    {
        var frm_obj=document.getElementById("frm");
        frm_obj.action="data.php";
    }
</Script>
<script language="javascript" type="text/javascript">


</script>

<table class="tbl_table" align="center">
<form action="" method="POST" id="frm" >
    <tr>
        <td class="td_title" colspan="2"><strong>Help Printing</strong></td>
    </tr>
                 <tr>
                  <td class="td_class_right">Student Name</td>
                  <td>
                      <select name="sid" id="sid"><option value="">Student ID</option>
                            <?php foreach ($sid as $sids): ?>
                                <option value="<?php htmlout($sids['SID']); ?>">
                                    <?php htmlout($sids['Name']);?>
                                    <?php htmlout($sids['SID']);?>

                                    </option>

                            <?php endforeach; ?>

                    </select>

                  </td>


        <tr>
        <td class="td_button" colspan="2">
            <input class="buttondesign"   type="submit" value="Search" name="search" onclick="change_action()">
            <input class="buttondesign"   type="submit" value="print" name="print" onclick="change_action()">
        </td>
    </tr>  
</form>
</table>

data.php

<?php
include_once '../inc/connection.inc.php';
?>
<?php
if (isset($_POST['search']) && $_POST['search']  != "" )
    {
        $sid = $_POST["sid"];
        $stmt = $dbh->query("SELECT * FROM vwStatementOpen WHERE SID='$sid' ORDER BY RCDate");  
        $stmt->setFetchMode(PDO::FETCH_ASSOC);  
        include 'view.html.php';

     }
 ?>

<?php

if (isset($_POST['print']) && $_POST['print']  != "" )
    {
        $sid = $_POST["sid"];
        $stmt = $dbh->query("SELECT * FROM vwStatementOpen WHERE SID='$sid' ORDER BY RCDate");  
        $stmt->setFetchMode(PDO::FETCH_ASSOC);  
        include 'print.html.php';

     }


?>

view.htm.php

    <?php
    include '../templete/header.php';
        $total=0;
        $scoursefee = 0;
        $sinipay = 0;
        $sdisc = 0;
        $scholar = 0;
        $nomonth = 0;
        $totalpaid=0;
        $balance=0;
        $totalother=0;
        $totaloverall=0;
    ?>

    <?php 

            $sid = $_POST["sid"];
            $s = $dbh->query("SELECT * FROM StudentRecord WHERE SID='$sid'");  
            $s->execute();
            while($row = $s->fetch())
                {
                  $ssid = $row['SID'];
                  $sname = $row['Name'];
                  $snric = $row['NRIC'];
                  $scoursecode = $row['CourseCode'];
                  $spaytype = $row['PayMode'];
                  $scoursefee = $row['CourseFee'];
                  $sinipay = $row['IniPayment'];
                  $sdisc = $row['Discount'];
                  $scholar = $row['ScholarShip'];
                  $nomonth = $row['NoMonths'];
                }

            $a = $dbh->query("SELECT SUM(Amount) as Amount FROM vwStatement WHERE SID='$sid'");  
            $a->execute();
            while($row = $a->fetch())
                {
                     $totalpaid=$row['Amount'];                
                }
                $balance=$scoursefee-$totalpaid;

            $b = $dbh->query("SELECT SUM(Amount) as Amount FROM vwStatementopen WHERE SID='$sid'");  
            $b->execute();
            while($row = $b->fetch())
                {
                     $totaloverall=$row['Amount'];                
                }
                $totalother=$totaloverall-$totalpaid
    ?>
    <div class="title">Statement</div>  
    <hr />
        <table align="center" cellpadding="1" cellspacing="1" style="width: 100%" >

            <tr>
                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Student ID</td>
                <td class="labeldesign_statement" style="width: 34px">:</td>
                <td class="labeldesign_statement"><?php echo htmlout($ssid);?></td>

                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Course Fee</td>
                <td class="labeldesign_statement" style="width: 27px">:</td>
                <td class="labeldesign_statement"><?php echo number_format($scoursefee,2);?></td>

                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Total Paid(Fee)</td>
                <td class="labeldesign_statement" style="width: 27px">:</td>
                <td class="labeldesign_statement"><?php echo number_format($totalpaid,2);?></td>            
            </tr>
            <tr>
                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Student Name</td>
                <td class="labeldesign_statement" style="width: 34px">:</td>    
                <td class="labeldesign_statement"><?php echo htmlout($sname);?></td>

                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Initial Payment</td>
                <td class="labeldesign_statement" style="width: 27px">:</td>
                <td class="labeldesign_statement"><?php echo number_format($sinipay,2);?></td>

                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Balance(Fee)</td>
                <td class="labeldesign_statement" style="width: 27px">:</td>
                <td class="labeldesign_statement"><?php echo number_format($balance,2);?></td>              

            </tr>
            <tr>
                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">NRIC/PP</td>
                <td class="labeldesign_statement" style="width: 34px">:</td>    
                <td class="labeldesign_statement"><?php echo htmlout($snric);?></td>

                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Discount</td>
                <td class="labeldesign_statement" style="width: 27px">:</td>
                <td class="labeldesign_statement"><?php echo number_format($sdisc,2);?></td>    

                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Total Paid(Others)</td>
                <td class="labeldesign_statement" style="width: 27px">:</td>
                <td class="labeldesign_statement"><?php echo number_format($totalother,2);?></td>                      
            </tr>    
            <tr>
                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Course</td>
                <td class="labeldesign_statement" style="width: 34px">:</td>    
                <td class="labeldesign_statement"><?php echo htmlout($scoursecode);?></td>

                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Scholarship</td>
                <td class="labeldesign_statement" style="width: 27px">:</td>
                <td class="labeldesign_statement"><?php echo number_format($scholar,2);?></td>   

                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Total Paid(Overall)</td>
                <td class="labeldesign_statement" style="width: 27px">:</td>
                <td class="labeldesign_statement"><?php echo number_format($totaloverall,2);?></td>                       
            </tr>    
            <tr>
                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">Pay Type</td>
                <td class="labeldesign_statement" style="width: 34px">:</td>    
                <td class="labeldesign_statement"><?php echo htmlout($spaytype);?></td>

                <td class="tdempty" style="width: 30px"></td>
                <td class="labeldesign_statement" style="width: 114px">No of Month</td>
                <td class="labeldesign_statement" style="width: 27px">:</td>
                <td class="labeldesign_statement"><?php echo number_format($nomonth);?></td>                  
            </tr>            
        </table>    




        <table width="100%" align="center" cellpadding="4" cellspacing="1">

            <tr>
                <td class="tdempty"></td>
                <td class="tbl_header">Receipt No</td>
                <td class="tbl_header">Receipt Date</td>
                <td class="tbl_header">Description</td>
                <td class="tbl_header">Mode</td>
                <td class="tbl_header">Cheque No</td>
                <td class="tbl_header_right">Amount</td>
                 <td class="tdempty"></td>
            </tr>
             <?php 

              if(isset($stmt))
                { while($row = $stmt->fetch())
                {?>

        <tr>
          <td class="tdempty"></td>  

          <td class="tbl_content"><?php echo $row['RCNo'];?></td>
          <td class="tbl_content"><?php echo date("d-m-Y", strtotime($row['RcDate']));?></td>
          <td class="tbl_content"><?php echo $row['Rcvdfor'];?></td>
          <td class="tbl_content"><?php echo $row['Mode'];?></td>
          <td class="tbl_content"><?php echo $row['ChqNo'];?></td>
          <td class="tbl_content_right"><?php echo number_format($row['Amount'],2) ;?></td>
          <td class="tdempty"></td>
         </tr>
            <?php
                $total+=$row['Amount'];
            }}?> 
    <tr><td colspan="9"><hr /></td></tr>

    <tr>  
        <td colspan="6"></td>
        <td class="tbl_subtotal_right">  <?php echo number_format($total,2);?></td>
    </tr>

    </table>

       <?php unset($dbh); unset($stmt); ?>

    <?php
    include '../templete/footer.php';
    ?>

print_head.php

    <?php 
        include '../inc/connection.inc.php';
        include_once '../inc/html_function.php'; 


            $comp = $dbh->query("SELECT * FROM CompanyInfo");  
            $comp->execute();
            while($row = $comp->fetch())
                {

                  $cname = $row['Name'];
                  $caddress = $row['Address'];
                  $ctel=$row['Tel'];
                  $cfax=$row['Fax'];
                  $cemail=$row['Email'];

                }

    ?>
    <!DOCTYPE html>
    <html lang="en">
    <!--<META HTTP-EQUIV="REFRESH" CONTENT="120">-->
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>HCK reporting Service-PHP</title>

        <link rel="shortcut icon" href="../image/102.ICO" />
        <link rel="stylesheet" type="text/css" href="../css/print.css">
         <!--<a href="http://www.silsnetwork.com"><img id="logo" src="../image/logo.jpg" alt="sils" /></a>-->
         <div class="title"><?php htmlout($cname)?></div>
         <div class="address"><?php htmlout($caddress)?></div>
         <div class="address">Tel:<?php htmlout($ctel)?>    Fax:<?php htmlout($cfax)?>   Email:<?php htmlout($cemail)?> </div>
         <div class="address"><hr /> </div>

        <div class="clickon_display">
        <!--<a href="javascript:window.print();">Printed on :- <?php echo date("M d, Y"); ?> </a>-->
        </div>
        <!--<a href="javascript:window.print();"><?php echo $title; ?> </a>-->
        <!--<a href="javascript:window.print();"><h6>Titel of reports</h6></a>-->
    <script language="Javascript1.2">
      function printpage() 
        {
            window.print();


        }
    </script>

    </head>
    <body onload="printpage()">

print.html.php

<?php
include '../templete/print_head.php';
    $total=0;
    $scoursefee = 0;
    $sinipay = 0;
    $sdisc = 0;
    $scholar = 0;
    $nomonth = 0;
    $totalpaid=0;
    $balance=0;
    $totalother=0;
    $totaloverall=0;
?>

<?php 

        $sid = $_POST["sid"];
        $s = $dbh->query("SELECT * FROM StudentRecord WHERE SID='$sid'");  
        $s->execute();
        while($row = $s->fetch())
            {
              $ssid = $row['SID'];
              $sname = $row['Name'];
              $snric = $row['NRIC'];
              $scoursecode = $row['CourseCode'];
              $spaytype = $row['PayMode'];
              $scoursefee = $row['CourseFee'];
              $sinipay = $row['IniPayment'];
              $sdisc = $row['Discount'];
              $scholar = $row['ScholarShip'];
              $nomonth = $row['NoMonths'];
            }

        $a = $dbh->query("SELECT SUM(Amount) as Amount FROM vwStatement WHERE SID='$sid'");  
        $a->execute();
        while($row = $a->fetch())
            {
                 $totalpaid=$row['Amount'];                
            }
            $balance=$scoursefee-$totalpaid;

        $b = $dbh->query("SELECT SUM(Amount) as Amount FROM vwStatementopen WHERE SID='$sid'");  
        $b->execute();
        while($row = $b->fetch())
            {
                 $totaloverall=$row['Amount'];                
            }
            $totalother=$totaloverall-$totalpaid
?>
<div class="title">Statement</div>  
<hr />
    <table align="center" cellpadding="1" cellspacing="1" style="width: 100%" >

        <tr>
            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Student ID</td>
            <td class="labeldesign_statement" style="width: 34px">:</td>
            <td class="labeldesign_statement"><?php echo htmlout($ssid);?></td>

            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Course Fee</td>
            <td class="labeldesign_statement" style="width: 27px">:</td>
            <td class="labeldesign_statement"><?php echo number_format($scoursefee,2);?></td>

            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Total Paid(Fee)</td>
            <td class="labeldesign_statement" style="width: 27px">:</td>
            <td class="labeldesign_statement"><?php echo number_format($totalpaid,2);?></td>            
        </tr>
        <tr>
            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Student Name</td>
            <td class="labeldesign_statement" style="width: 34px">:</td>    
            <td class="labeldesign_statement"><?php echo htmlout($sname);?></td>

            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Initial Payment</td>
            <td class="labeldesign_statement" style="width: 27px">:</td>
            <td class="labeldesign_statement"><?php echo number_format($sinipay,2);?></td>

            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Balance(Fee)</td>
            <td class="labeldesign_statement" style="width: 27px">:</td>
            <td class="labeldesign_statement"><?php echo number_format($balance,2);?></td>              

        </tr>
        <tr>
            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">NRIC/PP</td>
            <td class="labeldesign_statement" style="width: 34px">:</td>    
            <td class="labeldesign_statement"><?php echo htmlout($snric);?></td>

            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Discount</td>
            <td class="labeldesign_statement" style="width: 27px">:</td>
            <td class="labeldesign_statement"><?php echo number_format($sdisc,2);?></td>    

            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Total Paid(Others)</td>
            <td class="labeldesign_statement" style="width: 27px">:</td>
            <td class="labeldesign_statement"><?php echo number_format($totalother,2);?></td>                      
        </tr>    
        <tr>
            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Course</td>
            <td class="labeldesign_statement" style="width: 34px">:</td>    
            <td class="labeldesign_statement"><?php echo htmlout($scoursecode);?></td>

            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Scholarship</td>
            <td class="labeldesign_statement" style="width: 27px">:</td>
            <td class="labeldesign_statement"><?php echo number_format($scholar,2);?></td>   

            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Total Paid(Overall)</td>
            <td class="labeldesign_statement" style="width: 27px">:</td>
            <td class="labeldesign_statement"><?php echo number_format($totaloverall,2);?></td>                       
        </tr>    
        <tr>
            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">Pay Type</td>
            <td class="labeldesign_statement" style="width: 34px">:</td>    
            <td class="labeldesign_statement"><?php echo htmlout($spaytype);?></td>

            <td class="tdempty" style="width: 30px"></td>
            <td class="labeldesign_statement" style="width: 114px">No of Month</td>
            <td class="labeldesign_statement" style="width: 27px">:</td>
            <td class="labeldesign_statement"><?php echo number_format($nomonth);?></td>                  
        </tr>            
    </table>    




    <table width="100%" align="center" cellpadding="4" cellspacing="1">
            <tr><td colspan="7"><hr /></td></tr>
        <tr>
            <td class="tdempty"></td>
            <td class="tbl_header">Receipt No</td>
            <td class="tbl_header">Receipt Date</td>
            <td class="tbl_header">Description</td>
            <td class="tbl_header">Mode</td>
            <td class="tbl_header">Cheque No</td>
            <td class="tbl_header_right">Amount</td>
             <td class="tdempty"></td>
        </tr>
        <tr><td colspan="7"><hr /></td></tr>
         <?php 

          if(isset($stmt))
            { while($row = $stmt->fetch())
            {?>

    <tr>
      <td class="tdempty"></td>  

      <td class="tbl_content"><?php echo $row['RCNo'];?></td>
      <td class="tbl_content"><?php echo date("d-m-Y", strtotime($row['RcDate']));?></td>
      <td class="tbl_content"><?php echo $row['Rcvdfor'];?></td>
      <td class="tbl_content"><?php echo $row['Mode'];?></td>
      <td class="tbl_content"><?php echo $row['ChqNo'];?></td>
      <td class="tbl_content_right"><?php echo number_format($row['Amount'],2) ;?></td>
      <td class="tdempty"></td>
     </tr>
        <?php
            $total+=$row['Amount'];
        }}?> 
<tr><td colspan="7"><hr /></td></tr>

<tr>  
    <td colspan="6"></td>
    <td class="tbl_subtotal_right">  <?php echo number_format($total,2);?></td>
</tr>
<tr><td colspan="7"><hr /></td></tr>
</table>

   <?php unset($dbh); unset($stmt); ?>

Please Help me

Maideen

Member Avatar for LastMitch

hi. i have a problem in printing the report to printer. Now i can print the php reports, but it will open both printer dialog and page together. But I need only one to printer dialog box

I am very confused what you just mention. If you are connected to a printer then it should print the files which it does since you mention it works.

The issue you mention has nothing to do with the code provided.

It just that you have 2 printers connecting to the computer am I right?

This has nothing to do with the PHP code, this more like a networking issue rather than a code related.

You can set the setting on your network so you can print on that one printer from that computer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.