How to highlight rows in php table? - php

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>

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

Highlighting table rows based on conditional formatting of sql query

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.

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>';
}

PHP Mysql_fetch_assoc is Omitting last row

In PHP MYSQL_FETCH_ASSOC is omitting Last Row. This never happened. But this time it put me into soup at the last moment.
Even I've put up mysql_num_rows the result is 14 records -- but on list it shows only 13 records and the 14th record is omitted.
Any kind of help is Appreciated.
$uno = $_GET["uno"];
$xtc1 = 'select * from rform where uno="' . $uno . '" order by rno DESC';
$xtc = mysql_query($xtc1) or die('User Reservation Retrival Error : ' . mysql_error());
$trno = mysql_fetch_assoc($xtc);
$trow = mysql_num_rows($xtc);
echo '<p>List of Onlilne Reservations made by <strong style="font-weight:bold; color:red;">' . ucwords($trno["cname"]) . ' (' . $trow . ')</strong></p>';
echo '<table cellpadding="5" cellspacing="0" border="1">';
echo '<tr>';
echo '<td colspan="5" style=" font-size:14px; text-align:center; font-weight:bold; color:red;">' . ucwords($trno["cname"]) . '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>R.NO</th>';
echo '<th>From</th>';
echo '<th>To</th>';
echo '<th>Date & Time of<Br>Travel</th>';
echo '<th>Reserved On</th>';
echo '</tr>';
while($mtn = mysql_fetch_assoc($xtc)){
$dt = $mtn["csdate"] . ' ' . $mtn["ctime"];
echo '<tr>';
echo '<td>' . $mtn["rno"] . '</td>';
echo '<td>' . $dt . '</td>';
echo '<td>' . $mtn["caddr"] . '</td>';
echo '<td>' . $mtn["cdest"] . '</td>';
echo '<td>' . date('d-M-Y',strtotime($mtn["tstamp"])) . '</td>';
echo '</tr>';
}
echo '</table>';
You have an extra $trno = mysql_fetch_assoc($xtc) that you sem to be discarding. This is your missing row. Just remove that line.
deleting the first $trno = mysql_fetch_assoc($xtc); will solve this problem.
In case you need to read the first line of $xtc before the loop. You can change while loop to do-while, without deleted the first $mtn = mysql_fetch_assoc($xtc);
do{
//whatever
}while($mtn = mysql_fetch_assoc($xtc));

Categories