Checkbox selection in table - php

I've seen similar questions on here but the code is different when I try to implement it. I'm using php and html to connect to my db2 database, pull data from a table and display the pulled data in an html table. I'm trying to add functionality to my 'delete' button and would like to be able to mark a check-box that would highlight the selected row in the table. I'm working on my delete.php but for now I would like to implement this row selection. I found a line of code that I put at the start of my table, but how do I highlight the row? Here's what I have so far:
<html>
<head><title>DB Testing</title></head>
<style>
table{
font-family: arial, sans-serif;
border-collapse: collapse;
width: 80%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
<body>
<?php
//db2 express c (v10.5) in local
$database = "db";
$user = "user";
$password = "password";
//create connection
$conn = db2_connect($database, $user, $password);
//check connection
if($conn) {
//echo "DB2 Connection succeeded.<br><br>";
} else{
exit("failed".db2_conn_errormsg());
}
//select fields from database
$sql = "select 'JUNK', A, B, START_TIME, END_TIME, START_DAY,
END_DAY, C, ID, BUSINESS_AREA, ENVIRONMENT from testtable where A='D'
and AVAILABILITY = 'Y'";
$stmt = db2_prepare($conn, $sql);
//db2_execute executes a sql statement that was prepared by db2_prepare
if($stmt){
$result = db2_execute($stmt);
if(!$result){
echo "exec errormsg: " .db2_stmt_errormsg($stmt);
}
//the echos below output the data in an html table
echo '<table border = 1>';
echo '<thead>';
echo '<tr>';
echo'<th></th>';
echo'<th>A</th>';
echo'<th>B</th>';
echo'<th>START_TIME</th>';
echo'<th>END_TIME</th>';
echo'<th>START_DAY</th>';
echo'<th>END_DAY</th>';
echo'<th>C</th>';
echo'<th>ID</th>';
echo'<th>BUSINESS_AREA</th>';
echo'<th>ENVIRONMENT</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while($row = db2_fetch_assoc($stmt)) {
echo '<tr>';
echo "<td><input name=\"checkbox[]\" type=\"checkbox\" value=\"".$rows['']. "\" /></td>";
echo '<td>' . $row['A'] . '</td>';
echo '<td>' . $row['B'] . '</td>';
echo '<td>' . $row['START_TIME'] . '</td>';
echo '<td>' . $row['END_TIME'] . '</td>';
echo '<td>' . $row['START_DAY'] . '</td>';
echo '<td>' . $row['END_DAY'] . '</td>';
echo '<td>' . $row['C'] . '</td>';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['BUSINESS_AREA'] . '</td>';
echo '<td>' . $row['ENVIRONMENT'] . '</td>';
echo '</tr>';
echo '</tbody>';
}
echo '</table>';
}else {
echo "exec errormsg: ".db2_stmt_errormsg($stmt);
}
db2_close($conn);
?>
<?php
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
?>
</body>
</html>

