How to send a variable value with HREF in PHP - 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>';

Related

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.

Unable to print picture after request

What´s is wrong on this sentence?
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['u_id'] . '</td>';
echo '<td>'. $row['u_role'] . '</td>';
echo '<td>'. $row['u_name'] . '</td>';
echo '<td>'. $row['u_passw'] . '</td>';
echo '<td>'. $row['u_init'] . '</td>';
echo '<td>'. $row['c_name'] . '</td>';
echo '<td>'. $row['u_mail'] . '</td>';
echo '<td>'.'<img href="'. $row['u_pic'] . '" width=45 height=45></img>'.'</td>';`
}
I´m able to print all fields after query but for last one is not possible, I got on db saved the url for the picture with this format
http://localhost/Pics/st.gif dbfield as varchar
If I inspect the component in the browser I can see it was properly selected by mysql but there is not print on the screen.
Any comment? Thanks in advance
For images you'll have to use:
<img src="url">
echo '<td><img src="'. $row['u_pic'] . '" width=45 height=45></img></td>';
It should be in following way,
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['u_id'] . '</td>';
echo '<td>'. $row['u_role'] . '</td>';
echo '<td>'. $row['u_name'] . '</td>';
echo '<td>'. $row['u_passw'] . '</td>';
echo '<td>'. $row['u_init'] . '</td>';
echo '<td>'. $row['c_name'] . '</td>';
echo '<td>'. $row['u_mail'] . '</td>';
echo '<td>'.'<img src="'. $row['u_pic'] . '" width=45 height=45></img>'.'</td>';
}

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

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>

Print out only the result set that has an "a" at the end of a certain value PHP?

I have a foreach loop that iterates over objects in an array. Currently, it outputs information for every element. I need to print out data for only the objects that have an "a" at the end of their id property. Code is as follows:
foreach ($obj as $key => $value) {
echo '<tr id="subRow">';
echo '<td>' . $value->date . '</td>';
echo '<td class="leftLine">' . $value->Name . '</td>';
echo '<td class="leftLine">' . $value->dealType . '</td>';
echo '<td class="leftLine">' . $value->id . '</td>';
echo '<td class="leftLine">' . 'Adobe PDF' . '</td>';
echo '</tr>';
}
So for every element of the $obj that contains an ID number with an "a" at the end of it, print out that entire element.
substr can help with that:
foreach ($obj as $key => $value) {
if(substr($value->id, -1) === 'a') {
echo '<tr id="subRow">';
echo '<td>' . $value->date . '</td>';
echo '<td class="leftLine">' . $value->Name . '</td>';
echo '<td class="leftLine">' . $value->dealType . '</td>';
echo '<td class="leftLine">' . $value->id . '</td>';
echo '<td class="leftLine">' . 'Adobe PDF' . '</td>';
echo '</tr>';
}
}

Categories