im creating an invoice function. It involves 3 pages.
The first page which is a form that takes in the variable of customer name, description, amount, quantity and discount.
The second page parses all these variable, make a new variable called total that will calculate amount X quantity minus away the discount and then insert into database then redirect to the third page.
The Third Page
Is supposed to sum the total column for a final total variable. How do i use the sum function to calculate the total in order to get the final total? And also how do i store all the variables printed out so that i can store them into database?
E.g. i want to print out all the descriptions, customer name, amount, quantity and total entered in page one and then add the final total which was calculated in page 3 .
Page one(submit form)
<!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>Untitled Document</title>
<script type="text/javascript">
var count = 0;
function addTextArea() {
count = count + 1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' " + "id=name" + count + "> </div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' " + "id=quantity" + count + "></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]' value ='' " + "id=amount" + count + "></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('discount');
div.innerHTML += "<div><input type='text' name='discount[]' value ='' " + "id=discount" + count + "></div>";
div.innerHTML += "\n<br />";
}
function removeTextArea() {
document.getElementById("name" + count).remove();
document.getElementById("quantity" + count).remove();
document.getElementById("amount" + count).remove();
document.getElementById("discount" + count).remove();
count = count - 1;
}
</script>
</head>
<body>
<form action="invoicesubmit.php" method="POST">
<?php
echo "<table border='2'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "</tr>"; echo "<tr>";
echo "<td>"?>
<input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/>
<?php "</td>"; echo "<td>"?>
<input type='text' size="50" name='quantity[]' value='' />
<?php "</td>";
echo "<td>"?>
<input type='text' size="50" name='amount[]' value='' />
<?php "</td>";
echo "<td>"?>
<input type='text' size="50" name='discount[]' value='' />
<?php "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"?>
<div id="name"></div>
<?php "</td>"; echo "<td>"?>
<div id="quantity"></div>
<?php "</td>"; echo "<td>"?>
<div id="amount"></div>
<?php "</td>"; echo "<td>"?>
<div id="discount"></div>
<?php "</td>";
echo "</tr>"; ?> <br />
<input type="button" value="Add Description" onClick="addTextArea();">
Customer Name: <input type="text" value="" name="cust_name" />
<input type="button" value="Remove Description" onClick="removeTextArea();">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Page Two (Store into database)
<?php require_once ("includes/session.php");?>
<?php require_once ("includes/db_connection.php");?>
<?php require_once ("includes/functions.php");?>
<?php require_once ("includes/validation_function.php");?>
<?php
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Total_amt</th>\n";
echo "</tr>";
<?php
if (isset($_POST['submit']))
{ // Process the form
$name_array = $_POST['name'];
$quantity_array = $_POST['quantity'];
$amount_array = $_POST['amount'];
$discount_array = $_POST['discount'];
for ($i = 0; $i < count($name_array); $i++)
{
$name = $name_array[$i];
$quantity = $quantity_array[$i];
$amount = $amount_array[$i];
$discount = $discount_array[$i];
$total_amt = ($amount - ($amount * ($discount / 100))) * $quantity;
$cust_name = mysqlprep($_POST["cust_name"]);
global $connection;
$query = "INSERT INTO invoicesub (";
$query.= " cust_name, description, quantity, amount, discount, total";
$query.= ") VALUES (";
$query.= " '{$cust_name}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total}";
$query.= ")";
$result = mysqli_query($connection, $query);
echo "<tr>";
echo "<td>" . $name . "</td>";
echo "<td>" . $quantity . "</td>";
echo "<td>" . "$" . $amount . "</td>";
echo "<td>" . $discount . "%" . "</td>";
echo "<td>" . "$" . $total_amt . "</td>";
echo "</tr>";
if ($result)
{
redirect_to("invoicesubmitfinal.php");
}
}
?>
page three (Print and store into 2nd database)
<?php
$query = "SELECT * ";
$query.= "FROM invoicesub";
$result = mysqli_query("SELECT sum(amount) FROM invoicesub") or die(mysqli_error());
while ($rows = mysql_fetch_array($result))
{
$result1 = mysqli_query("SELECT sum(quantity) FROM invoicesub") or die(mysqli_error());
while ($rows1 = mysqli_fetch_array($result1))
{
$result2 = mysqli_query("SELECT sum(total) FROM invoicesub") or die(mysqli_error());
while ($rows2 = mysqli_fetch_array($result2))
{
echo $rows['amount'];
echo $rows1['quantity'];
echo $rows2['total'];
$finaltotal = $rows2['total'];
}
}
}
?>
Since, your requirement is not really understandable, i tried clearing some errors & changed the SQL. if you, refrain your questions, along with the included function & connection files, we can only take a look. The modified codes are shown below.
invoicesubmit.php
<?php require_once ("includes/session.php");?>
<?php require_once ("includes/db_connection.php");?>
<?php require_once ("includes/functions.php");?>
<?php require_once ("includes/validation_function.php");?>
<?php
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Total_amt</th>\n";
echo "</tr>";
if (isset($_POST['submit'])){ // Process the form
$name_array = $_POST['name'];
$quantity_array = $_POST['quantity'];
print_r($quantity_array);
$amount_array = $_POST['amount'];
$discount_array = $_POST['discount'];
for ($i = 1; $i < count($name_array); $i++){
$name = $name_array[$i];
$quantity = $quantity_array[$i];
$amount = $amount_array[$i];
$discount = $discount_array[$i];
$total_amt = ($amount - ($amount * ($discount / 100))) * $quantity;
$cust_name = mysqlprep( $_POST["cust_name"]);
global $connection;
$query = "INSERT INTO invoicesub (";
$query.= " cust_name, description, quantity, amount, discount, total";
$query.= ") VALUES (";
$query.= " '{$cust_name}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total_amt}";
$query.= ")";
$result = mysqli_query($connection, $query);
echo "<tr>";
echo "<td>" . $name . "</td>";
echo "<td>" . $quantity . "</td>";
echo "<td>" . "$" . $amount . "</td>";
echo "<td>" . $discount . "%" . "</td>";
echo "<td>" . "$" . $total_amt . "</td>";
echo "</tr>";
}
if ($result){
redirect_to("invoicesubmitfinal.php?cname=$cust_name");
}
}
?>
invoicesubmitfinal.php
<?php
require_once ("includes/db_connection.php");
global $connection;
$name=$_REQUEST['cname'];
$sql1="SELECT sum(amount) as amount, sum(quantity) as quantity, sum(total) as total FROM invoicesub where cust_name=$name";
$result = mysqli_query($connection, $sql1) or die(mysqli_error());
while ($rows = mysql_fetch_array($result)){
echo $rows['amount'];
echo $rows['quantity'];
echo $rows['total'];
}
?>
Related
After selecting the check box, the check box values and the sum values are reset when the next page is passed. How should I modify it?
PHP code:
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td > <input name='amount' type='checkbox' value ='" . $row['amount'] . " ' onclick='totalIt()'></td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['DATE'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo " <input value='₩0' readonly='readonly' type='text' name='total' />";
// Free result set
mysqli_free_result($result);
JavaScript code:
function totalIt() {
var input = document.getElementsByName("amount");
var total = 0;
for (var i = 0; i < input.length; i++) {
if (input[i].checked) {
total += parseFloat(input[i].value);
}
}
document.getElementsByName("total")[0].value = "₩" + total;
}
and How can I check one more value in advance?
I cannot seem to get the value from my dropdown box which is using JQuery linked up to my database, into an SQL statement?
Where is the error?
Thanks
Here is my PHP:
<form action="" method="post">
<p class="timetable-p">Room code:
<select id="combobox" name="combobox">
<form action="" method="post"><?php
echo '<option class="option">Type/Select a room</option>';
while ($row = $res->fetchRow()) {
$code = $row['roomcode'];
$titles[] = $row['park'];
echo '<option class="option" name="codedrop">'.$code.'</option>';
}
?>
<input type="submit" value="subm" name="subm">
</form>
<?php
if( isset( $_POST['subm'] ) )
{
$codedropOption= $_POST['codedrop'];
$resql = "SELECT * FROM 'ROOMS' WHERE 'roomCode' LIKE '$codedrop%'";
$res1 = mysql_query($resql);
echo "<table>";
while($row = mysql_fetch_array($res1)){
echo "<tr><td>" . $row['roomCode'] . "</td>";
echo "<td>" . $row['Style'] . "</td><td>" . $row['dataProjector'] . "</td>";
echo "<td>" . $row['Whiteboard'] . "</td><td>" . $row['OHP'] . "</td>";
echo "<td>" . $row['wheelchairAccess'] . "</td>";
echo "<td>" . $row['lectureCapture'] . "</td>";
echo "<td><input type='radio' name='radioSelect' value= '". $row['roomCode']."'></td>";
}
echo "</table>";
}
?>
</form>
1) Remove name="codedrop" from <option> Like
echo '<option class="option">'.$code.'</option>';
N.B.: Only <select> bears the name attribute, not <option>.
2) Remove first <form>, it's of no use. And even, nested <form> are not allowed.
3) Change
$codedropOption= $_POST['codedrop'];
To
$codedropOption= $_POST['combobox'];
to not get
Undefined index: codedrop
4) Change
$resql = "SELECT * FROM 'ROOMS' WHERE 'roomCode' LIKE '$codedrop%'";
To
$resql = "SELECT * FROM ROOMS WHERE roomCode LIKE '$codedrop%'";
Use backtick in place of single quotes '.
5) Change
$resql = "SELECT * FROM 'ROOMS' WHERE 'roomCode' LIKE '$codedrop%'";
To
$resql = "SELECT * FROM ROOMS WHERE roomCode LIKE '$codedropOption%'";
Updated Code
<form action="" method="post">
<p class="timetable-p">Room code:
<select id="combobox" name="combobox">
<?php
echo '<option class="option">Type/Select a room</option>';
while ($row = $res->fetchRow()) {
$code = $row['roomcode'];
$titles[] = $row['park'];
echo '<option class="option">'.$code.'</option>';
}?>
</select>
<input type="submit" value="subm" name="subm">
</form>
<?php
if( isset( $_POST['subm'] ) )
{
$codedropOption= $_POST['combobox'];
$resql = "SELECT * FROM ROOMS WHERE roomCode LIKE '$codedropOption%'";
$res1 = mysql_query($resql);
echo "<table>";
while($row = mysql_fetch_array($res1)){
echo "<tr><td>" . $row['roomCode'] . "</td>";
echo "<td>" . $row['Style'] . "</td><td>" . $row['dataProjector'] . "</td>";
echo "<td>" . $row['Whiteboard'] . "</td><td>" . $row['OHP'] . "</td>";
echo "<td>" . $row['wheelchairAccess'] . "</td>";
echo "<td>" . $row['lectureCapture'] . "</td>";
echo "<td><input type='radio' name='radioSelect' value= '". $row['roomCode']."'></td>";
}
echo "</table>";
}
?>
How do I retrieve data from a SQL table, modify the data and store it in another database table with multiple rows & columns and with single submit button I want insert every rows at a time I don't know how to get that hidden value and work properly with that
<?php
include"connect_database.php";
if(isset($_POST['submit'])) {
$amt = $_POST['total'];
if($amt > 0) {
$qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
for($i=1; $i<=$amt; $i++) {
$qry .= "('".$_POST["rollno$i"]."', '".$_POST["name$i"]."', '".$_POST["year$i"]."', '".$_POST["attendance$i"]."', '".$_POST["reason$i"]."' ),"; // loop the mysql_query values to avoid more server loding time
}
$qry = substr($qry, 0, strlen($qry)-2);
$insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
}
// Redirect for each cases
if($insert) {
$msg = '<script type="text/javascript">alert("added");</script>';
}
else {
$msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
}
};
if (isset($_POST['select']))
{
$sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);
echo "<table border=1>
<tr>
<th>Rollno</th>
<th>Name</th>
<th>Year</th>
<th>Attendance</th>
<th>reason</th>
</tr>";
for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=rollno$i value=" . $record['rollno'] . " </td>";
echo "<td>" . "<input type=text name=name$i value=" . $record['name'] . " </td>";
echo "<td>" . "<input type=text name=year$i value=" . $record['year'] . " </td>";
echo "<td> "."<select name=attendance$i >
<option value=Present >present</option>
<option value=Absent >Absent</option>
</select>"."</td>";
echo "<td>". "<textarea cols=15 rows=2 name=reason$i placeholder=Enter reason ...></textarea>" . "</td>" ;
echo "<td>" . "<input type=hidden name=total value=" . $i-1 . "</td>";
echo "</tr>";
}
echo"</table>";
echo "<input type=submit name=submit value=save class=Button3>";
echo "</form>";
};
mysqli_close($dbcon);
?>
you are opening multiple forms, for each row in your table on.
This causes your html to be invalid, just start the form before displaying the table.
You could use this html
<table>
<?php
for ($i = 0; $i < $num; $i++) {
$record = mysqli_fetch_array($myData);
?>
<tr>
<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
<td><input type="text" name="name[<?= $record['rollno'] ?>]" value="<?= $record['name']?>" </td>
<td><input type="text" name="year[<?= $record['rollno'] ?>]" value="<?= $record['year'] ?>" </td>
<td><select name="attendance[<?= $record['rollno'] ?>]" >
<option value="Present" >present</option>
<option value="Absent" >Absent</option>
</select></td>
<td><textarea cols="15" rows="2" name="reason[<?= $record['rollno'] ?>]" placeholder="Enter reason ..."></textarea></td>
</tr>
<?php
}
?>
</table>
with this your values will every row will be put into the $_POST-Array, you can access the values via the indexes (I am guessing rollno represents the ID of the dataset).
When you really only want to insert all the values into a table, you can leave the index out. Meaning you could write
<td><input type="text" name="rollno[]" value="<?= $record['rollno'] ?>" </td>
Instead of
<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
You don't need the hidden field, you can just count the items in the array.
$total = count($_POST['rollno']);
<?php
include"connect_database.php";
if(isset($_POST['submit'])) {
$amt = $_POST['total'];
$rollnos= $_POST['rollno'];
if($amt > 0) {
$qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
$i=0;
foreach($rollnos as $rollno) {
$qry .= "('".$rollno."', '".$_POST["name"][$i]."', '".$_POST["year"][$i]."', '".$_POST["attendance"][$i]."', '".$_POST["reason"][$i]."' ),"; // loop the mysql_query values to avoid more server loding time
$i=$i+1;
}
$qry = substr($qry, 0, strlen($qry)-2);
$insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
}
// Redirect for each cases
if($insert) {
$msg = '<script type="text/javascript">alert("added");</script>';
}
else {
$msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
}
};
if (isset($_POST['select']))
{
$sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);
echo "<table border=1>
<tr>
<th>Rollno</th>
<th>Name</th>
<th>Year</th>
<th>Attendance</th>
<th>reason</th>
</tr>";
for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
echo "<tr>";
echo "<td>" . "<input type='text' name='rollno[]' value='" . $record['rollno'] . "'> </td>";
echo "<td>" . "<input type='text' name='name[]' value='" . $record['name'] . "'> </td>";
echo "<td>" . "<input type='text' name='year[]' value='" . $record['year'] . "'> </td>";
echo "<td> "."<select name='attendance[]' >
<option value='Present' >present</option>
<option value='Absent' >Absent</option>
</select>"."</td>";
echo "<td>". "<textarea cols='15' rows='2' name='reason[]' placeholder='Enter reason ...'></textarea>" . "</td>" ;
echo "<td></td>";
echo "</tr>";
}
echo "<input type='hidden' name='total' value='" . $i-1 . "'>";
echo"</table>";
echo "<input type='submit' name='submit' value='save' class='Button3'>";
echo "</form>";
};
mysqli_close($dbcon);
?>
The Problem: Trying to calculate sum of a total variable which is inside the table invoicesub in the Form Process (2nd page). Trying to make the sum(total) appear in 3rd page which is display_invoice and also get the customer_name in the form process page(2nd page). somehow $_get["$cust_name"]; is not working and im getting a blank from it. Next, after i can get the sum_total variable how do i parse all these to the 4th page using a form?
Edit:http://imageshack.com/a/img19/8729/btls.png (picture of output)
http://imageshack.com/a/img20/8744/s3ut.png (picture of my database)
Too low rep, i cant post images. I uploaded it to image shack. The 1 on top of the table is from the mysqli_num_rows. This is the first row i entered in the form page. The 2nd row is missing
edit:
New Invoicefinal
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Services Rendered</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Price($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Amount</th>\n";
echo "</tr>";
$cname = $_GET["cname"];
global $connection;
$sql1="SELECT description,quantity, amount, discount, total, SUM(total) as sumtotal FROM invoicesub WHERE cust_name='$cname' GROUP BY description ORDER BY id";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
echo $count;
while ($rows = mysqli_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $rows['description'] . "</td>";
echo "<td>" . $rows['quantity'] . "</td>";
echo "<td>" . $rows['amount'] . "</td>";
echo "<td>" . $rows['discount']. "%" . "</td>";
echo "<td>" ."$". $rows['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
$sql1="SELECT SUM(total) as total_amt FROM invoicesub WHERE cust_name='$cname'";
$result3 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
while ($rows = mysqli_fetch_array($result3)){
echo "<tr>";
echo "<td>". "Total Amount:" ."$". $rows['total_amt'] . "</td>";
echo "</tr>";
echo "<form action=\"4thpage.php\" method=\"POST\">";
echo "<input type=\"hidden\" name=\"total_amt\"/>";
echo "Customer Paid:";
echo "<input type=\"text\" name=\"paid\" value=\"\"/>";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\"/>";
echo "<input type=\"button\" value=\"Cancel\" onclick=\"window.location='manage_content.php';\"/>";
echo "</form>";
}
The Form: It consists of a form that can allow more than 1 row of inputs, which means i will have more than 2 descriptions, quantity, amount and discount. Thus, i put them inside an array
Form Process: This is the 2nd page, it will take in all the arrays parsed in, make a loop to insert all the inputted variables in the form into the table invoicesub.
Display_invoice: This is the 3rd page, i want to show all the customer_name, descriptions, quantity, amount, total_amt that was keyed into invoicesub and then do a sum(total) and show it at the bottom of the page. I also want to insert the customer_name, sum(total) variable into the fourth page through the form at display_invoice. Is this possible?
Form
function addTextArea(){
count= count+1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' "+"id=name"+count+"> </div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' "+"id=quantity"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]' value ='' "+"id=amount"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('discount');
div.innerHTML += "<div><input type='text' name='discount[]' value ='' "+"id=discount"+count+"></div>";
div.innerHTML += "\n<br />";
}
function removeTextArea(){
document.getElementById("name"+count).remove();
document.getElementById("quantity"+count).remove();
document.getElementById("amount"+count).remove();
document.getElementById("discount"+count).remove();
count = count-1;
}
</script>
</head>
<body>
<form action="invoiceprocess.php" method="POST">
<?php
echo "<table border='2'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "</tr>";
echo "<tr>";
echo "<td>"?><input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/><?php "</td>";
echo "<td>"?><input type='text' size="50" name='quantity[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='discount[]' value='' /><?php "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"?><div id="name"></div> <?php "</td>";
echo "<td>"?><div id="quantity"></div> <?php "</td>";
echo "<td>"?><div id="amount"></div> <?php "</td>";
echo "<td>"?><div id="discount"></div> <?php "</td>";
echo "</tr>";
?>
Customer Name:
<br />
<input type="text" name="cust_name" value="" />
<br />
<input type="button" value="Add Description" onClick="addTextArea();">
<input type="button" value="Remove Description" onClick="removeTextArea();">
<input type="submit" name="submit" value="submit">
</form>
</body>
invoiceprocess (The 2nd page)
if (isset($_POST['submit'])){ // Process the form
$name_array = $_POST['name'];
$quantity_array = $_POST['quantity'];
$amount_array = $_POST['amount'];
$discount_array = $_POST['discount'];
$cust_name_array = mysql_prep( $_POST['cust_name']);
for ($i = 0; $i < count($name_array); $i++){
$cust_name = $cust_name_array;
$name = $name_array[$i];
$quantity = $quantity_array[$i];
$amount = $amount_array[$i];
$discount = $discount_array[$i];
$total_amt = ($amount - ($amount * ($discount / 100))) * $quantity;
global $connection;
$query = "INSERT INTO invoicesub (";
$query.= " cust_name, description, quantity, amount, discount, total";
$query.= ") VALUES (";
$query.= " '{$cust_name}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total_amt}";
$query.= ")";
$result = mysqli_query($connection, $query);
}
redirect_to("invoicesubmitfinal.php?cname=".urlencode($cust_name));
}
Display_Invoice(invoicesubmitfinal.php)
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Customer_name</th>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Total_amt</th>\n";
echo "</tr>";
$cust_name = $_GET["$cust_name"];
global $connection;
$sql1="SELECT SUM(total) FROM invoicesub WHERE cust_name='$cust_name'";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
while ($rows = mysqli_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $rows['quantity'] . "</td>";
echo "<td>" . $rows['amount'] . "</td>";
echo "<td>" . $rows['discount']. "%" . "</td>";
echo "<td>" ."$". $rows['total'] . "</td>";
echo "<td>" . "$" . $total_amt . "</td>";
echo "</tr>";
<form action="insertfinal.php" method="POST">
Customer Paid:
<input type="text" name="paid" value="" />
</form>
i am creating a form to do billing and i had a fatal error on the total_amt_array. Also, how do i parse an array of items into database? The form is in the Create Invoice and the invoicesubmit is where it adds all the arrays into the database. Anyone can explain to me why i can't make a total sum of the discount, amount and quantity array.
Also, does anyone have an easier way to calculate all of the total_amt_array. I've searched stackoverflow and others find the final total amount by adding the entire cost column to get the final total cost amount.
Create Invoice
<!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>Untitled Document</title>
<script type="text/javascript">
var count = 0;
function addTextArea(){
count= count+1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' "+"id=name"+count+"> </div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' "+"id=quantity"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]' value ='' "+"id=amount"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('discount');
div.innerHTML += "<div><input type='text' name='discount[]' value ='' "+"id=discount"+count+"></div>";
div.innerHTML += "\n<br />";
}
function removeTextArea(){
document.getElementById("name"+count).remove();
document.getElementById("quantity"+count).remove();
document.getElementById("amount"+count).remove();
document.getElementById("discount"+count).remove();
count = count-1;
}
</script>
</head>
<body>
<form action="invoicesubmit.php" method="POST">
<?php
echo "<table border='2'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "</tr>";
echo "<tr>";
echo "<td>"?><input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/><?php "</td>";
echo "<td>"?><input type='text' size="50" name='quantity[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='discount[]' value='' /><?php "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"?><div id="name"></div> <?php "</td>";
echo "<td>"?><div id="quantity"></div> <?php "</td>";
echo "<td>"?><div id="amount"></div> <?php "</td>";
echo "<td>"?><div id="discount"></div> <?php "</td>";
echo "</tr>";
?>
<br />
<input type="button" value="Add Description" onClick="addTextArea();">
<input type="button" value="Remove Description" onClick="removeTextArea();">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
invoicesubmit
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/db_connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/validation_function.php"); ?>
<?php
if (isset($_POST['submit'])) {
// Process the form
$name_array = $_POST['name'];
$quantity_array = $_POST['quantity'];
$amount_array = $_POST['amount'];
$discount_array = $_POST['discount'];
$total_amt_array = ($amount_array - ($amount_array * ($discount_array/ 100))) * $quantity_array ;
for ($i =0; $i < count($name_array); $i++) {
$name = $name_array[$i];
$quantity = $quantity_array[$i];
$amount = $amount_array[$i];
$discount = $discount_array[$i];
$total_amt = $total_amt_array[$i];
echo $name;
echo "<br />";
echo $quantity;
echo "<br />";
echo $amount;
echo "<br />";
echo $discount;
echo "<br />";
echo $total_amt;
}
}
/*
//validations
$required_fields = array("name", "quantity", "amount", "discount");
validate_presences($required_fields);
$fields_with_max_lengths = array("name" => 200);
validate_max_lengths($fields_with_max_lengths);
if (!empty($errors)) {
$_SESSION["errors"] = $errors;
redirect_to("create_invoice.php");
}*/
/*
// 2. Perform database query
$query = "INSERT INTO invoicesub (";
$query .= " description, quantity, amount, discount, total";
$query .= ") VALUES (";
$query .= " '{$name}', '{$quantity}', '{$amount}', '{$discount}', '{$total}'";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
// Success
$_SESSION["message"] = "Subject created.";
redirect_to("confirm_invoice.php");
}
else {
// Failure
$_SESSION["message"] = "Subject creation failed.";
redirect_to("create_invoice.php");
}
} else {
// This is probably a GET request
redirect_to("create_invoice.php");
} */
?>
<?php
if (isset($connection)) { mysqli_close($connection); }
?>
First you have:
echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
^^---create an array in $_POST
Then:
$amount_array = $_POST['amount'];
^^^^^^^^^^^----this is now an array
Then:
$total_amt_array = ($amount_array - ($amount_array * ($discount_array/ 100))) * $quantity_array ;
This code boils down to:
$total_amt_array = Array - (Array * (Array / 100))) * Array;
You cannot multiply/divide arrays in PHP.