You'll need some javascript to do the trick, see the snippet:
function hl(ob){
if(ob.checked == true){
ob.parentNode.parentNode.style.backgroundColor='red';
}
if(ob.checked != true){
ob.parentNode.parentNode.style.backgroundColor='white';
}
}
<table border=1 cellspacing=0 cellpadding=5>
<tr>
<td><input type="checkbox" name="checkbox" value="1" onClick="hl(this)" /></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox" value="2" onClick="hl(this)" /></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox" value="3" onClick="hl(this)" /></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>
EDIT
<html>
<head>
<title>DB Testing</title>
<script type="text/javascript">
function hl(ob){
if(ob.checked == true){
ob.parentNode.parentNode.style.backgroundColor='red';
}
if(ob.checked != true){
ob.parentNode.parentNode.style.backgroundColor='white';
}
}
</script>
<style>
table{
font-family: arial, sans-serif;
border-collapse: collapse;
width: 80%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
</head>
<body>
<?php
//db2 express c (v10.5) in local
$database = "db";
$user = "user";
$password = "password";
//create connection
$conn = db2_connect($database, $user, $password);
//check connection
if($conn) {
//echo "DB2 Connection succeeded.<br><br>";
} else{
exit("failed".db2_conn_errormsg());
}
//select fields from database
$sql = "select 'JUNK', A, B, START_TIME, END_TIME, START_DAY,
END_DAY, C, ID, BUSINESS_AREA, ENVIRONMENT from testtable where A='D'
and AVAILABILITY = 'Y'";
$stmt = db2_prepare($conn, $sql);
//db2_execute executes a sql statement that was prepared by db2_prepare
if($stmt){
$result = db2_execute($stmt);
if(!$result){
echo "exec errormsg: " .db2_stmt_errormsg($stmt);
}
//the echos below output the data in an html table
echo '<table border = 1>';
echo '<thead>';
echo '<tr>';
echo'<th></th>';
echo'<th>A</th>';
echo'<th>B</th>';
echo'<th>START_TIME</th>';
echo'<th>END_TIME</th>';
echo'<th>START_DAY</th>';
echo'<th>END_DAY</th>';
echo'<th>C</th>';
echo'<th>ID</th>';
echo'<th>BUSINESS_AREA</th>';
echo'<th>ENVIRONMENT</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while($row = db2_fetch_assoc($stmt)) {
echo '<tr>';
echo "<td><input name=\"checkbox[]\" type=\"checkbox\" value=\"".$rows['']. "\" onClick=\"hl(this)\" /></td>";
echo '<td>' . $row['A'] . '</td>';
echo '<td>' . $row['B'] . '</td>';
echo '<td>' . $row['START_TIME'] . '</td>';
echo '<td>' . $row['END_TIME'] . '</td>';
echo '<td>' . $row['START_DAY'] . '</td>';
echo '<td>' . $row['END_DAY'] . '</td>';
echo '<td>' . $row['C'] . '</td>';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['BUSINESS_AREA'] . '</td>';
echo '<td>' . $row['ENVIRONMENT'] . '</td>';
echo '</tr>';
echo '</tbody>';
}
echo '</table>';
}else {
echo "exec errormsg: ".db2_stmt_errormsg($stmt);
}
db2_close($conn);
?>
<?php
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
?>
</body>
</html>

Related

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

Looking to to determine Elapsed Time in PHP

I am working on a project where I am determining the time between two times. The times are in the format HH:MM AM/PM. How would you convert this to an elapsed time in hours and minutes?
Here is the code below.
<?php
$flightcount = 0;
$pdo = Database::connect();
$pdo1 = Database::connect();
$sql = "SELECT * FROM FlightBasic ORDER BY STR_TO_DATE(DepartTime, '%l:%i %p')";
$sql2 = "SELECT * FROM Connections";
$Org = $_GET["origin"];
$Dest = $_GET["destination"];
$DDate = $_GET["ddate"];
$ADate = $_GET["adate"];
$sql2 = "SELECT * FROM connections";
foreach ($pdo->query($sql) as $row) {
if($row["Date"] == $DDate){
if($row["Date"] == $DDate){
foreach ($pdo1->query($sql2) as $row2) {
$conn = $row2["Connection"];
if($row2["Origin"]==$Org && $row2["Destination"]==$Dest){
if($row["Origin"]==$Org && $row["Destination"]==$conn){
$fldpt = $row["DepartTime"];
$flarr = $row["ArriveTime"];
$flnum = $row["FlightNumber"];
$sql3 = "";
echo
$fltr = $row2["TravelTime"];
foreach ($pdo->query($sql) as $row) {
if($row["Origin"]==$conn && $row["Destination"]==$Dest){
echo '<tr style="color:black; height:20px; background-color:#95AFB8">';
echo '<td align="center">' . $fldpt . '</td>';
echo '<td align="center" >' . $flarr . '</td>';
echo '<td align="center" >' . $flnum . '</td>';
echo '<td align="center" valign="middle" rowspan="2" width="108">1 Stop: <br> Stops In ' . $row2['Connection'] . '</td>';
echo '<td align="center" valign="center" rowspan="2">' . $fltr . '</td>';
echo '</tr>';
echo '<tr style="color:black; height:20px; background-color:#DFEBF0">';
echo '<td align="center" width="82">' . $row['DepartTime'] . '</td>';
echo '<td align="center" width="84">' . $row['ArriveTime'] . '</td>';
echo '<td align="center" width="94">' . $row['FlightNumber'] . '</td>';
echo '</tr>';
}
}
}
}
}
}
if($row["Origin"]==$Org && $row["Destination"]==$Dest){
echo '<tr style="color:black; height:40px; background-color:#95AFB8">';
echo '<td align="center">' . $row['DepartTime'] . '</td>';
echo '<td align="center">' . $row['ArriveTime'] . '</td>';
echo '<td align="center">' . $row['FlightNumber'] . '</td>';
echo '<td align="center">Nonstop</td>';
echo '<td align="center">' . $row['TravelTime'] . '</td>';
echo '</tr>';
}
}
}
Database::disconnect();
?>
Thanks for the help!
Here is a simple code calculating difference between times. Your code has nothing clear on what you really want, therefor, transform this according to your needs.
<?php
$first = new DateTime( Your time );
$second = new DateTime( Another time );
$diff = $first->diff( $second );
This prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
echo $diff->format( '%H:%I:%S' ); // -> Difference
?>
The A (or a) shows the AM / PM format. More details in PHP documentation http://php.net/manual/en/function.date.php

