We help IT Professionals succeed at work.
Get Started

send the output of the script as an  mail

asaidi
asaidi asked
on
633 Views
Last Modified: 2012-08-29
Hi Experts
i have a script that print to the screen or html output reports please see the picture ..
and i have a table with for each customer has his email
after each end of customer in my while loop i want to mail it to the email address i have in my data
<?php
include('db.php');
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=windows-1252' />
<title>CAMPION The Power Behind The Water</title>
<?php
  if(preg_match('/(?i)msie [1-9]/',$_SERVER['HTTP_USER_AGENT'])) {
    echo " <link href='ie.css' rel='stylesheet' type='text/css' /> ";
  } else {
   echo "<link href='fire1.css' rel='stylesheet' type='text/css' />";
  }
?>

</head>
<body>
<?php
$start = date("Y-m-1", strtotime("last month"));
$end = date("Y-m-t", strtotime("last month"));  
$sql="SELECT *
     FROM count_transactions,automatic_report  
             WHERE DATE(count_transactions.tran_date) BETWEEN '$start' AND '$end' and count_transactions.account_no=automatic_report.account 
  and count_transactions.customer_no=automatic_report.customer
                AND automatic_report.periode=2  
                    GROUP BY count_transactions.customer_no order by count_transactions.account_no";  
 
           
          $res = mysql_query($sql) or die("FAIL: $sql<br>" . mysql_error());
         $num = mysql_num_rows($result1);
          while($rows = mysql_fetch_array($res)) { 
              $tot1=0;
              $tot22=0;
              $tot33=0;
              $tot44=0;
              $general1=0;
              $dateb =$rows['tran_date'];
              $dateb1=date('d-m-Y',strtotime($dateb));             
              $i=0; 
              $netw=$rows['unit_serial'];
              $client=$rows['customer_no'];
              $auto=$rows['account_no'];
              $channel=$rows['pulse_channel'];
?>
<table id ='table4'><tr>
<?php
 
$q1="SELECT unitName FROM net_watch_units WHERE serial_no=$netw";
                   $res1=mysql_query($q1);
                   $r1=mysql_fetch_array($res1);
                   $account=$r1['unitName']; 
$q2="SELECT account_name FROM account_details WHERE auto_id=$auto";
                   $res2=mysql_query($q2);
                   $r2=mysql_fetch_array($res2);
                   $name=$r2['account_name'];  

$q3="SELECT customer_name FROM customer_details WHERE auto_id=$client";
                   $res3=mysql_query($q3);
                   $r3=mysql_fetch_array($res3);
                   $name1=$r3['customer_name'];  

?> 
<?php
$query1="select account_ref,serial_no,pulser1_name,pulser2_name,pulser3_name,pulser4_name from net_watch_units
         where serial_no=$netw ";
$result2=mysql_query($query1);
$record2=mysql_fetch_array($result2);
$pulser1=$record2['pulser1_name'];  
$pulser2=$record2['pulser2_name'];
$pulser3=$record2['pulser3_name'];
$pulser4=$record2['pulser4_name'];
if(empty($pulser1) and empty($pulser2) and empty($pulser3) and empty($pulser4)){

?> 
  <input type="button"  onclick="history.go(-1);" value="No Channels please go Back">
<?php  
 exit; 
} 
if($pulse[0]=='1' && $pulse[1]=='2' && $pulse[2]=='3' && $pulse[3]=='4'){
   $chane1=$pulser1; 
   $chane2=$pulser2; 
   $chane3=$pulser3; 
   $chane4=$pulser4;  

} 
if($pulse[0]=='1' && $pulse[1]=='' && $pulse[2]=='' && $pulse[3]==''){
    
   $pulser2=''; 
   $pulser3=''; 
   $pulser4='';  

}  
if($pulse[0]=='' && $pulse[1]=='2' && $pulse[2]=='' && $pulse[3]==''){
   $pulser1=''; 
    
   $pulser3=''; 
   
   $pulser4='';  

}  
if($pulse[0]=='' && $pulse[1]=='' && $pulse[2]=='3' && $pulse[3]==''){
   $pulser1=''; 
   $pulser2='';
   $pulser4='';  
    
}  
if($pulse[0]=='' && $pulse[1]=='' && $pulse[2]=='' && $pulse[3]=='4'){
   $pulser1=''; 
   $pulser2='';
   $pulser3=''; 
   

}  
?>    
<td rowspan="2" style="width:30%"><img src='campion.png' alt='The Power Behind The Water'/></td>
<td colspan="2" style=" width:20%;font-size:14px;">Unit Name : <b><br><?php echo $account;?></b></td> 
<td colspan="2" style=" width:20%;font-size:14px;">Account Name : <b><br><?php echo $name;?></b></td> 
<td colspan="2" style=" width:15%;font-size:14px;">Customer Name : <b><br><?php echo $name1;?></b></td></tr>

<td style="font-size:12px;width:10%;">Report Date : <b><br><?php echo $dateb1;?></b></td> 
<?php
         if($report=='1'){
            $rep='Litres';
         }
         if($report=='2'){
            $rep='M3';
         }
         if($report=='3'){
            $rep='Gallons (Imp)';
         }
    
         ?>
<td style="width:10%;font-size:14px;">Report Units : <b><br><?php echo $rep;?></b></td><td colspan="3" style="font-size:14px;">Report Name : <b><br> Daily Totals Usage Report</b></td> 
</tr>
</table>

<table class='table2'> 
  <thead>
<tr>
      <th id="fr" scope="col">Date</th>   
     
      <th id="fn" scope="col"><?php echo $pulser1;?></th>  
   
         <th id="fn" scope="col"><?php echo $pulser2;?></th>  
       
        <th id="fn" scope="col"><?php echo $pulser3;?></th>  
       
         <th id="fn" scope="col"><?php echo $pulser4;?></th>  
        
       <th id="fr" scope="col">Total</th>  
    </tr>
  </thead> 
  <?php
 
 
         if($report=='1'){
            $rep='Litres';
         }
         if($report=='2'){
            $rep='M3';
         }
         if($report=='3'){
            $rep='Gallons (Imp)';
         }
$pdf="SELECT DATE(count_transactions.tran_date) as datei,
      SUM(case when count_transactions.pulse_channel = 1 then unit_qty else 0 end) as name1,
      SUM(case when count_transactions.pulse_channel = 2 then unit_qty else 0 end) as name2,
      SUM(case when count_transactions.pulse_channel = 3 then unit_qty else 0 end) as name3,
      SUM(case when count_transactions.pulse_channel = 4 then unit_qty else 0 end) as name4,
      SUM(count_transactions.unit_qty) as Total
from count_transactions,automatic_report  
WHERE  DATE(count_transactions.tran_date) BETWEEN '$start' AND '$end'   
  and count_transactions.account_no=automatic_report.account 
  and count_transactions.customer_no=automatic_report.customer
                AND automatic_report.periode=2 and count_transactions.unit_serial=$netw                                 
GROUP BY DATE(count_transactions.tran_date)";
$result=mysql_query($pdf) or die('Could not connect: ' . mysql_error());
$num=mysql_num_rows($result);
while($row= mysql_fetch_array($result)){
    $jour = $row['datei'];
    $jour=date('d-m-Y',strtotime($jour));
    $total = $row['Total'];
    $name1 = $row['name1'];
    $name2 = $row['name2'];
    $name3 = $row['name3'];
    $name4 = $row['name4']; 
if($report==2){
             $name1=$name1/1000;
             $name2=$name2/1000;
             $name3=$name3/1000;
             $name4=$name4/1000;
             $total=$total/1000; 
          }
          elseif($report==3){
              $name1=$name1*0.2199;
              $name2=$name2*0.2199;
              $name3=$name3*0.2199;
              $name4=$name4*0.2199;
              $total=$total*0.2199;   
          }
          else{
            $name1=$name1*1;
            $name2=$name2*1;
            $name3=$name3*1;
            $name4=$name4*1;
            $total=$total*1; 
          }
$namef1=str_pad(number_format($name1,2,'.',','),$imax, $pad,  STR_PAD_LEFT);
$namef2=str_pad(number_format($name2,2,'.',','),$imax, $pad,  STR_PAD_LEFT);
$namef3=str_pad(number_format($name3,2,'.',','),$imax, $pad,  STR_PAD_LEFT);
$namef4=str_pad(number_format($name4,2,'.',','),$imax, $pad,  STR_PAD_LEFT);
$totalf=str_pad(number_format($total,2,'.',','),$imax, $pad,  STR_PAD_LEFT); 
$tot1=$tot1+$name1; 
$tot22=$tot22+$name2;
$tot33=$tot33+$name3;
$tot44=$tot44+$name4; 
$general1=$general1+$total;
$somme1=str_pad(number_format($tot1),$imax,  $pad,  STR_PAD_LEFT); 
$somme2=str_pad(number_format($tot22),$imax, $pad,  STR_PAD_LEFT); 
$somme3=str_pad(number_format($tot33),$imax, $pad,  STR_PAD_LEFT); 
$somme4=str_pad(number_format($tot44),$imax, $pad,  STR_PAD_LEFT); 
$general=str_pad(number_format($general1,2,'.',','),$imax, $pad,  STR_PAD_LEFT);
if(empty($pulser1)){
   $somme1='';   
   $namef1='';
    
}
if(empty($pulser2)){
   $somme2='';   
    $namef2='';
    
}
if(empty($pulser3)){
   $somme3='';   
    $namef3='';
    

}
if(empty($pulser4)){
    $somme4='';   
    $namef4='';
    $name4=0;
   
}
$i++;
echo"<tr>"; 
       	echo"<td>$jour</td><td>$namef1</td><td>$namef2</td><td>$namef3</td><td>$namef4</td><td>$totalf</td>";
}        


 
  $av1=round($tot1/($i),2); 
  $av2=round($tot22/($i),2);
  $av3=round($tot33/($i),2); 
  $av4=round($tot44/($i),2);  
  $avg=$av1+$av2+$av3+$av4;
  $av1=str_pad(number_format($av1,2),$imax, $pad,  STR_PAD_LEFT);
  $av2=str_pad(number_format($av2,2),$imax, $pad,  STR_PAD_LEFT);
  $av3=str_pad(number_format($av3,2),$imax, $pad,  STR_PAD_LEFT);
  $av4=str_pad(number_format($av4,2),$imax, $pad,  STR_PAD_LEFT);
  
  $avg1=str_pad(number_format($avg,2),$imax, $pad,  STR_PAD_LEFT); 


?>

<tr>
<td id='td1'>Total </td>  
<td id='td1'><?php echo $somme1;?></td> 
<td id='td1'><?php echo $somme2;?></td>
<td id='td1'><?php echo $somme3;?></td>
<td id='td1'><?php echo $somme4;?></td>
<td id='td1'><?php echo $general;?></td></tr>
<?php
if(empty($pulser1)){
   $av1='';   
   
}
if(empty($pulser2)){
   $av2='';   
   
}
if(empty($pulser3)){
   $av3='';   
   
}
if(empty($pulser4)){
   $av4='';   
   
}
?>
<tr>
<td id='td1'>Daily Average </td> 
<td id='td1'><?php echo $av1;?></td> 
<td id='td1'><?php echo $av2;?></td> 
<td id='td1'><?php echo $av3;?></td> 
<td id='td1'><?php echo $av4;?></td> 
<td id='td1'><?php echo $avg1;?></td> 
</tr></table> 
 

<p><img src='litre.php?start=<?php echo $start?>&end=<?php echo $end?>&netw=<?php echo $netw?>&auto=<?php echo $auto?>'/></p> 
</tr></table> 
<?php 
} 
if($num>0){

?>
<input type="button" id="BTNPRINT" value="Print" onClick="window.print();"> 
<input type="button" id="BTNPRINT"onclick="window.location.href='export.php?date=<?php echo $date?>&date2=<?php echo $date2?>&netw=<?php echo $netw?>'" value='Export'>
<?php

}
else{
echo "<h2> No Data Found...</h2>"; 
echo "<br>";

?>
<input type="button"  onclick="history.go(-1);" value="Back">
<?php
}

$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
</body>
</html>    

Open in new window

ex.jpg
Comment
Watch Question
Senior Software Engineer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 3 Answers and 34 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE