Highlighting table rows based on conditional formatting of sql query - php

EDITED
I have an sql query and I want to highlight the table row with a red highlight so that if the transaction_date = 0000-00-00 that whole row is highlighted with red. Here is the snippet of the code. The transaction_date shows the date that a specific training course was taken and if the course wasn't taken then the date that will be returned will be 0000-00-00. Any help would be greatly appreciated. Thank you.
$sql = "SELECT DATE_FORMAT(date_fluids, '%m/%d/%Y ') AS transaction_date, first_name, last_name, supervisor_group, trade, t_id, c_id, department_number, date_fluids, fluids_refresh, fluid_period, employee_type
FROM peopleinfo WHERE
(last_name LIKE '%$value1%' )
and
(trade LIKE '%$value2%')
and
(t_id LIKE '%$value3%')
and
(c_id LIKE '%$value4%')
and
(department_number LIKE '%$value5%')
and
(date_fluids LIKE '%$value6%')
and
(date_lock LIKE '%$value7%')
and
(date_confine LIKE '%$value8%')
and
(date_fall LIKE '%$value9%')
and
(supervisor_group LIKE '%$value10%')
AND
(employee_type='1')
ORDER BY last_name ASC";
$query = mysqli_query($conn, $sql);
$row = (mysqli_fetch_array($query));
if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
while ($row = mysqli_fetch_array($query))
{
if ($row['transaction_date'] == 0000-00-00){
echo '<tr class="highlight">';
echo '<td >' . $row['first_name']. '</td>';
echo '<td >' . $row['last_name']. '</td>';
echo '<td >' . $row['supervisor_group']. '</td>';
echo '<td >' . $row['trade']. '</td>';
echo '<td >' . $row['t_id']. '</td>';
echo '<td >' . $row['c_id']. '</td>';
echo '<td >' . $row['department_number']. '</td>';
echo '<td >' . $row['transaction_date']. '</td>';
echo '<td >'. $row['fluid_period']. '</td>';
echo '</tr>';
} else {
echo '<tr >';
echo '<td >' . $row['first_name']. '</td>';
echo '<td >' . $row['last_name']. '</td>';
echo '<td >' . $row['supervisor_group']. '</td>';
echo '<td >' . $row['trade']. '</td>';
echo '<td >' . $row['t_id']. '</td>';
echo '<td >' . $row['c_id']. '</td>';
echo '<td >' . $row['department_number']. '</td>';
echo '<td >' . $row['transaction_date']. '</td>';
echo '<td >'. $row['fluid_period']. '</td>';
echo '</tr>';
}
}
mysqli_free_result($query);
mysqli_close($conn);
?>
</tbody>
</table>

I would just do:
echo '<tr' . ($row['transaction_date'] === '0000-00-00' ? 'class="highlight"' : '') . '>
and then use css to highlight it however you want.

Related

How to send a variable value with HREF in PHP