Checking and Returning values from database

I have this little issue:
I want to retrieve some rows from the database, it retrieves fine, but when i insert a particular value in the text field which is not present in the database to track, it returns an empty/blank page, below is the code i used:
<form id="track" name="track" method="post" action="track_now.php">
<h2>Track your shipment Here</h2>
<p><label> Tracking Reference:
<input type="text" id="reference" name="reference" value="" maxlength="40" required="required" /></label></p>
<div class="button_holder">
<p> <input type="submit" id="track" value="Track Now" maxlength="40" required="required" /></label>
</label></p>
</div>
</form>
and this is the track_now.php
<form id="track" name="track" method="post" action="">
<h2>Your Shipment Result</h2>
<?php
//error_reporting(0);
$ref = mysql_real_escape_string($_POST['reference']);
// conmnecting to the database
if(isset($ref))
{
$db = mysql_connect('localhost', 'admin', "admin") or die(mysql_error("Cannot Connect to Database"));
mysql_select_db('tracking') or die(mysql_error());
$sql = "SELECT * FROM order_tracking WHERE ship_ref = '".$ref."' ";
$rs = mysql_query($sql);
if($row = mysql_fetch_array($rs)) {
echo '<table width="518" border="1";>';
echo '<tr>';
echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Reference: </td>';
echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">' . $row['ship_ref'] . "<br />" . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Type: </td>';
echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">' . $row['ship_type'] . "<br />" . '</td>';
echo '</tr>';
}
echo "</table>";
}
else if ($rs != $row) {
print 'Invalid Tracking Number, Please click here to try again' ;
}
mysql_close();
?>
Please, what am i doing wrong here?
Your condition could be simplified, try this way:
if(isset($ref)){
$db = mysql_connect('localhost', 'admin', "admin") or die(mysql_error("Cannot Connect to Database"));
mysql_select_db('tracking') or die(mysql_error());
$sql = "SELECT * FROM order_tracking WHERE ship_ref = '".$ref."' ";
$rs = mysql_query($sql);
if(!rs){
die(mysql_error());
}
if($row = mysql_fetch_array($rs)) {
echo '<table width="518" border="1";>';
echo '<tr>';
echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Reference: </td>';
echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">' . $row['ship_ref'] . "<br />" . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Type: </td>';
echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">' . $row['ship_type'] . "<br />" . '</td>';
echo '</tr>';
echo "</table>";
}
mysql_close();
}
else{
print 'Invalid Tracking Number, Please click here to try again' ;
}
because else if ($rs != $row) will have undefined value if first condition is not met.
As #marco pointed out, you can check for rows without fetching:
if(mysql_num_rows($rs) > 0){
//found a row
}

The Next iteration

