how to get the total of the earnings [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
the code is expected to calculate the total amount of the "Net Earning" row. How do i write a code to do that. The $stmt variable is associated to a query from the database
<div class="block-card-body">
<div class="my-table table-responsive">
<table class="table align-items-center table-flush mb-0">
<thead class="thead-light">
<tr>
<th>Order ID</th>
<th>Amount</th>
<th>Fee</th>
<th>Net Earning</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<?php
foreach ($stmt as $val) {
?>
<td><?php echo $val['PAY_ID']; ?></td>
<td class="text-color">$<?php echo $val['PAY_AMOUNT']; ?></td>
<td class="text-danger">$<?php echo ($val['PAY_AMOUNT']) * 0.1 ; ?></td>
<td class="text-success">$<?php echo $val['PAY_AMOUNT'] - (($val['PAY_AMOUNT']) * 0.1) ; ?></td>
<td><?php echo $val['PAY_DATE']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div><!-- end block-card-body -->

Create a variable to use as an accumulator, initialise it to zero.
Then in the loop, calulate the net and add it to your accumulator. How you display it is up to you later.
Also note, I moved the <tr> inside the loop so you get a well formed table row
<tbody>
<?php
$netTotal = 0;
foreach ($stmt as $val) {
?>
<tr>
<td><?php echo $val['PAY_ID']; ?></td>
<td class="text-color">$<?php echo $val['PAY_AMOUNT']; ?></td>
<td class="text-danger">$<?php echo ($val['PAY_AMOUNT']) * 0.1 ; ?></td>
<?php
$t = $val['PAY_AMOUNT'] - (($val['PAY_AMOUNT']) * 0.1);
$netTotal += $t;
?>
<td class="text-success">$<?php echo $t;?></td>
<td><?php echo $val['PAY_DATE']; ?></td>
</tr>
<?php
}
?>
</tbody>

Related

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\read.php on line 41 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to create a website that can do the basic my SQL functions like create, read, update and delete and I was working on the read page when I got this error:
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\read.php on line 41.
Here is my code :
<?php
$link = mysqli_connect("localhost", "root", "", "db_school");
if (mysqli_connect_error()) {
die ("Database Connection Error");
}
$query = "SELECT * FROM tbl_student ORDER BY id DESC";
$result = mysqli_query($link, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Read</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
</head>
<body>
<?php
include 'procces_read.php';
?>
<div class="container">
<div class="box">
<h4 class="display-4 text-center">Read</h4>
<br>
<?php if (isset($_GET['success'])) { ?>
<div class="alert alert-success" role="alert">
<?php echo $_GET['success']; ?>
</div>
<?php } ?>
<?php if (mysqli_num_rows($result)) { ?>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Student ID</th>
<th scope="col">Student Name</th>
<th scope="col">Student DOB</th>
<th scope="col">Student Sex</th>
<th scope="col">Student Class</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
while($rows = mysqli_fetch_assoc($result)){
$i++;
}
?>
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $rows['student_id'];?></td>
<td><?php echo $rows['Name']; ?></td>
<td><?php echo $rows['DOB']; ?></td>
<td><?php echo $rows['sex']; ?></td>
<td><?php echo $rows['Class']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="link-right">
Create
</div>
</div>
</div>
</body>
</html>
Your table rows (from <tr> to </tr>) should be inside the while loop. The $rows variable is not defined outside of it.
<tbody>
<?php
$i = 0;
while($rows = mysqli_fetch_assoc($result)){
$i++;
?>
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $rows['student_id'];?></td>
<td><?php echo $rows['Name']; ?></td>
<td><?php echo $rows['DOB']; ?></td>
<td><?php echo $rows['sex']; ?></td>
<td><?php echo $rows['Class']; ?></td>
</tr>
<?php }
} ?>

MySQL not fetching all results

Trying to fetch all results in PHP from MySQL database, but it is leaving the first query.
My MySQL table is in the below image: .
My Code:
<?php
$irn = "33857";
$stmt = $user_home->runQuery('SELECT * FROM invoice WHERE Inv = :inv ORDER BY Sr ASC ');
$stmt->bindParam(':inv',$irn);
$stmt->execute();
$rowc = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
?>
<table id="chiru_inv" class="table table-striped table-hover table-bordered table-responsive">
<tr>
<td colspan="4" align="center">
<h1>Company</br><span style="font-size: 75%;">Number</span></h1>
</td>
</tr>
<tr>
<td colspan="2">
<h3><span style="float: left;"><?php echo $rowc['Customer']; ?> (<?php echo $rowc['Inv']; ?>)</span></h3>
</td>
<td colspan="2">
<h3><span style="float: right;"><?php echo $rowc['Date']; ?></span></h3>
</td>
</tr>
<tr>
<th>Sr.</th>
<th>Item</th>
<th>Qty</th>
<th>Amount</th>
</tr>
<?php
$i = 0;
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $Item; ?></td>
<td>5</td>
<td>200</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2"><strong><?php echo getIndianCurrency(225); ?>Only</strong></td>
<td><strong>Total:</strong></td>
<td><strong>225</strong></td>
</tr>
</table>
<?php
}
?>
The result when I execute the above code is in the below image:
There are three queries with invoice number 33857, but only two are displayed (leaving the first one)!
I need all to be displayed as per invoice number.
Please help me from sorting out the error/code I made or I left.
The problem in your code is with the line:
$rowc = $stmt->fetch(PDO::FETCH_ASSOC);
in the beginning of your code you're fetching the first row.
You have to update your while loop, like this
<?php
$i = 0;
while($rowc)
{
extract($rowc);
$i++;
?>
<!-- Your HTML/PHP code for the table -->
<?php
$rowc=$stmt->fetch(PDO::FETCH_ASSOC);
}
?>
So, the idea is to fetch the row at the end of the while loop, in order to not lose your first row.

adding inline CSS in datatable row on condition not working

I tried to highlight records whose certain columns have some values other than NULL. I'm using dataTable plugin.
<table class="table table-striped table-hover" id="checkin-checkout-record-table">
<thead>
<tr>
<th>Employee Name</th>
<th>Check-in-date</th>
<th>Check-in-time</th>
<th>Check-out-date</th>
<th>Check-out-time</th>
<th class="col-lg-2">Late Check-in Remarks</th>
<th class="col-lg-2">Early Check-out Remarks</th>
</tr>
</thead>
<tbody>
<?php
foreach ($checkinCheckoutList as $member): ?>
<tr <?php
if(isset($member['early_checkout_remarks']) ||isset($member['delayed_checkin_remarks']))
{?>
style="background-color:red;"
<?php } ?> >
<td><?php echo $member['fullname'] ?></td>
<td> <?php echo $member['checkin_date']; ?></td>
<td> <?php echo $member['checkin_time']; ?></td>
<td><?php echo $member['checkout_date']; ?></td>
<td><?php echo $member['checkout_time']; ?></td>
<td><?php echo $member['delayed_checkin_remarks']; ?></td>
<td><?php echo $member['early_checkout_remarks']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
But the result is not as expected. Some records are not highlighted. This works well with other normal table. Please help.

My full data is not able to display on my web page from database

I wrote code to retrieve data from database and to print in form of tables on my web. I have stored 4 columns of data in my database but while retrieving it's only showing one column.
My database image
My webpage
My code:
<?php
$con = mysqli_connect("localhost", "root", "", "project");
if(!$con)
{
die('not connected');
}
$result= mysqli_query($con, "SELECT name, stay, food, travel,
SUM(stay + food + travel) AS totalamount,doj
FROM placedetails ");
?>
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>place</th>
<th>stay cost</th>
<th>food cost</th>
<th>flight cost</th>
<th>Date of journey</th>
<th>Total cost</th>
</tr>
</thead>
<?php
while($row =mysqli_fetch_array($result))
{
?>
<tbody>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['doj'] ;?></td>
<td><?php echo $row['totalamount'] ;?></td>
</tr>
</tbody>
<?php
}
?>
</table>
</div>
</div>
Can anyone can tell where the mistake is?
And one more question: I want to diplay only the recent uploaded data on my web page. Suppose I have 4 names as mumbai, but uploaded at different times, I want to display the most recently added mumbai name on my web page
Can anyone help me out in this matter? I will be very thankful..
update your code as move tbody from php loop
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>place</th>
<th>stay cost</th>
<th>food cost</th>
<th>flight cost</th>
<th>Date of journey</th>
<th>Total cost</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['doj'] ;?></td>
<td><?php echo $row['totalamount'] ;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
1) <tbody> should be outside of while loop
2) if you want show only one record means no need to use while loop
3) if you want show recent record means just do descending sort by date column or id column
Query :
SELECT name, stay, food, travel, SUM(stay + food + travel) AS totalamount,doj FROM placedetails order by doj desc limit 1;
Table :
<tbody>
<?php
$row = mysqli_fetch_assoc($result); //fetch first record set only
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food']; ?></td>
<td><?php echo $row['travel']; ?></td>
<td><?php echo $row['doj']; ?></td>
<td><?php echo $row['totalamount'];?></td>
</tr>
</tbody>

