Get total count for table row? - php

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)

Related

Php Loop TD if rows didn't exist or match

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; ?>

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 } ?>

How to use another loop inside for loop in JSON in API use php

here is my php code in which i want some more configurations.....
<?php
for ($i=0; $i < sizeof($json['TrainRoute']); $i++) { ?>
<tr>
<td><?php echo $json['TrainRoute'][$i]['SerialNo']; ?></td>
<td><?php echo $json['TrainRoute'][$i]['StationName']; ?></td>
<td><?php echo $json['TrainRoute'][$i]['ScheduleArrival']; ?> / <?php echo $json['TrainRoute'][$i]['ScheduleDeparture']; ?></td>
<td class="train-mobile"><?php echo $json['TrainRoute'][$i]['ActualArrival']; ?> / <?php echo $json['TrainRoute'][$i]['ActualDeparture']; ?></td>
<td class="train-mobile"><?php echo $json['TrainRoute'][$i]['Day']; ?></td>
<td style="color: red;"><?php echo $json['TrainRoute'][$i]['DelayInDeparture']; ?></td>
</tr>
<?php } ?>
In the above code, i want to insert the following condition..
<td style="color: green;">
<?php
if ( $json['TrainRoute']['StationName'] = $json['CurrentStation']['StationName'] ) {
echo $json['CurrentStation']['StationName'];
}
?>
</td>
So that when i display, below code comes with the above code but the below code will display only once.
When I simply paste below code in above code, below code display with every repetition of above code, but I want it to display only once at the current position.
How can I solve this?
Within your if statement, you are assigning a value to the array this will always evaluate to true, and thus the if statement will never fail;
$json['TrainRoute']['StationName'] = $json['CurrentStation']['StationName']
This is because you're using a single equals
It should be a double or triple equals for comparison, or strict comparison respectively;
$json['TrainRoute']['StationName'] == $json['CurrentStation']['StationName']

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)']; ?>">

How to make a PHP if statment dependent on SQL query results

I am trying to echo out the user level depending on whether the user level is either 1 or 5 based on SQL data results. Here:
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<tr>
<td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
<td><?php echo $rrows['id']; ?></td>
<td><?php echo $rrows['date']; ?></td>
<td><?php echo $rrows['user_name'];?></td>
<td><?php echo $rrows['user_email']; ?></td>
<td><?php ?></td>
So I need a sort of if statement to select the user level from $rrows['id'] then if that selected data is 1 echo out "Network" and if it is "5" echo out "Administrator". How can this be done?
Seems like you need something like that:
$user_levels = array('Network','role2','role3','role4','Administrator');
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<tr>
<td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
<td><?php echo $rrows['id']; ?></td>
<td><?php echo $rrows['date']; ?></td>
<td><?php echo $users_levels[(int)$rrows['id']-1]; ?></td>
<td><?php echo $rrows['user_name'];?></td>
<td><?php echo $rrows['user_email']; ?></td>
<td><?php ?></td>
The reason I am using -1 in the array is because the array is 0 based and your roles start with 1.
If you need to use the 'user_level' simply replace the row with
<td><?php echo $users_levels[(int)$rrows['user_level']-1]; ?></td>
Hope this helps!
I wonder how you got that far without an if statement but anyway, you also could do this right in you sql query. I always try to outsource as much logic to mysql as possible for a balanced load distribution.
SELECT level, IF(level = 1, 'Network', 'Administrator') as level FROM table
I think you can nest if statements to have more options.
Adapted from this answer: 'IF' in 'SELECT' statement - choose output value based on column values
I wouldn't do it with a if statement. I'd do it like that:
$levels = array(1 => "Network", 5 => "Administrator");
echo $levels[$rrows['user_level']];
That way, if you want to add other levels, you just add a value to the array and that's it.

Categories