Php Loop TD if rows didn't exist or match - php

i want to loop some DTR values and their absent in PHP
As you can see the date should be 1,2,3,4,5 but since the only record they have is 3,4,5 there's some skipped rows, is it possible to loop empty TD and put missing date if there's no row exist? like i want to loop 1-31 then match the date then put the date where it belongs? idk dude
this is my code for my loop which basically just call the results of my query
<?php foreach ($report_stmt as $row): ?>
<tr>
<td><?php
$thisDate = $row['dtr_date'];
$day = strtotime($thisDate);
$newFormat = ltrim(date('d',$day),0);
echo $newFormat;
?></td>
<td><?php echo $row['dtr_in'] ?></td>
<td><?php echo $row['dtr_out'] ?></td>
<td><?php echo $row['dtr_in2'] ?></td>
<td><?php echo $row['dtr_out2'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
this is my query
$report_list = "SELECT * FROM tbl_dtr INNER JOIN tbl_users_info ON tbl_dtr.dtr_by = tbl_users_info.user_info_email WHERE dtr_by = :user AND dtr_date LIKE '%$month_filter%' ";

You can try to aggregate an array with the day of month as index. Then just loop through the day of months instead of loop through the query results.
Note: date with 'j' is better than trimming leading zero with 'd'. You may read the DateTime::format for the details of all formatting character.
<?php
// Aggregate an array of reports first
// Note: assume there is no 2 rows with the same 'dtr_date'
$reports = [];
foreach ($report_stmt as $row) {
$row['date'] = date('j', strtotime($row['dtr_date']));
$reports[$row['date']] = $row;
}
?>
<?php for ($i=1; $i<=31; $i++): ?>
<tr>
<td><?php echo $i ?></td>
<td><?php echo $reports[$i]['dtr_in'] ?? '' ?></td>
<td><?php echo $reports[$i]['dtr_out'] ?? '' ?></td>
<td><?php echo $reports[$i]['dtr_in2'] ?? '' ?></td>
<td><?php echo $reports[$i]['dtr_out2'] ?? '' ?></td>
</tr>
<?php endfor; ?>

Related

Get total count for table row?

I Have an if Statement in the function is to make Row Red... if $ced (which is date value coming from MySQL) is lesser than the current time the code is given below: what I am looking for is how can I get total number of rows like 20,30 or whatever the value is and echo
<tr <?php if(($ced <= time())): ?> style="color:red;" <?php endif; ?>>
<td><?php echo $students_rollno_class;?></td>
<td><?php echo $students_admission_no;?> </td>
<td><?php echo $students_firstname;?></td>
<td><?php echo $students_contact;?></td>
<td><?php echo $students_reference_no;?></td>
<td><?php echo date('d/m/Y', strtotime($students_date));?></td>
<td><?php echo $newformat;?></td>
what I tried is
if(($ced <= time())){
$totalCount=$totalCount+1 ;}
the problem is its giving me a row numbers like 1,1,1,1,2,2,2,2,4,4,4,4 but i am looking for is single total count like 20
I have a box where I want to show total count the code is given below
<div class="info-box-content">
<span class="info-box-text"><?php echo "Course Ended"; ?></span>
<span class="info-box-number"><?php echo $totalCount; ?></span>
</div>
image for reference as the rows are changing to red and i want to show total count in the box
Loop i Created but with this its giving me value of 384 as per picture i have only 188 students
<?php
if(($ced <= time())){
$totalCount=$totalCount+1 ; ?>
<tr <?php if(($ced <= time())): ?> style="color:red;" <?php endif; ?>>
<td><?php echo $students_rollno_class;?></td>
<td><?php echo $students_admission_no;?> </td>
<td><?php echo $students_firstname;?></td>
<td><?php echo $students_contact;?></td>
<td><?php echo $students_reference_no;?></td>
<td><?php echo date('d/m/Y', strtotime($students_date));?></td>
<td><?php echo date('d/m/Y', $ced);?></td>
</tr><?php }}?>
<?php echo $totalCount;?>
and making all rows red
In order for $totalCount++ to do what you're looking for, it needs to be within some kind of a loop.
You also don't want to echo $totalCount++, you want to echo your variable $totalCount. This is what contains your total count. The ++ is what increments your value stored in $totalCount.
Can you please share the remainder of your code so we can see how you are trying to accomplish this?
Just getting directly in your sql request the data filtered by date $ced <= time()
Doing this, you will access directly to the total count without counter, just by calling count($array)

i want varable in two equal parts