I'm having some display problems here.
I have a "backend.php" file where I ask for two inputs.
These inputs are processed by "Addproducts.php" file and this file redirects to backend.php.
Backend.php also shows the current records in the database.
Here's the code for backend.php
<html>
<head>
<title>Inventory - Backend</title>
</head>
<body>
<form action="addproducts.php" method="post">
<table>
<tr>
<td>Product Name : </td>
<td><input type="text" name="pname"/></td>
</tr>
<tr>
<td>Product Quantity : </td>
<td><input type="text" name="productq"/></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="Add Product"/></td>
</tr>
</table>
</form>
<h2>Current Products</h2>
<?php
$db = mysql_connect('127.0.0.1', 'root', '') or die ('Unable to Connect.Check your connection parameters');
mysql_select_db('stock_inventory', $db) or die (mysql_error($db));
$query = 'SELECT * FROM PRODUCTS';
$result = mysql_query($query, $db) or die (mysql_error($db));
echo '<table>';
echo '<tr>';
echo '<th>Product ID </th>';
echo '<th>Producr Name </th>';
echo '<th>Product Stock </th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
echo '<br/>';
echo '</table>';
}
else
{
echo "No products in the database";
}
}
?>
</body>
</html>
It displays something like this :-
Product ID Producr Name Product Stock
1 NewProduct 1
2HTC One5
3Samsung10
4Sony10
You see?
Only the first product is aligned, the rest are not.
How do I make them all align ?
Thanks.
The reason is you are closing your table tag within the loop, move it outside the loop like follows:
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
else
{
echo "No products in the database";
}
}
echo '<br/>';
echo '</table>';
Update: A better fix (see Barmar's comment below):
if (empty(mysql_num_row($result))) {
echo "<tr><td colspan='3'>No products in the database</td></tr>";
} else {
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
}
echo '</table>';
Also start looking into using mysqli(http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), mysql_ is deprecated!
I would suggest removing the
<br/>
from within your table. I believe that putting markup like line breaks inside of table markup will break the layout.

php linebreak causes string value to disappear

I have some php querying and printing the info of a mysql table on a medical recruitment website.
Sometimes the users enter, causing line breaks where I would rather not have them, because
it affects the layout of the rendered HTML table in a way that it shouldn't.
I can trim the strings when they fill in the forms and the data get written into the table, but there's already a lot of data there, so i decided to remove the line breaks on the page
where php reads and prints the results.
i declared one of the strings where this causes trouble as such:
$specialtr = trim($special, "\n");
and then where the html table gets rendered:
echo '<td width="150">' . $specialtr . '</td>';
Here is the whole code:
All the code works, it's only the parts above that I added that now causes the column with $specialtr variable not to print/print as empty.
<form action="selected.php" method="POST">
<table border="1" cellpadding="2" cellspacing="2" style="width: 98%; margin-left: auto; margin-right: auto;">
<tr>
<td>
<?php
$dbhost = 'xxx';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error($conn));
mysql_select_db($dbname) or die(mysql_error($conn));
$specialtr = trim($special, "\n");
echo '<table border="1" cellpadding="2" cellspacing="2" style="margin-left: auto; margin-right: auto;">';
$query = 'SELECT userid FROM employee
ORDER BY userid ASC';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
$num_entries = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $value) {
echo '<tr>';
echo '<td><input type="radio" name="employee" value="' . $row['userid'] . '"/></td>';
echo '<td>Select</td>';
}
}
echo '</tr>';
echo '</table>';
?>
</td>
<td>
<?php
echo '<table border="1" cellpadding="2" cellspacing="2" style="width: 98%; margin-left: auto; margin-right: auto;">';
echo '<tr>';
$query = 'SELECT userid, name, surname, sex, age, nationality, email, telnr, special FROM employee
ORDER BY userid ASC';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
while ($row = mysql_fetch_assoc($result)) {
extract($row);
echo '<td>' . $userid . '</td>';
echo '<td>' . $name . '</td>';
echo '<td>' . $surname . '</td>';
echo '<td>' . $sex . '</td>';
echo '<td>' . $age . '</td>';
echo '<td>' . $nationality . '</td>';
echo '<td>' . $email . '</td>';
echo '<td>' . $telnr . '</td>';
echo '<td width="150">' . $specialtr . '</td>';
echo '</tr>';
}
echo '</table>';
?>
</td>
</tr>
</table>
<br/>
<br/>
<input type="submit" name="go" value="Go!" />
</form>
The code worked well enough, but since I added the trim, that entire column (the $special column disappears).
I hope maybe someone can tell me why it disappears or if my syntax has a little mistake.
Using extract() like that is a horrible thing. The function should be avoided as a general rule.
Also note that you're doing your trim() call BEFORE $special is defined, so your $specialtr variable is going to be an empty string. Calling the function before you start fetching your database results and extracting a result row into $special will not magically apply trim() to reach row you've fetched. If you want the results to be trimmed as you fetch them, then you have to apply the trim AFTER you do the fetch:
while ($row = mysql_fetch_assoc($result)) {
echo '<td>' . $row['userid'] . '</td>';
...
echo '<td width="150">' . trim($row['special']) . '</td>';
^^^^^^^^^^^^^^^^^^^^^
}

Categories