been trying out my new skills in php to build an accounts style system, that logs incoming and out going payments similar to what you would do in excel. thought this would be a good starting point to test my stills.
now ive built a form which submits to a database, and also a page that allows you to view the payments logged via date.
stuck on getting it to display a profit total. This will be worked out from the incoming payments total minus the outgoing payments totals.
ive attached my code below, id really appreciate any help i can get on this.
<style>
</style>
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM payments");
echo "<table border='0' align='center' text-align='left'>
<tr>
<th>Title:</th>
<th>Date:</th>
<th>Incoming:</th>
<th>Outgoing:</th>
<th>Notes:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
after adding code provided below:
<style>
</style>
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM payments");
$totalIncoming = 0;
$totalOutgoing = 0;
while($row = mysqli_fetch_array($result))
{
$totalIncoming .= $row['incoming'];
$totalOutgoing .= $row['outgoing'];
}
echo "<table border='0' align='center' text-align='left'>
<tr>
<th>Title:</th>
<th>Date:</th>
<th>Incoming:</th>
<th>Outgoing:</th>
<th>Notes:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
$profit = $totalIncoming - $totalOutgoing;
echo "Profit :".$profit;
mysqli_close($con);
?>
ok is this right?
<style>
</style>
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM payments");
$totalIncoming = 0;
$totalOutgoing = 0;
while($row = mysqli_fetch_array($result))
{
$totalIncoming += $row['incoming'];
$totalOutgoing += $row['outgoing'];
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
$profit = $totalIncoming - $totalOutgoing;
echo "Profit :".$profit;
mysqli_close($con);
?>
Try This
$totalIncoming = 0;
$totalOutgoing = 0;
while($row = mysqli_fetch_array($result))
{
$totalIncoming += $row['incoming'];
$totalOutgoing += $row['outgoing'];
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
$profit = $totalIncoming - $totalOutgoing;
echo "Profit :".$profit;
Related
I want to add "book" button to each car product.But it only display only one button for first car only.
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<td><button onclick=\"book_car('" . $row['car_id'] .
"')\">Book</button></td>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "</tr>";
}
There is no error.But i just want "book" button display for each car products.
This is simply because your button is out of the while loop !
Also you did not close first tr tag .
Correct code :
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>action</th>";<!-- Added this line -->
echo "</tr>";<!-- Added this line -->
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "<td><button onclick=\"book_car('" . $row['car_id'] .
"')\">Book</button></td>";<!-- Replaced This line -->
echo "</tr>";
}
echo "</table>";
I hope this helps you :)
I have the following PHP/MySql:
$sql = "SELECT * from tblDigest";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Diagram</th>
<th>Headcode</th>
<th>Date</th>
<th>Dep Time</th>
<th>Origin</th>
<th>Destination</th>
<th>Arr Time</th>
<th>Booked Traction</th>
<th>Actual Traction</th>
<th>Type</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["diagram"] . "</td>";
echo "<td>" . $row["headcode"] . "</td>";
echo "<td>" . $row["depDate"] . "</td>";
echo "<td>" . $row["depTime"] . "</td>";
echo "<td>" . $row["origin"] . "</td>";
echo "<td>" . $row["destination"] . "</td>";
echo "<td>" . $row["arrTime"] . "</td>";
echo "<td>" . $row["bookedTraction"] . "</td>";
echo "<td>" . $row["actualTraction"] . "</td>";
echo "<td>" . $row["type"] . "</td>";
echo "</tr>";
}
echo "</table>";
}
I would like to group the results by 'diagram', so that I can display them as an accordion. Is there any easy way to do this?
Failing that, how can I get the value for the first row of the table (so I can have a $diagNo variable and compare $row["diagram"] against that to find out when it has changed.
Sort your results by diagram and watch when its value changes:
$sql = "SELECT * from tblDigest ORDER BY diagram";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Diagram</th>
<th>Headcode</th>
<th>Date</th>
<th>Dep Time</th>
<th>Origin</th>
<th>Destination</th>
<th>Arr Time</th>
<th>Booked Traction</th>
<th>Actual Traction</th>
<th>Type</th>
</tr>";
// output data of each row
$oldDiagram = '';
while($row = $result->fetch_assoc()) {
if($oldDiagram == '')
{
// this is the first diagram - create the first accordeon
$oldDiagram = $row['diagram'];
}
elseif($oldDiagram != $row['diagram'])
{
// a new group starts right now - create new accordeon
$oldDiagram = $row['diagram'];
}
echo "<tr>";
echo "<td>" . $row["diagram"] . "</td>";
echo "<td>" . $row["headcode"] . "</td>";
echo "<td>" . $row["depDate"] . "</td>";
echo "<td>" . $row["depTime"] . "</td>";
echo "<td>" . $row["origin"] . "</td>";
echo "<td>" . $row["destination"] . "</td>";
echo "<td>" . $row["arrTime"] . "</td>";
echo "<td>" . $row["bookedTraction"] . "</td>";
echo "<td>" . $row["actualTraction"] . "</td>";
echo "<td>" . $row["type"] . "</td>";
echo "</tr>";
}
echo "</table>";
}
I am trying to add a column before my first column that starts numbering each record, starting from 1. I was trying to add autoincrement to this line echo "" . $row['ss'] . "";, but that does not seem to want to work. Any ideas how to do this?
My code looks like this:
$result = mysqli_query($con,"SELECT * FROM `results` WHERE Event='100' AND Gender='M' ORDER BY Performance ASC");
echo "<table border='0'>
<tr>
<th align='left'>Pos</th>
<th align='left'>Event</th>
<th>Performance</th>
<th>Wind</th>
<th>Place</th>
<th align='left'>Name</th>
<th align='left'>Surname</th>
<th>Age</th>
<th>Team</th>
<th>Meet</th>
<th>Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Add this line in header
<th align='left'>#</th>
And here php code
$count = 1;
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $count . "</td>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
//other code
$count=$count+1;
}
A better solution would be to use a variable counter to "number" your rows:
$counter = 1;
while($row = mysqli_fetch_array($result))
echo "<td>" . $counter . "</td>";
$counter++;
}
This is the right way to do it since if that ss column is the mysql auto increment, then your rows will not be numbered by the SORT but rather from the order in which they were inserted into the database regardless of any sorting you apply.
Try to change fetch_array with fetch_assoc
I have MAMP (local hosted SQL,WEB etc server) the database name is :NKTDEBITS the table name is :Insurance and the column on the table is STATECOV. I know I'm close with this but still get a black in the field that should generate the total, anyone got a idea?
<?php
$con=mysqli_connect("localhost","root","root","KNTDebits");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Insurance");
$result2 = mysqli_query($con,"SELECT * FROM Office");
$result3 = mysqli_query($con,"SELECT * FROM RichmondLocation");
$result4 = mysqli_query($con,"SELECT * FROM DanvilleLocation");
$result5 = mysql_query('SELECT SUM(STATECOV) AS STATECOV_sum FROM Insurance');
echo "<table border='1'>
<tr>
<th>Truck Number</th>
<th>VIN</th>
<th>Make</th>
<th>Model</th>
<th>State Coverage</th>
<th>Comprehinsive Coverage</th>
<th>Property Damage/th>
<th>Personal Injury</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['TNUM'] . "</td>";
echo "<td>" . $row['VIN'] . "</td>";
echo "<td>" . $row['MAKE'] . "</td>";
echo "<td>" . $row['MODEL'] . "</td>";
echo "<td>" . $row['STATECOV'] . "</td>";
echo "<td>" . $row['COMPRE'] . "</td>";
echo "<td>" . $row['PROPDMG'] . "</td>";
echo "<td>" . $row['PRSINJ'] . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 2 Start
str_repeat(' ', 5); // adds 5 spaces
echo "<table border='5'>
<tr>
<th>Richmond</th>
<th>Date</th>
<th>Payment</th>
<th>Payer</th>
</tr>";
while($row3 = mysqli_fetch_array($result3))
{
echo "<tr>";
echo "<td>" . $row3[''] . "</td>";
echo "<td>" . $row3['DATE'] . "</td>";
echo "<td>" . $row3['PAYMENT'] . "</td>";
echo "<td>" . $row3['PAYER'] . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 4 Start
str_repeat(' ', 5); // adds 5 spaces
echo "<table border='5'>
<tr>
<th>Danville</th>
<th>Date</th>
<th>Payment</th>
<th>Payer</th>
</tr>";
while($row4 = mysqli_fetch_array($result4))
{
echo "<tr>";
echo "<td>" . $row4[''] . "</td>";
echo "<td>" . $row4['DATE'] . "</td>";
echo "<td>" . $row4['PAYMENT'] . "</td>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 5 Start
echo "<table border='5'>
<tr>
<th>Total</th>
</tr>";
$result = mysql_query('SELECT SUM(STATECOV) AS value_sum FROM Insurance');
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
while($row = mysql_fetch_assoc($result));
{
echo "<tr>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You need a GROUP BY to go along with your SUM. I don;t know enough about you table to let you know what column you should use for this.
You should be handling potential error cases whenn you query the database, as you will quickly see when you are getting database/query errors.
You should also look to use mysqli or PDO instead of the deprecated mysql_* functions.
How do I place a space between two tables which are vertical. The tables are within the php script:
$result = mysql_query("SELECT * FROM t2 WHERE FIRSTNAME='{$_POST["fname"]}' AND HOSPNUM='{$_POST["hnum"]}'");
echo "<table border='1'>
<tr>
<th>HospNum</th>
<th>RoomNum</th>
<th>LastName</th>
<th>FirstName</th>
<th>MidName</th>
<th>AdmitDate</th>
<th>AdmitTime</th>
<th>Address</th>
<th>TelNum</th>
<th>Civil Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['HOSPNUM'] . "</td>";
echo "<td>" . $row['ROOMNUM'] . "</td>";
echo "<td>" . $row['LASTNAME'] . "</td>";
echo "<td>" . $row['FIRSTNAME'] . "</td>";
echo "<td>" . $row['MIDNAME'] . "</td>";
echo "<td>" . $row['ADDATE'] . "</td>";
echo "<td>" . $row['ADTIME'] . "</td>";
echo "<td>" . $row['ADDRESS'] . "</td>";
echo "<td>" . $row['TELNUM'] . "</td>";
echo "<td>" . $row['CSTAT'] . "</td>";
echo "</tr>";
}
echo "</table>";
$result2 = mysql_query("SELECT * FROM t2 WHERE FIRSTNAME='{$_POST["fname"]}' AND HOSPNUM='{$_POST["hnum"]}'");
echo "<table border='1'>
<th>Sex</th>
<th>Age</th>
<th>Birthday</th>
<th>STAT1</th>
<th>STAT2</th>
<th>STAT4</th>
<th>STAT5</th>
<th>STAT6</th>
<th>STAT7</th>
<th>STAT8</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['SEX'] . "</td>";
echo "<td>" . $row['AGE'] . "</td>";
echo "<td>" . $row['BDAY'] . "</td>";
echo "<td>" . $row['STAT'] . "</td>";
echo "<td>" . $row['STAT2'] . "</td>";
echo "<td>" . $row['STAT3'] . "</td>";
echo "<td>" . $row['STAT4'] . "</td>";
echo "<td>" . $row['STAT5'] . "</td>";
echo "<td>" . $row['STAT6'] . "</td>";
echo "<td>" . $row['STAT7'] . "</td>";
echo "<td>" . $row['STAT8'] . "</td>";
echo "</tr>";
}
echo "</table>";
use CSS to apply margin between the two tables:
<style type="text/css">
table{
margin: 10px 0;
}
</style>