Create HTML table with PHP foreach [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My table is messed when I tried to create the HTML table . Here is my code.
$data['items'] = array(
array(
'des' => 'item #1',
'price' => 10,
'o' => 2,
't' => 15,
),
array(
'des' => 'item #3',
'price' => 11,
'o' => 4,
't' => 10,
),
);
Here is my proceed to create the table. But it seems totally wrong. Specially the condition checking in foreach in the tr and td...
<table><thead align="left" style="display: table-header-group"><tr><th>
<table><tr> <td>col 1 </td>
<td>col 2 </td>
<td>col 3 </td>
<td>col 4 </td>
<td>col 5 </td>
</tr></table>
</th></tr></thead>
<tbody>
<?php foreach ($data['items'] as $rows) :?>
<tr class="item_row">
<?php
$total = 0;
foreach ($rows as $key => $value) : ?>
<td><?php echo $key+1 ?></td>
<td> <?php echo $value['description'] ?></td>
<td> <?php echo $value['price'] ?></td>
<td> <?php echo $value['o'] ?></td>
<td> <?php echo $value['t'] ?></td>
<?php endforeach;?>
</tr>
<?php endforeach;?>
</tbody>
</table>
The problem I am facing firstly the condition is not right, so at every row it just priting the first value every time.
secondly, obviously the table broke.
Need some help to solve this problem.
You have a loop too many, the inner loop is not necessary and causes warnings as $value is not an array.
Apart from that, you have messed up your table head, adding another table and th tags where they are not necessary.
You need something like:
<table>
<thead align="left" style="display: table-header-group">
<tr>
<th>col 1 </th>
<th>col 2 </th>
<th>col 3 </th>
<th>col 4 </th>
<th>col 5 </th>
</tr>
</thead>
<tbody>
<?php
$total = 0;
foreach ($data['items'] as $rows) :?>
<tr class="item_row">
<td><?php echo ++$total; ?></td>
<td> <?php echo $rows['description']; ?></td>
<td> <?php echo $rows['price']; ?></td>
<td> <?php echo $rows['o']; ?></td>
<td> <?php echo $rows['t']; ?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>

Categories