Jump to content

mysql joining tables, sum(total+total) = grand total..


scarezekiel

Recommended Posts

im an amateur..please show me how to do this

 

 

this is my code

 




<?php require("html2fpdf.php"); 

$server = '';
$username = '';
$password = '';
$database_name='';

$dbconn = mysql_connect($server, $username,$password,false) or die("Could not establish connection");

mysql_select_db($database_name, $dbconn) or die ("Could not select database");

if (!$dbconn) {
    die('Something went wrong while connecting to MSSQL');
}


ob_start(); ?>




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
  <title></title>
  <h5 align="center"><U>confidential</U></h5>
  
</head>
<body>
<table style="text-align: left; width: 715px; height: 32px;"
border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td colspan="2" rowspan="1"
style="font-style: italic;">List-Field</td>
    </tr>
    <tr>
      <td style="font-style: italic;">Field No</td>
      <td style="font-style: italic;">Ex Field No</td>
      <td style="font-style: italic;">Type</td>
      <td style="font-style: italic;">Planting Year</td>
      <td style="font-style: italic;">Field Hectares</td>
      <td style="font-style: italic;">Reference</td>
      <td style="font-style: italic;">Location ID</td>
      <td style="font-style: italic;">Statement Year</td>
      <td style="font-style: italic;">Total Hectares</td>
      <br />      
    </tr>
    

<?
$query="SELECT * FROM tblfield";
        $result=mysql_query($query);
while($row=mysql_fetch_array($result)) { 
$fieldno = $row['fieldno'];
$exfieldno = $row['exfieldno'];
    $type = $row['type'];
    $plantingyear = $row['plantingyear'];
    $fieldhectares = $row['fieldhectares'];
    $reference = $row['reference'];
    $locationid = $row['locationid'];
    $statementyear = $row['statementyear'];
    $totalhectares = $row['totalhectares'];
echo "<tr><td>$fieldno</td>";
echo "<td>$exffieldno</td></tr>";
echo "<td>$type</td></tr>";
echo "<td>$plantingyear</td></tr>";
echo "<td>$fieldhectares</td></tr>";
echo "<td>$reference</td></tr>";
echo "<td>$locationid</td></tr>";
echo "<td>$statementyear</td></tr>";
echo "<td>$totalhectares</td></tr>";
}
?>


</tbody>
</table>
<table style="text-align: left; width: 715px; height: 32px;"
border="1" cellpadding="2" cellspacing="2">
</tbody>
<tr>
      <td colspan="2" rowspan="1"
style="font-style: italic;">List-Land Utilities</td>
    </tr>
    <tr>
      <td style="font-style: italic;">Description</td>
      <td style="font-style: italic;">Land Hectares</td>
      <td style="font-style: italic;">Statement Year</td>
      <td style="font-style: italic;">Location ID</td> 
      <td style="font-style: italic;">Total Hectares</td>     
    </tr>

<?
$query="SELECT * FROM tbllandutilities";
        $result=mysql_query($query);
while($row=mysql_fetch_array($result)) { 
$description = $row['description'];
$landhectares = $row['landhectares'];
    $statementyear = $row['statementyear'];
    $locationid = $row['locationid'];
    $totalhectares = $row['totalhectares'];  
echo "<tr><td>$description</td>";
echo "<td>$landhectares</td></tr>";
echo "<td>$statementyear</td></tr>";
echo "<td>$locationid</td></tr>";
echo "<td>$totalhectares</td></tr>";
}
?>
  </tbody>
</table>
<br>
</body>
</html>




<?php $var = ob_get_clean(); 


$pdf = new HTML2FPDF('P', 'mm', 'Letter'); 
		$pdf->AddPage(); 
		$pdf->WriteHTML($var); 
		$pdf->Output('test.pdf', 'I');

?>

 

 

i need to get both the totals from these two table..then get the grandtotal..

 

and perhaps check my codes i know they are messed up :)

 

PHOTO EXPLAINS ALL

 

post-131669-1348240358483_thumb.jpg

Link to comment
Share on other sites

Why you need totalhectares column in your table when you are not storing any data into it?

 

