Newbie ques - display subtotal lines

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
namtab
Forum Newbie
Posts: 3
Joined: Thu Nov 10, 2005 6:33 am

Newbie ques - display subtotal lines

Post by namtab »

twigletmac | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Hi 

Im i total newbie to php, and need help showing subtotals in my result set.  any help would be greatly appreciated.

I have a mssql stored procedure which has a table  with 
the following data:-

Code: Select all

Name    Date          Amount 
Jack    11/11/2005    500.00
Jack    12/12/2005    100.00
Jill    11/11/2005    200.00
Jill    11/11/2005    300.00
and when executed returns the data :-

Code: Select all

Name    Amount    11/11/2005    12/11/2005
Jack    500.00    500.00
Jack    100.00                  100.00 
Jill    200.00    200.00                       
Jill    300.00    300.00
I am now stumped has to how to insert subtotals into the result

Code: Select all

Name            Amount        11/11/2005        12/11/2005
Jack            500.00        500.00
Jack            100.00                          100.00 
Jack Total      600.00        500.00            100.00
Jill            200.00        200.00                       
Jill            300.00        300.00 
Jill Total      500.00        500.00
here is my code :-

Code: Select all

<html>
<body>
<?php

	// Connect directly to the MS SQL server via DB Library
	$con = mssql_connect ("********", "******", "********");
		
	if($con)
	{

		mssql_select_db ("[********]", $con);
			
		$sql= "exec sales_crosstab " ;
		$sql.= " '10/2005', ";
	        $sql.= " '12/2005' ";
				
		// Execute our query
		$rs= mssql_query ($sql, $con);

		if($rs)
		{
			$colCount = mssql_num_fields($rs);

			echo "<table>\n";
		
			echo "  <tr>\n";
			for($x = 0; $x < $colCount; $x++)
			{
				echo "<td><font size=4>" . mssql_field_name($rs, $x) . "</td>\n";
			}
            			
			echo "<tr><td></td></tr>";
			echo "<tr><td></td></tr>";

			$rowcount = mssql_num_rows($rs);
		}
						 
        	$col = " ";	
			while($row = mssql_fetch_row($rs))
			{
				
				$rowcount = mssql_num_rows($rs);
						   
				echo "  <tr>\n";
				for($x = 0; $x < $colCount; $x++)
					{
						echo "    <td>" . $row[$x] . "</td>\n";
						$col = $row[0];
					}
													
			  echo "  </tr>\n";
			}                 

			echo "</table>\n";
		
       
        echo "<br>";
		echo "Total: " . $totalval;

		mssql_close ($con);
	}

	;

	//foreach($GLOBALS as $n => $v)
	//{
	//	echo $n . " = " . $v . "<br>";
	//}
?>
</body>
</html>
twigletmac | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

twigletmac | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

in your do { } While statement why not keep a running total.

Code: Select all

$total = '0'

do {

//other code

$total = $total + (amount);

} while (condition)
then print the total at the end
namtab
Forum Newbie
Posts: 3
Joined: Thu Nov 10, 2005 6:33 am

Post by namtab »

Thx for the reply

wouldn't that just give me the grand total? Im sure I need to put the name in a variable and do a check where the value doesn't match the variable but I just can;t seem to get it to work.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Can you use

Code: Select all

and

Code: Select all

tags, please (both of you )?

How about something like this (which is essentially what Php3ch0 said)?

Code: Select all

$total = 0;

$result=mysql_query("SELECT * FROM ".$tp."table WHERE name='Jack' ORDER BY date");
$my_select ++;
while ( $a_row = mysql_fetch_object( $result ) )
{
    print "Name: Jack<br />";
    print "Date: ".$a_row->date."<br />";
    print "Date Total: ".$a_row->total."<br />";
    $total += $a_row->total;
    print "Running Total: ".$total."<br />";   
}
Post Reply