Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Total from Variable amount of Variables Loaded from Database
Old 01-20-2011, 05:14 PM Total from Variable amount of Variables Loaded from Database
Experienced Talker

Posts: 40
Name: Andrew Benson
Trades: 0
I am making a payroll software that calculates the amount the employee should be paid based of certain variables. The variables and rates are loaded from the database in the first script and generates the html input for the person to fill out how much they did. The variables (called vars or units in the script), are just how many that person did. The rate is what they get paid for one of those units. So for each var the total would be unit * the correlating rate. I need to add all the totals of each of these vars to make $subTotal. How do i add the totals in the while loop?
I hope everyone understands my terminology and thanks in advance.

PHP Code:
<?php require("libraries/auth/membersarea.php"?>
<h2>Work Time</h2>

<?php
$sql 
"SELECT * FROM vars  WHERE active = '1' AND adminOnly = '0' ORDER BY ID ASC";

$result mysql_query($sql);

if (!
$result) {
    echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
    exit;
}

if (
mysql_num_rows($result) == 0) {
    echo 
"No Published Variables Found!";
    include(
"template/footer.php");   
    exit;
}
    echo 
"<form action=\"newpayment_process.php\" method=\"post\">";
while(
$row mysql_fetch_array($result))
    {
    echo 
"<table id=\"line".$row['ID']."\" align=\"center\" border=\"0\" style=\"width: 40%; font-size:14px;\">\r\n<tr>\r\n";
    echo 
"<td style=\"width: 30%;\"><b>".$row['varName']."</b></td>";
    echo 
"<td style=\"width: 18%;\"><input type=\"text\" name=\"".$row['ID']."\" /></td>\r\n";
    echo 
"</tr>\r\n";
    echo 
"</table>\r\n";
    }
    
?>
<br />
<input type="submit" value="Submit" align="center" />
</form>
<?php include("template/footer.php"?>
PHP Code:
<?php
// Start Session
session_start();

// Establish Authentication System
require("libraries/auth/membersarea.php");

// Establish Connection to Database
require("libraries/db/dbconnect.php");

//Begin 
    
    
$username $_COOKIE['ID_my_site'];
    
$PIDhigh mysql_result(mysql_query("select max(PID) from payroll"), 0);
    
//"Manual" Auto-incrementing of PID 
    
$PID $PIDhigh 1;
    
$userID mysql_result(mysql_query("select ID from users where username = '$username'"), 0);
    
$user_Name mysql_result(mysql_query("select name from users where username = '$username'"), 0);
    
    
mysql_query("insert into payroll (PID, ID, username) 
    values ('"
.$PID."', '$userID', '$username')") or die(mysql_error());


$sql "SELECT * FROM vars  WHERE active = '1' AND adminOnly = '0' ORDER BY ID DESC";

$result mysql_query($sql);

if (!
$result) {
    echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
    exit;
}

if (
mysql_num_rows($result) == 0) {
    echo 
"No Published Variables Found!";
    include(
"template/footer.php");   
    exit;
}

    
while(
$row mysql_fetch_array($result))
    {
    
//ID from vars
    
$id $row['ID'];
    
    
//ID's value
    
$value $_POST[$id];
    
    
//Generate field names for payroll table
    
$field mysql_result(mysql_query("select varAlias from vars where ID = '".$id."'"), 0);
    
$fieldRateText $field "_rate";
    
    
//Get rates
    
$fieldRate mysql_result(mysql_query("select rate from current_rates where vID = '".$id."'"), 0);
    
    
//Dump into MySQL
    
mysql_query("update payroll set ".$field." = '".$value."' where PID = '".$PID."'") or die(mysql_error());
    
mysql_query("update payroll set ".$fieldRateText." = '".$fieldRate."' where PID = '".$PID."'") or die(mysql_error());
    

    
//$varTotal is the total for that var
    
$varTotal $value $fieldRate;
    
    
//$subTotal is the total before taxes
    //Generate $subTotal somehow?
    //$total is the total after taxes

    
}

// To be implemented
//Email to administrators that payroll request has been filed
    
$adminEmail mysql_result(mysql_query("select email from users where siteNotify = '1'"), 0);
    
$adminName mysql_result(mysql_query("select name from users where siteNotify = '1'"), 0);
    
$emailMessage "Hello ".$adminName.",\n A payroll request has been submitted by ".$user_Name." and is pending your approval.";
    
$emailSubject "RAI Payroll System - Pending Payroll Submission";
    
$emailFrom "noreply@djabnetworks.com";
    
$emailHeader "From: " $emailFrom;
    
mail($adminEmail,$emailSubject,$emailMessage,$emailHeader);


//Success message
    
echo "Your payroll request has been submitted. ".$adminName." has been notified and will confirm your request before it is available on your payroll.";






//End

// Include footer
include("template/footer.php");

?>
djab is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-20-2011, 07:51 PM Re: Total from Variable amount of Variables Loaded from Database
Marik's Avatar
Skilled Talker

Posts: 99
Trades: 0
Imagine your database looks like this:

id | price | rate

1 | 5 | 2
2 | 3 | 3

With the following code you can get the subtotal:

PHP Code:
$query mysql_query(" SELECT * FROM test_table ");
while (
$row mysql_fetch_array($query)) {
    
$total $row['price'] * $row['rate'];
    
$subtotal += $total// will output 19
}

echo 
$subtotal
Basially all you will be doing is creating a variable called $subtotal and with each iteration of the while loop you will be adding to (and not replacing) the previous set value:

Code:
+=
__________________

Please login or register to view this content. Registration is FREE
Marik is offline
Reply With Quote
View Public Profile
 
Old 02-01-2011, 07:29 AM Re: Total from Variable amount of Variables Loaded from Database
Novice Talker

Posts: 4
Trades: 0
I believe it would be a whole lot more easily if you draw a program structure first. And I do understand that the thing I am saying now might be too basic, and it might be useless or so, but sometimes we could simplify the matter by breaking it down first. You should state down the entire module that you could use in connecting each other together and which should be solved first, combined together, etc.
__________________

Please login or register to view this content. Registration is FREE
dropstalk is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Total from Variable amount of Variables Loaded from Database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13233 seconds with 12 queries