Jump to content

[SOLVED] column total


didgydont

Recommended Posts

hi all im trying to add the total of a column not the row

i had a look around in google and found how to add row and also found

http://www.kksou.com/php-gtk2/articles/add-a-total-row-to-the-end-of-GtkTreeView---Part-1.php

but could not folow or understand the code it errored out when i copied all the code i think it had some thing to do with not haveing Gtk

basicly all i want is the total of oweing for example

thank you for your time

example.jpg


                 <?php
$con = mysql_connect("localhost","*******","*******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("*********", $con);

include("toolbar.php");
// Begin your table outside of the array
echo "<table border=\"0\" cellpadding=\"5\" cellspacing=\"0\">
    <tr>
        <td><b>&nbsp Cost</b></td>
        <td><b>&nbsp Deposit</b></td>
        <td><b>&nbsp Owing</b></td>
        <td><b>&nbsp Ordered</b></td>
    </tr>";

// Define your colors for the alternating rows 

$color1 = "#F0F8FF";
$color2 = "#CCFFFF";
$row_count = 0; 

// Perform an statndard SQL query: 

$result = mysql_query("SELECT * FROM jobs
ORDER BY jobnumber");


// We are going to use the "$row" method for this query. This is just my preference. 

while($row = mysql_fetch_array($result)) {
    $customer = $row['clientid'];
    $cost = $row['cost'];
    $depos  = $row['deposit'];
    $oweing = ($cost - $depos);
    $result2 = mysql_query("SELECT * FROM clients WHERE Uid='$customer'");
    $row2 = mysql_fetch_array($result2);
    $status = $row['status'];
    $jobno = $row['jobnumber'];
    $custname = $row2['FirstName'] ." ". $row2['LastName'];
    $item = $row['item'];
    $orderd = $row['ordered'];
    $oweing2 = number_format($oweing,2);
    $cost2 = number_format($cost,2);
    $depos2 = number_format($depos,2);

    /* Now we do this small line which is basically going to tell  
    PHP to alternate the colors between the two colors we defined above. */ 

    $row_color = ($row_count % 2) ? $color1 : $color2;

    // Echo your table row and table data that you want to be looped over and over here. 

    echo "<tr>
    <td align=\"right\" bgcolor=\"$row_color\">&nbsp \$$cost2</td>
    <td align=\"right\" bgcolor=\"$row_color\">&nbsp \$$depos2</td>
    <td align=\"right\" bgcolor=\"$row_color\">&nbsp \$$oweing2</td>
    <td bgcolor=\"$row_color\">&nbsp $orderd</td>
    </tr>";

    // Add 1 to the row count 

    $row_count++; 
} 

// Close out your table. 

echo "</table>";
mysql_close($con);
?>
code]

Link to comment
https://forums.phpfreaks.com/topic/91357-solved-column-total/
Share on other sites

i ended up doing this

<?php
$con = mysql_connect("********","************","**********");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("*************", $con);

include("toolbar.php");
// Begin your table outside of the array
echo "<table border=\"0\" cellpadding=\"5\" cellspacing=\"0\">
    <tr>
        <td><b>&nbsp Cost</b></td>
        <td><b>&nbsp Deposit</b></td>
        <td><b>&nbsp Owing</b></td>
        <td><b>&nbsp Ordered</b></td>
    </tr>";

// Define your colors for the alternating rows 

$color1 = "#F0F8FF";
$color2 = "#CCFFFF";
$row_count = 0; 

// Perform an statndard SQL query: 

$result = mysql_query("SELECT * FROM jobs
ORDER BY jobnumber");


// We are going to use the "$row" method for this query. This is just my preference. 

while($row = mysql_fetch_array($result)) {
    $customer = $row['clientid'];
    $cost = $row['cost'];
    $depos  = $row['deposit'];
    $oweing = ($cost - $depos);
    $result2 = mysql_query("SELECT * FROM clients WHERE Uid='$customer'");
    $row2 = mysql_fetch_array($result2);
    $result3 = mysql_query("SELECT SUM(cost) FROM jobs");
    $row3 = mysql_fetch_array($result3);
    $result4 = mysql_query("SELECT SUM(deposit) FROM jobs");
    $row4 = mysql_fetch_array($result4);
    $status = $row['status'];
    $jobno = $row['jobnumber'];
    $custname = $row2['FirstName'] ." ". $row2['LastName'];
    $item = $row['item'];
    $orderd = $row['ordered'];
    $oweing2 = number_format($oweing,2);
    $cost2 = number_format($cost,2);
    $depos2 = number_format($depos,2);
    $totcost = $row3['SUM(cost)'];
    $totdepos = $row4['SUM(deposit)'];
    $totoweing = ($totcost - $totdepos);
    $totoweing2 = number_format($totoweing,2);
    $totcost2 = number_format($totcost,2);
    $totdepos2 = number_format($totdepos,2);


    /* Now we do this small line which is basically going to tell  
    PHP to alternate the colors between the two colors we defined above. */ 

    $row_color = ($row_count % 2) ? $color1 : $color2;

    // Echo your table row and table data that you want to be looped over and over here. 

    echo "<tr>
    <td align=\"right\" bgcolor=\"$row_color\">&nbsp \$$cost2</td>
    <td align=\"right\" bgcolor=\"$row_color\">&nbsp \$$depos2</td>
    <td align=\"right\" bgcolor=\"$row_color\">&nbsp \$$oweing2</td>
    <td bgcolor=\"$row_color\">&nbsp $orderd</td>
    </tr>";

    // Add 1 to the row count 

    $row_count++; 
}

// Close out your table.
// Make a MySQL Connection

//$query = "SELECT type, SUM(cost) FROM jobs";

//$result2 = mysql_query($query) or die(mysql_error());

// Print out result
//while($row = mysql_fetch_array($result)){
//echo "<tr><td>Total \".  \" = $\". $row['SUM(cost)']</td></tr> ";

//}
echo "</table>";

/*$result3 = mysql_query("SELECT SUM(cost) FROM jobs");
while($row3 = mysql_fetch_array($result3)) {
$totcost = $row3['SUM(cost)'];
}
$result4 = mysql_query("SELECT SUM(deposit) FROM jobs");
while($row4 = mysql_fetch_array($result4)) {
$totdepos = $row4['SUM(deposit)'];
} */
echo " <B>&nbsp$$totcost2  &nbsp$$totdepos2  &nbsp$$totoweing2 </B>";

mysql_close($con);

?>

Link to comment
https://forums.phpfreaks.com/topic/91357-solved-column-total/#findComment-468163
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.