I have a script which returns 5 columns from a php result. I am trying to match one of the columns due date against the current date. If the date returned is older I want to highlight that row. It does not seem to for some reason. I have tested my if statement and it work.
<?php
// First of all initialise the user and check for permissions
require_once "/var/www/users/user.php";
$user = new CHUser(7);
// Initialise the template
require_once "/var/www/template/template.php";
$template = new CHTemplate();
// And create a cid object
require_once "/var/www/WIPProgress/DisplayWIPOnLocation.php";
$WIPProgress= new CHWIPProgress();
$content = "<h1>Check WIP Status on Location </h1>";
$content =
"<form action='index.php' method='get' name ='location'>
<select id='location' name ='location' >
<option>Skin Room</option>
<option>Clicking</option>
<option>Kettering</option>
<option>Closing</option>
<option>Rushden</option>
<option>Assembly</option>
<option>Lasting</option>
<option>Making</option>
<option>Finishing</option>
<option>Shoe Room</option>
</select>
<input type='submit' />
</form>";
if(isset($_GET['location'])) {
$wip = $WIPProgress->ListWIPOnLocation($_GET['location']);
$location = $_GET['location'];
$todays_date = date("Y-m-d H:i:s");
// Now show the details
$content .=
"<h2>Details for $location </h2>
<table>
<tr>
<th>PPDescription</th>
<th>Works Order</th>
<th>Bundle Number</th>
<th>Bundle Reference</th>
<th>Due Date</th>
</tr>";
foreach($wip as $x) {
if(strtotime($x['DueDate']) > strtotime($todays_date)){
$content .=
"<tr bgcolor='#FF0000'>
<td>" . $x['Description'] . "</td>
<td>" . $x['WorksOrder'] . "</td>
<td>" . $x['Number'] . "</td>
<td>" . $x['Reference'] . "</td>
<td>" . $x['DueDate'] . "</td>
</tr>";
}
}
}
else {
$content .= "<h3>Please choose a location to view WIP</h3>";
}
$template->SetTag("content", $content);
echo $template->Display();
?>
Is there any classes available in php which highlights rows returned?
Instead of defining bgcolor with tr define with each td in it.
OR better way to associate a CSS class to the tr and target all the td of this tr through CSS.
$content .= "<tr class='highlight'>...";
<style>
.highlight td
{
background-color:#FF0000;
}
</style>
Related
i need to get the counting of the table rows in order as a column in column "N"
also i need to make the column"N" width looks smaller than others columns
i think i have done everything but this is the only point i couldn't achieved
the output should be as the picture :
i hope you could help me guys, thank you
here is my codes :
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// DataBase connection
$host = "localhost";
$user = "root";
$password = "";
$db = "worksheet9";
$con = mysqli_connect($host, $user, $password, $db);
//verfiy Connection
if( mysqli_connect_errno()) {
echo " Connection Error:" . mysqli_connect_error();
}
if(isset($_POST['submit'])) {
$task=$_POST['task'];
// insert sql query
$sql = "INSERT INTO dolist (task) VALUES ('$task')";
$result=mysqli_query($con, $sql);
// verify query
if ($result) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
?>
<form method='POST'>
<input type="text" name="task" id="task" required
placeholder="add task" oninvalid="this.setCustomValidity('you must fill the task')"
oninput="this.setCustomValidity('')">
<input type="submit"name="submit"value="add task" ><!-- comment -->
</form>
<table border="1">
<tr>
<th> N </th>
<th>ID </th>
<th>task</th>
<th>delete</th><!-- comment -->
<th>update</th>
</tr>
<?php
// Select sql query
$sql = "SELECT ID,task FROM dolist";
$result = mysqli_query($con, $sql);
while ($rows = mysqli_fetch_array($result)) {
echo"<form method ='POST'>";
echo "<tr>";
echo "<td>" . "<input type='number' readonly name='number' value='" . $rows[''] . "'></td>";
echo "<td>" . "<input type='text' readonly name='id' value='" . $rows['ID'] . "'></td>";
echo "<td>" . "<input type='text' name='task' value= '" . $rows['task'] . "'></td>";
echo "<td>" . "<input type='submit' name='delete' value='x'>" . "</td>";
echo "<td>" . "<input type='submit' name='update' value='update'>" . "</td>";
echo "</tr>";
echo "</form>";
}
if(isset($_POST['update'])) {
$sql = "UPDATE dolist SET task='$_POST[task]'WHERE ID=$_POST[id]";
$result = mysqli_query($con,$sql) or die(mysqli_connect_errno());
if($result){
echo "updated";
} else {
echo "update faild";
}
}
//perform only when the user click on Delete
if(isset($_POST['delete'])) {
$sql = "DELETE FROM dolist WHERE ID=$_POST[id]";
$result = mysqli_query($con,$sql) or die(mysqli_connect_errno());
if($result){
echo "DELETED";
}else{
echo "delete faild";
}
}
?>
</table>
</body>
</html>
For row numbering add a counter to your PHP code:
$rowNumber = 1;
while ($rows = mysqli_fetch_array($result)){
echo"<form method ='POST'>";
echo "<tr>";
echo "<td>" . $rowNumber++. "</td>";
// echo other fields...
echo"</tr>";
For formatting, give your table an ID (not essential but it makes the CSS styling more specific)
This should then generate HTML like this:
<table id="results">
<tr><th>N</th><th>ID</th><th>task</th><th>Delete</th><th>Update</th></tr>
<tr><td>1</td><td>11</td><td>task</td><td>X</td><td>Update</td></tr>
<tr><td>2</td><td>12</td><td>task</td><td>X</td><td>Update</td></tr>
</table>
Now you can style it with CSS:
/* Border around the table */
#results {
border: 1px solid grey;
}
/* Border around the individual cells */
#results th, #results td {
border: 1px solid gray;
}
/* Set the width of the first TH. Also sets the width of the res of the column */
#results th:first-of-type {
width:1em;
}
/* Set the width of the 3rd column (the 'task' column */
#results th:nth-of-type(3) {
width:10em;
}
Output:
Note: you could do this by adding class attributes to the <th> elements and styling those. It makes for easier maintenance later.
Use of formatting attributes in HTML tags (like BORDER="1") is considered bad practice and should be avoided.
Don't use JS or PHP to count something pure CSS can help you with
Use CSS Counters
Use <thead> and <tbody> table elements
Use width: 0%; for the td you want really condensed in width
.tbodyCount {
counter-reset: rowIndex;
}
.tbodyCount tr {
counter-increment: rowIndex;
}
.tbodyCount td:nth-child(1)::before {
width: 0%; /* Make as smaller as possible */
content: counter(rowIndex); /* Learn about counters*/
}
<table>
<thead>
<tr>
<th>N</th>
<th>ID</th>
</tr>
</thead>
<tbody class="tbodyCount">
<tr>
<td></td>
<td>Foo</td>
</tr>
<tr>
<td></td>
<td>Bar</td>
</tr>
<tr>
<td></td>
<td>Baz</td>
</tr>
</tbody>
</table>
I have posted a form to output data form database. I am having trouble within my if statement looking to echo all lines that my month form post == the month within my database i.e if my form post was 04 i want it to equal all data that == XXXX/04/XX and echo them all.
At the moment it is not returning anything as the date formats do not match
Thank you in advance.
if(isset($_POST['submit'])) {//form has been submitted
$search = new Page();
$search->sub_name = $_POST['sub_name'];
$search->location = $_POST['location'];
$search->date_from = $_POST['date_from'];
} ?>
<h2>
<?php echo $search->sub_name; echo " "; echo $search->location;?>
</h2>
<table class="bordered" cellpadding="1" cellspacing="1">
<thead>
<tr> <th>Course</th> <th>Dates</th> <th>Course Type</th> <th>Location</th><th>Price </th><th></th></tr>
</thead>
<?php
$pages = Page::find_all();
foreach($pages as $page){
if($search->sub_name == $page->sub_name && $search->location == $page->subject && $search->date_from == $page->date_one('m')) {
echo "<tbody><tr><td>" . nl2br($page->course_name) . "</td><td>" . nl2br($page->date_from) . "</td><td>" . nl2br($page->sub_name) . "</td><td>" . nl2br($page->subject) . "</td><td>" . nl2br($page->price) . "</td><td><a href=\"hbt_bookings.php?id=" . urlencode($page->id) . "#type\">Book>>></td>" ."</tr></tbody>";
}
}
?>
</table>
I have a while loop displaying my database columns and rows in table.
I want make a button which can delete specific MySQL row but unfortunately I can't select which of all rows have to be deleted simply because I use "while loop" to display my database content in HTML.
Providing picture for better understanding what I want to do:
So yeah, I want that when the green button is clicked - send MySQL query to delete this row (which is existing in my DB).
Providing code:
<?php
//CONECT TO MYSQL
include 'database.php';
//GET VALUES
$sql = "SELECT * FROM amountcontainer";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
//Table headlines - NOT A PHP
echo "<table class='moneytable'>
<tr>
<th style='background-color: #2d323a; color: white;'>Date</th>
<th style='background-color: #2d323a; color: white;'>Amount</th>
<th style='background-color: #2d323a; color: white;'>Reason</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc())
{
//RED //Make statement of amount red if it has a " - "
if(strpos($row['amount'],'-') !== false)
{
echo
"
<tr>
<th style='font-weight: normal;background-color: #ff9999;' class='row-time'>" . $row['time'] . "</th>
<th style='font-weight: normal;background-color: #ff9999;' class='row-amount'>" . $row['amount'] . "</th>
<th style='font-weight: normal;background-color: #ff9999;' class='row-reason'>" . $row['reason'] . "</th>
</tr>
";
}
//NORMAL //Make statement of amount normal if it doesn't have a " - "
else
{
echo
"
<tr>
<th style='font-weight: normal;' class='row-time'>" . $row['time'] . "</th>
<th style='font-weight: normal;' class='row-amount'>" . $row['amount'] . "</th>
<th style='font-weight: normal;' class='row-reason'>" . $row['reason'] . "</th>
</tr>
";
}
}
echo "</table>";
}
else
{
echo "0 results";
}
First, only use <th> elements for the table headers. For the actual data cells use the <td> element.
To allow deletion of individual database rows you can include links in your table. In your loop that creates the table rows:
<tr>
<td>" . $row['time'] . "</td>
<td>" . $row['amount'] . "</td>
<td>" . $row['reason'] . "</td>
<td><a href='?deleteId=$row[keyID]'>Delete</a></td>
</tr>
In this case, when the "Delete" link is selected it calls your same script with a $_GET variable: "deleteId" which you can test for and if found, perform the delete of the row. You can also embed the table in an html form and add a submit button to a cell in each row, with the value of the submit set to the row id to delete.
This sounds like a place for ajax. Heres a crude example that might give you a good starting point.
<?php
while($row = $result->fetch_assoc()) {
$isNegative = strpos($row['amount'],'-') !== false;
echo "<tr class='record' data-id=\"{$row["id"]}\">";
//Use ternary operator & css classes
echo "<td class='standard-record ".$isNegative ? "color-red" : "" ."''>{$row["record"]}</td>";
echo "</tr>";
}
?>
<script>
$(".record").click(function(){
var request = {
id: $(this).data("id")
};
$.post( "delete-record.php", request, function(data){
$(this).remove();
});
});
</script>
Change this
if(strpos($row['amount'],'-') !== false) # wrong way
to this
if($row['amount'] < 0) # detecting negative numbers
You need to add an extra
<th style='font-weight: normal;' class='row-reason'><a class="crossBtn" href="?del_id=".$row['id']."> </a></th>
And the get the del_id and delete this specific records.
I want to search by product name then get productdetailsid from db then compare it with each row in this table if it exists , set bachground-color for row!
<div class="input-group m-b"><span class="input-group-btn">
<button type="button" class="btn btn-primary">Go!</button> </span> <input type="text" class="form-control"id="autocomplete">
</div>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th style="text-align: center">#</th>
<th style="text-align: center">Product Name</th>
<th style="text-align: center">Quantity</th>
<th style="text-align: center">Product Price</th>
<th style="text-align: center">Whole Price</th>
<th style="text-align: center">Supplier Name</th>
<th style="text-align: center"></th>
</tr>
</thead>
<tbody>
<?php
$per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1) * $per_page;
$sql = "select p.productname,p.quantity,p.onesale,p.wholesale,p.productdetailsid,s.fullname from productdetails p , supplier s where s.supplierid = p.supplierID LIMIT $start_from,$per_page";
$count = ($page*5)-4;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<form method=\"post\" action=\"\">";
echo "<tr>";
echo "<td>" . $count++ . "</td>";
echo "<td>" . $row["productname"] . "</td>
<td>" . $row["quantity"] . "</td>
<td>" . $row["onesale"] . "</td>
<td>" . $row["wholesale"] . "</td>
<td>" . $row["fullname"] . "</td>
<td><button type=\"submit\" class=\"btn btn-xs btn-danger\" name=\"removeBtn\"><i class=\"fa fa-times\"></i> </button></td>
<td style=\"display:none;\"><input type=\"hidden\" name=\"id\" value='".$row["productdetailsid"]."'>
</td>"
;
echo "</tr>";
echo "</form>";
}
}
$conn->close();
?>
</tbody>
</table>
</div>
For Live search in the table you need to use AJAX for getting the result, then using that result you can use Javascript loop to find which rows are found and change the color. I would prefer JSON output (JSON array output) from the PHP script, I can use that in Javascript Loop...
in PHP
You need to make a seperate PHP file for getting ajax output...say ajax.php
<?php
//Assuming that 'q' is the POST variable for finding the query
if(!empty($_POST['q']))
{
//Put your database query execution code here with using q as search query
//Set header to JSON
header('Content-Type:application/json');
//assuming that $result is associative array of your SQL query output
which contains array of the rows to be hightlighted
echo json_encode($result);
}
This will give a JSON array output which can be easily read by AJAX or $.post functions...
in Javascript
here's the tricky part. you need to make an ajax call and then use that data to highlight the rows....but first need to give an ID to the table body...
...<tbody id="table1">....</tbody></table>
Then you need to use ajax or $.post function to make the call, I have used $.post as it's simple
/**** Cosidering "input1" is a input element for getting query to be searched with id = input1 ****/
var q_data = $("#input1").val();
$.post(ajax.php,
{
q: q_data
},
function(data,status)
{
$.each($("#table1").find("<tr>"),function(i,value)
{
var element = $(this);
$.each(data,function(i,val2){
if(val2 == $(element).find("<td>").text())
{
$(element).css('background','lgihtyellow');
}
});
});
}
);
The code may not work before suitable changes, but this is the logic and concept of ajax based live search in the table
I am trying to make something so it shows the database results. The problem is, it only shows 1 result. I want it to show multiple results using an quick and dirty template system. Also, is there a way to make my system better? Perhaps a class or a function? I need some insight on this. Thanks a bunch!
cp.php
<?php
require("db.php");
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
$title = "********";
require("templates/header.php");
if($mybb->user['uid'])
{
$uid = $mybb->user['uid'];
// Active Tournaments
// run queries, do all the brainwork
// lets get the forum name
$query = "SELECT tourneys.name, tourneys.date, tourneys.time
FROM tourneys
INNER JOIN players ON players.tid = tourneys.id
WHERE players.forumname = {$uid}";
$result = mysqli_query($conn, $query);
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$activetournaments = "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td>";
// $team = mysqli_query($conn, "SELECT * FROM tourneys WHERE id=" . $row['tid'] . "");
// $playing = mysqli_query($conn, "SELECT `` FROM tourneys WHERE id=" . $row['tid'] . "");
}
}
else
{
$error = "Only registered members may access this area.";
include ('templates/error.php');
}
require ("templates/cp.php")
?>
templates/cp.php
<h2>Welcome back <?=$mybb->user['username']; ?>!</h2>
<?=$postrequirement?>
<h3>Active Tournaments</h3>
<table>
<tr>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Playing as</th>
<th>Options</th>
</tr>
<tr>
<?=$activetournaments?>
</table>
<hr />
<h3>Participated Tournaments</h3>
<table>
<tr>
<th>Position</th>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Played as</th>
<th>Options</th>
</tr>
<tr>
<?=$participatedtournaments?>
</table>
I have changed $activetournaments to append to the end using '.='.
while($row = mysqli_fetch_assoc($result)) {
$activetournaments .= "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td> ";
}
At the moment, for each record, it is overwriting $activetournaments. You could also use $activetournaments as an array then do a while / foreach in the template.
In your cp.php you overwrite $activetournaments in each while execution, change your code to:
<?php
require("db.php");
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
$title = "1ShotGG Tournaments | Control Panel";
require("templates/header.php");
if($mybb->user['uid'])
{
$uid = $mybb->user['uid'];
// Active Tournaments
// run queries, do all the brainwork
// lets get the forum name
$query = "SELECT tourneys.name, tourneys.date, tourneys.time
FROM tourneys
INNER JOIN players ON players.tid = tourneys.id
WHERE players.forumname = {$uid}";
$result = mysqli_query($conn, $query);
// output data of each row
$activetournaments = '';
while($row = mysqli_fetch_assoc($result)) {
$activetournaments .= "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td>";
// $team = mysqli_query($conn, "SELECT * FROM tourneys WHERE id=" . $row['tid'] . "");
// $playing = mysqli_query($conn, "SELECT `` FROM tourneys WHERE id=" . $row['tid'] . "");
}
}
else
{
$error = "Only registered members may access this area.";
include ('templates/error.php');
}
require ("templates/cp.php")
?>
In your templates/cp.php you can add a </tr> tag here:
<tr>
<?=$activetournaments?>
</tr>
your new templates/cp.php:
<h2>Welcome back <?=$mybb->user['username']; ?>!</h2>
<?=$postrequirement?>
<h3>Active Tournaments</h3>
<table>
<tr>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Playing as</th>
<th>Options</th>
</tr>
<tr>
<?=$activetournaments?>
</tr>
</table>
<hr />
<h3>Participated Tournaments</h3>
<table>
<tr>
<th>Position</th>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Played as</th>
<th>Options</th>
</tr>
<tr>
<?=$participatedtournaments?>
</table>