fetching some data from mysqli
$value=mysqli_fetch_assoc($query)
I have a result like
$value = "123456789101112131415161718192021222324252627282930";
but I want to use it two variables like
$value1 = "123456789101112131415";
$value2 = "161718192021222324252627282930";
Use the database column names.
If you don't have the value you need in separate database column you have to somehow extract the value from an existing column.
You can use substr or regex to do so.
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['DATABASE_COLUMN_1_NAME']; ?></td>
<td><?php echo $row['DATABASE_COLUMN_2_NAME']; ?></td>
<td><?php echo $row['DATABASE_COLUMN_3_NAME']; ?></td>
<!-- show only a part of the db columns content -->
<?php $valueIwantToShow = subtr($row['DATABASE_COLUMN_4_NAME'], 0, 5); ?>
<td><?php echo valueIwantToShow; ?></td>
<!-- or show multiple values in one html column -->
<td><?php echo $row['DATABASE_COLUMN_6_NAME']; ?>, <?php echo $row['DATABASE_COLUMN_7_NAME']; ?></td>
</tr>
<?php } ?>

using get to retrieve value in php

I want to retrieve the information from a table to another using the GET function. but it's not working. please, what am i doing wrong?.
<td><?php echo $row['count(*)']; ?></td>
<td>N<?php echo number_format($row['sum(basic)'],2); ?></td>
<td>N<?php echo number_format($row['sum(hmo)'],2); ?></td>
<td>N<?php echo number_format($row['sum(dha)'],2); ?></td>
<td>N<?php echo number_format($row['sum(tax)'],2); ?></td>
<td>N<?php echo number_format($row['sum(netpay)'],2); ?></td>
<td><?php echo $row['year(date)'];?> </td>
</tr><?php }?>
the second page i want the the result to show in
$year = $_GET['year'];
$qry = "SELECT count(*), sum(basic), sum(hmo), sum(pension), sum(dha), sum(tax), sum(netpay), month(date) FROM salary WHERE year(date) ='$year' GROUP BY month(date)";
$run = mysql_query($qry) or die(mysql_error());
<?php while ($row = mysql_fetch_array($run)) {?>
<tr>
<td><?php echo $row['count(*)']; ?></td>
<td>N<?php echo number_format($row['sum(basic)'],2); ?></td>
<td>N<?php echo number_format($row['sum(hmo)'],2); ?></td>
<td>N<?php echo number_format($row['sum(dha)'],2); ?></td>
<td>N<?php echo number_format($row['sum(tax)'],2); ?></td>
<td>N<?php echo number_format($row['sum(netpay)'],2); ?></td>
<td><?php echo $row['month(date)'];?> </td>
</tr><?php }?>
This will work.
<td><?php echo $row['count(*)']; ?></td>
<td>N<?php echo number_format($row['sum(basic)'],2); ?></td>
<td>N<?php echo number_format($row['sum(hmo)'],2); ?></td>
<td>N<?php echo number_format($row['sum(dha)'],2); ?></td>
<td>N<?php echo number_format($row['sum(tax)'],2); ?></td>
<td>N<?php echo number_format($row['sum(netpay)'],2); ?></td>
<td>><?php echo $row['year(date)'];?> </td>
</tr><?php }?>
Reason: You were tried to pass value in month inside href. And tried to retrieve from get year parameter. Just change from month to year inside href. And you could able to retrieve year now. name parameter must to equal to get name parameter.
This is the URL in your link:
view_payroll_month.php?month=...
The query string parameter is called month. But you try to fetch a value called year:
$_GET['year']
Query string and/or form values are key/value pairs. The values are uniquely identified by the keys (names) with which they're associated. So you need to either:
Fetch the value by month instead of year, or
Pass the value year instead of month, or
Pass the value year in addition to month.
Something like this:
<a href="view_payroll_month.php?month=<?php echo $row['month(date)']; ?>&year=<?php echo $row['year(date)']; ?>">

php code to select sum and count of column

