hp
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "farm_house";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");

$myysql= "SELECT customerinformation.CustomerFullName, customerinformation.CustomerID, customerinformation.Housenumber, customerinformation.Sector, customerinformation.StreetNumber, customerconsumption.MilkDelieveredCow, milkrate.CowMilkRate, (customerconsumption.MilkDelieveredCow*milkrate.CowMilkRate) AS t_amount_cow, customerconsumption.MilkDeliveredBuffalow, milkrate.BuffaloMilkRate, (customerconsumption.MilkDeliveredBuffalow*milkrate.BuffaloMilkRate) AS t_amount_buff, customerconsumption.DeliveryDate, (customerconsumption.MilkDelieveredCow+customerconsumption.MilkDeliveredBuffalow) AS total_delivered
FROM ((customerinformation INNER JOIN customerconsumption ON customerinformation.CustomerID = customerconsumption.CustomerID) INNER JOIN customerpackage ON customerinformation.CustomerID = customerpackage.CustomerID) INNER JOIN milkrate ON customerpackage.RateID = milkrate.RateID LIMIT 1";

$result = mysql_query($myysql);
if(!$result)
{
    echo "could not successfully run query ($sql) from DB:".mysql_error();
    exit;
}
if(mysql_num_rows($result)==0)
{
    echo "No rows found,nothing to print";
    exit;
}
if(mysql_num_rows($result)!=0)
{
    //$rec=mysql_fetch_array($result);
    while ($rec = mysql_fetch_assoc($result)) {
    $CustomerFullName= $rec['CustomerFullName'];
    $CustomerID= $rec['CustomerID'];
    $Hosuenumber= $rec['Housenumber'];
    $Sector= $rec['Sector'];
    $StreetNumber=$rec['StreetNumber'];
    $MilkDelieveredcow= $rec['MilkDelieveredCow'];
    $CowMilkRate= $rec['CowMilkRate'];
    $t_amount_cow= $rec['t_amount_cow'];
    $MilkDelieveredBuffalow=$rec['MilkDeliveredBuffalow'];
    $BuffaloMilkRate= $rec['BuffaloMilkRate'];
    $t_amount_buff= $rec['t_amount_buff'];
    $DeliveryDate= $rec['DeliveryDate'];
    $total_delivered = $rec['total_delivered'];

    $returned_results[]=array('CustomerFullName'=>$rec['CustomerFullName'],'CustomerID'=>$rec['CustomerID'],'Housenumber'=>$rec['Housenumber'],'Sector'=>$rec['Sector'],'StreetNumber'=>$rec['StreetNumber'],'MilkDelieveredCow'=>$rec['MilkDelieveredCow'],'CowMilkRate'=>$rec['CowMilkRate'],'t_amount_cow'=>$rec['t_amount_cow'],'MilkDeliveredBuffalow'=>$rec['MilkDeliveredBuffalow'],'BuffaloMilkRate'=>$rec['BuffaloMilkRate'],'t_amount_buff'=>$rec['t_amount_buff'],'DeliveryDate'=>$rec['DeliveryDate'],'total_delivered'=>$rec['total_delivered'],);

    $query = "SELECT `MilkDelieveredCow`,`MilkDeliveredBuffalow`,`DeliveryDate` FROM `customerconsumption` WHERE `CustomerID`='".$CustomerID."' LIMIT 30";
    if($query_run = mysql_query($query)){

        if(mysql_num_rows($query_run)!=0){

            while ($rec2 = mysql_fetch_assoc($query_run)) {

                $returned_results2[]=array('MilkDelieveredCow'=>$rec2['MilkDelieveredCow'],'MilkDeliveredBuffalow'=>$rec2['MilkDeliveredBuffalow'],'DeliveryDate'=>$rec2['DeliveryDate']);

            }


        }


    }
    }}


    ?>
<!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=utf-8" />
<title>Invoice</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <p FONT align="left">Organish Puraa Milk, Islamabad
</p>
  <table width="242" border="1" cellspacing="2" cellpadding="10">
    <tr>
      <th width="214" scope="col"><h5>CUSTOMER MONTHLY INVOICE</h5></th>
    </tr>
  </table>
  <p  align="left"><u>CUSTOMER DETAILS</u></p>
  <p>NAME:
    <?php
  echo"$CustomerFullName";
  ?>
  </p>
  <p align="left">H.No:<?php echo"$Hosuenumber"; 
  ?>   St.No:<?php echo"$StreetNumber";?> </p>
  <p align="left">Sector:<?php echo"$Sector";?></p>
<p  align="left" ><U><strong>BILL DETAILS</strong></U>

    <table border="1" align="center">

        <tr>

            <td width="120">Milk Delivered</td>
            <td width="104">Date</td>
        </tr>

        <?php foreach($returned_results2 as $key){

         ?>

            <tr>

                <td><?php echo $key['MilkDelieveredCow']; ?></td>
                <td><?php echo $key['DeliveryDate']; ?></td>

            </tr>
        <?php
        } ?>



    </table>


</p>
<p  align="center" ></p>
  <div align="justify"></div>
  <p  align="left">Total Milk Delivered(Litres): Buffalo:<?php echo"$MilkDelieveredBuffalow"?>  Cow:<?php echo"$MilkDelieveredcow" ?></p>
<p align="left">Cost of Milk Delivered (Rs.) Cow::<?php echo "$t_amount_cow";?>  Buffalo:<?php echo"$t_amount_buff";?></p>
  <p align="left">Bill Payable for the month of June 2013: Rs.<?php echo"$";?>
    <input type="button"" value="Print" onClick="window.print()" />
  </p>
</form>
</body>
</html>

want to generate invoice for all the customers ,this code is generating invoice for one customer only,help!

Recommended Answers

All 2 Replies

Your query is designed to read the data only for one customer with the current $CustomerID. You have to change the query to read all (or selected set of) customers. But how would you display the invoices then (say for a few hundred of customers)??

looks like a bit aged question and u might have solved this. if not:

two suggestions first:
1) use "mysqli" in stead of "mysql". it is more secured and "improved"(i've read everywhere this suggestion) than mysql.
2) use "html 5". u just need to declare 'doctype' like this -

<!DOCTYPE html>

and now, i think u should try

while ($rec = mysqli_fetch_array($result) {
.................
}

&

while ($rec2 = mysql_fetch_assoc($query_run)) {
.................
}

fetch_assoc selects only one row.

and one thing more- its better to use

if(!$result)
{
    echo "could not successfully run query ($sql) from DB:".mysql_error();
    exit;
}
else if(mysql_num_rows($result)==0)
{
    echo "No rows found,nothing to print";
    exit;
}
else 
{
.............
}

rather using only 'if'

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.