i have one table in locallhost ,In my table there is an item id,Now I want to send the id Related row to another page by clicking on EDIT . Thankful
The part of the program I think is difficult to write below
....
....
$response["travel"]=array();
while ($row = mysql_fetch_array($sql)) {
// Print out the contents of the entry
echo '<tr>';
echo '<td class="text-center">' . $i . '</td>';
echo '<td class="text-center">' . $row['companyname'] . '</td>';
echo '<td class="text-center">' . $row['cod'] . '</td>';
echo '<td class="text-center">' . $row['bigan'] . '</td>';
echo '<td class="text-center">' . $row['stop'] . '</td>';
echo '<td class="text-center">' . $row['date'] . '</td>';
echo '<td class="text-center">' . $row['time'] . '</td>';
echo '<td class="text-center">' . $row['price'] . '</td>';
echo '<td class="text-center">' .'EDIT' .'</td>';
$i++;
.......
.....
You don't use <?php inside strings, you use that when you've gone back into HTML mode. You should just concatenate the variable, like you do everywhere else
echo '<td class="text-center">' .'ویرایش' .'</td>';

html/php sql query where clause

This code works but the one belows shows an error
if(isset($_POST['month'])=='')
{
$sql = ('SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM 2017_oct_land
WHERE taxability= "T"
group by District ASC');
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
--with AND-- why do i get this "Warning: Invalid argument supplied for foreach() in C:\wamp64\www\reportview\pages\code2.php on line 20" with this code?
if(isset($_POST['month'])!=NULL)
{
$kind = $_POST['kind'];
echo "MONTH : ".$kind."<br/>";
$sql = ('SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM julcons
WHERE taxability="T" and actualuse like "' . $kind .'"');
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
Here you go ( untested ) but should be close
if(isset($_POST['kind'])) {
//why check month and then use kind? which is it..
//isset returns boolean, not null, null works because it's false but it's not correct IMO
$kind = $_POST['kind']; //what is this kind or month
echo "MONTH : ".$kind."<br/>";
$sql = 'SELECT
substring(pin,5,2) as District,
count(arpn) as RPU,
sum(area) as AREA,
sum(marketvalue) as MV,
sum(assessedvalue) as AV
FROM julcons
WHERE taxability="T" and actualuse like :kind';
$stmt = $pdo->prepare($sql);
$stmt->execute([':kind' => $kind]);
while( false !== ( $row = $stmt->fetch(PDO::FETCH_ASSOC))){
echo '<tr>';
echo '<td>'. $row['District'] . '</td>';
echo '<td class="RPU">'. $row['RPU'] . '</td>';
echo '<td class="AREA">'. $row['AREA'] . '</td>';
echo '<td class="MV">'. $row['MV'] . '</td>';
echo '<td class="AV">'. $row['AV'] . '</td>';
}
include 'total.php';
}
Sorry, but you had some really big mistakes in your code.

my code cant display the picture from db?

Please help cant display image when I use php
$result = mysql_query("SELECT * FROM obs") or die(mysql_error());
echo "<center><table><tr><th width=200>Obs Number</th><th width=200>Name</th><th width=200>Purpose</th><th width=200>Date Of OB</th><th width=200>Destination</th><th width=200>Image</th></tr></center>";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
//cant display image
echo '<tr> <input type="hidden" name="showimage[]" value="'. $row['OBS_NUMBER'].'" >';
echo '<td width=200>' . $row['OBS_NUMBER'] . '</td>';
echo '<td width=200>' . $row['NAME'] . '</td>';
echo '<td width=200>' . $row['PURPOSE'] . '</td>';
echo '<td width=200>' . $row['DATE_OF_OB'] . '</td>';
echo '<td width=200>' . $row['DESTINATION'] . '</td>';
echo '<td width=200> <img src="data:image/jpeg;base64,' . base64_encode($row['imagesobs']) . '" width="250"></td>';
echo '</tr>';
}

Get Similar rows count in mysql

I am working on a project in which a tutor can save its class timing. He can see his timing according to the days. I used the code
$qry = mysqli_query($con, 'select * from users left join time_slot on users.id=time_slot.u_id where users.id=' . $id);
echo '<table class="table_class" border="2">
<tr>
<th class="details">id</th>
<th class="details">Date</th>
<th class="details">start time</th>
<th class="details">End Time</th>
</tr>';
while ($row = mysqli_fetch_array($qry)) {
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';
But It show the multiple time if a tutor have multiple class in same day.
I want to show if he has 2 or more class on similar day(Monday) then all time slot show in a single row. Same this for all days of the week. How can I do it?
You can use GROUP_CONCAT function for this. Assuming your ddl is something like that
create table users(id bigint, name varchar(50));
create table time_slot(id bigint, u_id bigint, day datetime, time_from time, time_to time);
the sql would be as follows:
select u.id,u.name, ts.day,
group_concat(ts.time_from, ' - ', ts.time_to ORDER BY ts.time_from, ts.time_to)
from users u left outer join time_slot ts on u.id = ts.u_id
group by u.id, u.name, ts.day
order by u.name, ts.day
See fiddle.
I have did with some temp values.
if you want in same way to impliment then it is usefull for you.
copy the code and check here http://phpfiddle.org/
$obj1['id']='1';
$obj1['name']='a1';
$obj1['day']='asdadh';
$obj1['time_from']='1';
$obj1['time_to']='1';
$obj2['id']='2';
$obj2['name']='a2';
$obj2['day']='asdad';
$obj2['time_from']='1';
$obj2['time_to']='1';
$obj3['id']='3';
$obj3['name']='a2';
$obj3['day']='asdad';
$obj3['time_from']='1';
$obj3['time_to']='1';
$arr = Array();
$arr[]=$obj1;
$arr[]=$obj2;
$arr[]=$obj3;
echo '<table class="table_class" border="2">';
echo '<tr>';
echo '<th class="details">id</th>';
echo '<th class="details">name</th>';
echo '<th class="details">day</th>';
echo '<th class="details">start time</th>';
echo '<th class="details">End Time</th>';
echo '</tr>';
foreach($arr as $row)
{
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';
echo "<br><br><br><br><br><br><br>";
$dates=Array();
$count=0;
foreach($arr as $id=>$row){
$val = $row['day'];
$key = array_search($val,$dates);
if(is_numeric($key)){
$arr[$key]['day']=$dates[$key].','.$val;
unset($arr[$id]);
}else{
$dates[$count]=$val;
}
$count++;
}
// new table
echo '<table class="table_class" border="2">';
echo '<tr>';
echo '<th class="details">id</th>';
echo '<th class="details">name</th>';
echo '<th class="details">day</th>';
echo '<th class="details">start time</th>';
echo '<th class="details">End Time</th>';
echo '</tr>';
foreach($arr as $row)
{
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';

How to highlight rows in php table?

I have table called reservations. It displays reservations made by users. I want highlight records in current date using end date.
Php code
$result2 = mysql_query("SELECT * FROM reservations WHERE hotel_id = '1' ORDER BY end");
while ($row = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td class="contacts">' . $row['fname'] . ' ' . $row['lname'] . '</td>';
echo '<td class="contacts">' . $row['start'] . '</td>';
echo '<td class="contacts">' . $row['end'] . '</td>';
echo '<td class="contacts">' . $row['qty'] . '</td>';
echo '</td>';
echo '<td class="contacts">' . $row['room_type'] . '</td>';
echo '<td class="contacts">' . '<a href=out.php?id=' . $row["res_id"] . '>' . 'Check Out' . '</a>' . '</td>';
echo '</tr>';
}
I'd do that at frontend side, but if you want to compute than in PHP, you will need to compare the date from $row and current date and add a class or a style tag to the .
Like so:
$rowHasCurrentDate = $row['date'] == date('Y-m-d');
echo '<tr' + ($rowHasCurrentDate ? ' class="highlight-row"' : "") + '>';
instead of
echo '<tr>';
You can replace the 'class="highlight-row"' with 'style="background-color:red"' if you want to do it without touching frontend side at all.
The example is kinda spaghetti-code, but you can move this logic somewhere else after you get it working.
I am assuming that your start date is current date. This is css thing, i have given you solution to you.
<html>
<head>
<style>
.currdate
{
background-color:red; //change this color to whatever you wish to change to
}
</style>
</head>
<body>
<?php
$result2 = mysql_query("SELECT * FROM reservations WHERE hotel_id = '1' ORDER BY end");
while ($row = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td class="contacts">' . $row['fname'] . ' ' . $row['lname'] . '</td>';
echo '<td class="contacts currdate">' . $row['start'] . '</td>';
echo '<td class="contacts">' . $row['end'] . '</td>';
echo '<td class="contacts">' . $row['qty'] . '</td>';
echo '</td>';
echo '<td class="contacts">' . $row['room_type'] . '</td>';
echo '<td class="contacts">' . '<a href=out.php?id=' . $row["res_id"] . '>' . 'Check Out' . '</a>' . '</td>';
echo '</tr>';
}
?>
<body>
</html>

Categories