there are multiple column in my table ..
col1 col2 col3 price
500 700 100 10
501 700 100 20
502 700 100 30
503 700 100 10
4 70
I need to get count of col1 and display its sum.
and also display sum of price column...
But the main thing is i need to display this in last row....after all data...how can select this and echo in last row...
plz help me...
I need to echo exactly as i put the data in above table...
I need sql query and also need help to echo only sum of this two column in last row only......
SELECT *,IFNULL(col1,'SUM'), count(*) as count FROM coupon_entry WHERE Group By col1 WITH ROLLUP
<?php if (is_array($data)) { foreach($data as $row){ ?>
<tr>
<td><?php echo htmlspecialchars($row['col1']); ?></td>
<td><?php echo htmlspecialchars($row['col2']); ?></td>
<td><?php echo htmlspecialchars($row['col3']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
</tr>
<?php } }?>
One solution is to calculate the sum in PHP with variables :
<?php if (is_array($data)) {
$totalCol1 = 0;
$totalPrice = 0;
foreach($data as $row){
$totalCol1 += $row['col1'];
$totalPrice += $row['price'];
?>
<tr>
<td><?php echo htmlspecialchars($row['col1']); ?></td>
<td><?php echo htmlspecialchars($row['col2']); ?></td>
<td><?php echo htmlspecialchars($row['col3']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
</tr>
<?php }
<tr>
<td><?php echo $totalCol1;?></td>
<td></td>
<td></td>
<td><?php echo $totalPrice;?></td>
</tr>
}?>
Either you have to do 2 separate queries or else you have to do your calculations in PHP - either one is fairly simple although the PHP solution will probably be slightly (negligibly?) faster.
double-query:
$r = query('SELECT *
FROM yada-yada');
while ($row = fetch($r)) {
echo "<td>$row[col1]</td>\n";
echo "<td>$row[col2]</td>\n";
echo "<td>$row[col3]</td>\n";
echo "<td>$row[price]</td>\n";
}
$r2 = query('SELECT COUNT(*) as cnt, sum(col1) as sum_col1,
sum(price) as sum_price
FROM yada-yada...');
if ($row = fetch($r2)) {
echo "<td>$row['cnt']</td><td>$row['sum_col1']</td>...$row['sum_price']...\n";
}
calculate in PHP:
$cnt = $sum_col1 = $sum_price = 0;
$r = query('SELECT *
FROM yada-yada');
while ($row = fetch($r)) {
echo "<td>$row[col1]</td>\n";
echo "<td>$row[col2]</td>\n";
echo "<td>$row[col3]</td>\n";
echo "<td>$row[price]</td>\n";
$cnt++;
$sum_col1 += $row['col1'];
$sum_price += $row['price'];
}
echo "<td>$cnt</td><td>$sum_col1</td>...$sum_price...\n";
Below is my sql query to get count and sum of column.
SELECT COUNT(coupon) As Total,SUM(Price) AS TotalPrice FROM coupon_entry
And put this in seprate row below table...
<?php
foreach($tot as $tota)
?>
<tr style="background-color:#33CCFF;">
<td colspan="2" style="text-align:right; font-weight:bold;">Total Coupon</td>
<td><?php echo $tota['Total'];?></td>
<td style="text-align:right; font-weight:bold;">Total Amount in Rs</td>
<td><?php echo $tota['TotalPrice'];?></td>
</tr>
<?php }?>

sum all value in foreach loop php

<?php foreach((array)$query as $row):?>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
<?php endforeach ;?>
I want to sum all values in $row->cost when $row->date=02-01-2016, like this in a table
Thanks.
echo array_sum(array_filter(array_map(function($row){
if($row->date == '02-01-2016')
return $row->cost;
}, $array)));
You can use
array_map: to compare and skip the values for which cost is not needed to add in sum.
array_filter: and then to remove the blank indexes
array_sum: and finally to sum cost
Note: if you are getting results from database, you can simply do sum there (see below):
$db->query("SELECT SUM(costs) FROM table_name WHERE `date`='2016-01-02'");
echo $db->fetchColumn();
Try below code:
<?php
$total = 0;
foreach((array)$query as $row):
$date = $row->date;
$cost = $row->cost;
if(date("M-d-Y", strtotime($date))==date("M-d-Y")) {
$total = $total+$cost;
}
?>
<td><?php echo $date ?></td>
<td><?php echo $cost ?></td>
<?php endforeach; ?>
<td colspan="2"><?php echo $total; ?></td>
<?php
$tot = 0;
foreach( (array)$query as $row ) {
if( $row->date=='02-01-2016' ) $tot = $tot+$row->cost;
?>
<tr>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
</tr>
<?php } ?>
<tr><td colspan="2"><?php echo $tot; ?></td></tr>
obviously, the comparison works if $row->date is formatted as dd-mm-yyyy
<table border=1>
<?php
$tot = 0;
foreach($query as $row ) {
if( $row->date=='2016-01-02' ) $tot += $row->cost;
?>
<tr>
<td><?php echo $row->date ?></td>
<td><?php echo $row->cost ?></td>
</tr>
<?php } ?>
<tr><td></td><td><?php echo $total; ?></td></tr>
</table>
It will work for you exctly what u want.

Categories