Here php solution for your problem

Declare two variable for totals and accumulate fieldhectares and landhectares values into these two variables while iterating through the rows

 

<?php require("html2fpdf.php"); 

$server = '';
$username = '';
$password = '';
$database_name='';

$dbconn = mysql_connect($server, $username,$password,false) or die("Could not establish connection");

mysql_select_db($database_name, $dbconn) or die ("Could not select database");

if (!$dbconn) {
    die('Something went wrong while connecting to MSSQL');
}


ob_start(); ?>




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
  <title></title>
  <h5 align="center"><U>confidential</U></h5>
  
</head>
<body>
<table style="text-align: left; width: 715px; height: 32px;"
border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td colspan="2" rowspan="1"
style="font-style: italic;">List-Field</td>
    </tr>
    <tr>
      <td style="font-style: italic;">Field No</td>
      <td style="font-style: italic;">Ex Field No</td>
      <td style="font-style: italic;">Type</td>
      <td style="font-style: italic;">Planting Year</td>
      <td style="font-style: italic;">Field Hectares</td>
      <td style="font-style: italic;">Reference</td>
      <td style="font-style: italic;">Location ID</td>
      <td style="font-style: italic;">Statement Year</td>
      <td style="font-style: italic;">Total Hectares</td>
      <br />      
    </tr>
    

<?
$total1=0;
$query="SELECT * FROM tblfield";
        $result=mysql_query($query);
while($row=mysql_fetch_array($result)) { 
$fieldno = $row['fieldno'];
$exfieldno = $row['exfieldno'];
    $type = $row['type'];
    $plantingyear = $row['plantingyear'];
    $fieldhectares = $row['fieldhectares'];
$total1=$total1+intval($fieldhectares);
    $reference = $row['reference'];
    $locationid = $row['locationid'];
    $statementyear = $row['statementyear'];
    $totalhectares = $row['totalhectares'];
echo "<tr><td>$fieldno</td>";
echo "<td>$exffieldno</td>";
echo "<td>$type</td>";
echo "<td>$plantingyear</td>";
echo "<td>$fieldhectares</td>";
echo "<td>$reference</td><";
echo "<td>$locationid</td>";
echo "<td>$statementyear</td>";
echo "<td>$totalhectares</td></tr>";
}
echo "<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>Total</td><td>$total1</td></tr>";
?>


</tbody>
</table>
<table style="text-align: left; width: 715px; height: 32px;"
border="1" cellpadding="2" cellspacing="2">
</tbody>
<tr>
      <td colspan="2" rowspan="1"
style="font-style: italic;">List-Land Utilities</td>
    </tr>
    <tr>
      <td style="font-style: italic;">Description</td>
      <td style="font-style: italic;">Land Hectares</td>
      <td style="font-style: italic;">Statement Year</td>
      <td style="font-style: italic;">Location ID</td> 
      <td style="font-style: italic;">Total Hectares</td>     
    </tr>

<?
$total2=0;
$query="SELECT * FROM tbllandutilities";
        $result=mysql_query($query);
while($row=mysql_fetch_array($result)) { 
$description = $row['description'];
$landhectares = $row['landhectares'];
$total2=$total2+intval($landhectares);
    $statementyear = $row['statementyear'];
    $locationid = $row['locationid'];
    $totalhectares = $row['totalhectares'];  
echo "<tr><td>$description</td>";
echo "<td>$landhectares</td></tr>";
echo "<td>$statementyear</td></tr>";
echo "<td>$locationid</td></tr>";
echo "<td>$totalhectares</td></tr>";
}
echo "<tr><td></td><td></td><td></td><td>Total</td><td>$total2</td></tr>";
echo "<tr><td>Grand Total</td><td>$total1 + $total2</td></tr>";
?>
  </tbody>
</table>
<br>
</body>
</html>




<?php $var = ob_get_clean(); 


$pdf = new HTML2FPDF('P', 'mm', 'Letter'); 
		$pdf->AddPage(); 
		$pdf->WriteHTML($var); 
		$pdf->Output('test.pdf', 'I');

?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.