Hide table row that contains specified string - javascript

$row['attended'] can be either 'YES' or 'NO' (strings). I want to hide every row that has 'Attended' as 'NO'. So in pseudo code:
if (<tr> contains 'NO') {
hide that row (<-That's the tricky part to me)
}
My code is on the server side, but I'd be happy to echo out some JavaScript if that's what's necessary.
<?php
include 'connect2.php';
date_default_timezone_set('Europe/Lisbon');
$today = date('Y-m-d');
$sql="Select * FROM detentions WHERE date = '$today'";
$result = mysqli_query($con,$sql);
echo "<center><table style='width:400px; border:3px solid #444'>
<tr>
<th style='background-color:#444'>First</th>
<th style='background-color:#444'>Last</th>
<th style='background-color:#444'>Attended</th>
<th style='background-color:#444'></th>
</tr>";
while($row=mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td><center>" . $row['attended'] . "</center></td>";
echo "<td><button type = 'button' class='unattended button-error pure-button button-xsmall' style='float:right; margin-left:0.2cm' name='" . $row['lname'] . "' id='" . $row['fname'] . "'><b>Unattended</b></button> <button type = 'button' class='editDet2 button-success pure-button button-xsmall' style='float:right' id=" . $row['det_id'] . "><b>Attended</b></button></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

Another option is to skip the record with $row['attended']="No" in your while statement... An "if ($result.attended=='yes') {add ...} stamement" will do... Though the more efficient and elegant way would be to add a clause on the query statement like:
Select * FROM detentions WHERE date = '$today' and attended='yes'"
As suggested on the previous answer...

replace your select statement
$sql="Select * FROM detentions WHERE date = '$today' AND attended = 'YES' OR attended = ''";
and remove your if condition.

You can add a WHERE clause unto your SELECT statement, which specifies that the Attended field should be equal to no. This would prevent the unwanted rows from being returned in the first place.
Change your SELECT statement to:
SELECT * FROM detentions WHERE date = '$today' AND attended='yes'

Related

How do I style specific words when it gets fetched from my MySQLI database in PHP

I have data being fetched from MySQLI database. I want to style "Small" so it looks like this:
Here is what my table looks like in PHP. I am unsure how to style specific words from fetched data.
Here's my PHP code:
<?php
$connect = mysqli_connect( 'placeholder','placeholder','placeholder','placeholder');
$sql = "SELECT * FROM databasename";
$result = $connect->query($sql);
if ($result->num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
echo "<tr><td>" . $row["item_name"] . "</td>
<td>" . $row["item_cooldown"] . "</td>
<td>" . $row["item_effect"] . "</td>
<td>" . $row["item_passive"] . "</td>
<td>" . $row["item_cost"] . "</td>
<td>" . $row["item_type"] . "</td>
<td>" . $row["item_size"] . "</td></tr>";
}
}
else {
echo "No Results";
}
$connect->close();
?>
</table>
Any help is very appreciated.
Add a span inside the related cell (the last td tag pair) and make a css class to style it.
You could even have different colours for different data if you make a base class and a modifier class that is either applied using if/else or switch, or just use the data as the class name if they're single-word.
Completely untested, but shouldn't be too far off. I used single quotes to not conflict with the double quotes of the html syntax. My PHP might be a bit rusty.
'<td>
<span class="sizetag' . $row['item_size'] . '">' . $row['item_size'] . '</span>
</td>'
CSS
.sizetag {
border-radius 3px
padding: .5em
}
.large {
background-color: green
}
Then supply with the classes you need and adjust the colours until they are nice and the text is stil readable.
If you don't want to use the data name as a class you could do the following which uses colour name (or whatever you need) instead.
I would place this inside the while:
$color = ""
switch ($row['item_size']) {
small:
$color = 'bg-green';
break;
medium:
$color = 'bg-red';
break;
large:
$color = 'bg-yellow';
break;
}
And this to replace the last td
'<td>
<span class="sizetag' . $color . '">' . $row['item_size'] . '</span>
</td>'
And again supply with the classes you need and adjust the colours until they are nice and the text is stil readable.
to your last <td> tag change it to:
<td style='backgroundColor:green'> ...

Updating mySQL table with user interface - PHP

[Sample Look]
I'm trying to make an interface where you can edit/add/remove fields of a mySQL database. This is how it looks visually, and I have all the functionality on the client side working.
My question is: How can I pass any edits/adds/removals to the server side? I'll include a link for my JSFiddle.
And the code below will show how I currently great the table.
<?php
$servername = "localhost";
$username = "lalalal";
$password = "lalalal";
$link = mysqli_connect("localhost", "lalala", "lalala", "lalala");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sqlStart = "SELECT `Name`, `EXT`, `Returning Time`, `Returning Date`, `Out`, `Reset`, `Booked` FROM `lalala`";
if($result = mysqli_query($link, $sqlStart)){
if(mysqli_num_rows($result) > 0){
echo "<table id = contactTable>";
echo "<tr id = row1>";
echo "<th id = sortTable onclick=sortTable(0)>Name ↕</th>";
echo "<th style = width:100px;>EXT</th>";
echo "<th style = width:300px;>Returning Time</th>";
echo "<th style = width:300px;>Returning Date</th>";
echo "<th style = width:70px;>Out</th>";
echo "<th style = width:100px;>Reset</th>";
echo "<th style = width:600px;>Booked</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
$currentCheck = $row['Out'];
if ($currentCheck == 0) {
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['EXT'] . "</td>";
$currentTime = $row['Returning Time'];
if ($currentTime == 0) {
echo "<td> <form> <input type = 'time', id = 'timePickChange'> </form> </td>";
} else {
echo "<td> <form> <input type = 'time', id = 'timePickChange' value =" . $currentTime . "> </form> </td>";
}
$currentDate = $row['Returning Date'];
echo "<td> <form> <input type = 'date', id = 'datePickChange' value =" . $currentDate . "> </form> </td>";
echo "<td> <form onclick = 'checkIfOutRow(this)'> <input type = 'checkbox', onclick = 'checkIfOutValue(this)'> </form> </td>";
echo "<td> <button onclick = 'clearForm(this)', id = buttonClear>Reset</button> </td>";
echo "<td> <textarea rows = '1', cols = '60'> </textarea> </td>";
} else if ($currentCheck == 1) {
echo "<tr style = 'background-color: #E2E9FD'>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['EXT'] . "</td>";
$currentTime = $row['Returning Time'];
echo "<td> <form> <input type = 'time', id = timePickChange disabled> </form> </td>";
$currentDate = $row['Returning Date'];
echo "<td> <form> <input type = 'date', id = datePickChange disabled> </form> </td>";
echo "<td> <form onclick = 'checkIfOutRow(this)'> <input type = 'checkbox', onclick = 'checkIfOutValue(this)' checked> </form> </td>";
echo "<td> <button onclick = 'clearForm(this)', id = buttonClear>Reset</button> </td>";
echo "<td> <textarea rows = '1', cols = '60'> </textarea> </td>";
}
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sqlStart. " . mysqli_error($link);
}
?>
Depending on your data validation model, you might want to control the inputs value client side before posting them to your back-end.
AFAIK, you're already adding/editing/removing your contacts on the client side, so If I understand correctly, when your user should click on Edit/Remove & confirm , it would be a confirmation of what the user has done in the browser, this doesn't really change much apart from the fact that otherwise you might need dedicated buttons/row (or any other bindable events).
For these operations what you could do is proceed to bulk delete / edit, and this could be easily done by filtering out in your JS all the modified/deleted data and sending it to your back end PHP with Ajax/jQuery in the form of a stringified array.
As for insertion operation you'd submit them at the same time you add them to your table, by executing a POST operation.
And it could be done with something like this :
$.ajax({
method: "PUT",
url: "some.php",
data: JSON.stringify(myUpdatedDataInAnArray)
// you might need to stringify your array to ensure format ?
})
.done(function( msg ) {
alert( "Data Updated: " + msg );
});
In your back end php, you'd listen for POST/PUT/DELETE methods with something like that :
if (isset($_POST['add'])){
do your thing
}
if (isset($_PUT['updated'])){
//Since you're sending a stringified array, you must parse it with
$myArray = json_decode($_PUT['updated']);
do your thing
}
if (isset($_DELETE['deleted'])){
do your thing
}
I say Ajax because using a traditional POST/PUT/DELETE form would result in refreshing the page.
Here are some useful refs :
JS JSON Stringify and JSON Parse
PHP : JSON DECODE and JSON Encode
Ajax docs
Ajax Examples

How can i display data from selected row in textarea

I'm trying to learn php with html and I decided to create a project.
Right now, I'm stuck at this point where I want to display data on a textarea.
I created a database named 'company' with the table 'login' and the columns 'id','password' and 'username'.
When I select an option in the select option menu, the data from the column 'id','password' and 'username' is displayed in a table. What I want to achieve is when I click on a row, that the data from the column "problem" is displayed on the textarea which describes the situation with explanation.
I don't know how to write this but the syntax should be something like: Select 'problem' from login WHERE id='selected row id' I created on my php a select case witch works fine. The only problem that I have is I don't know how to select the id when I click on a specific row to display the data on the textarea.
Here's my code so far:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Formulaire</title>
<link rel="stylesheet" type="text/css" href="formulaire.css"/>
<!--<script type="text/javascript" src="select1.js"></script>-->
<!--<script src="http://code.jquery.com/jquery-latest.js"></script>-->
<style>
table tr:not(:first-child){
cursor: pointer;transition: all .25s ease-in-out;
}
table tr:not(:first-child):hover{background-color: #ddd;}
</style>
</head>
<body>
<div id="form">
<form action="#" method="post">
<label>Veuillez choisir une option :</label>
<select id="choix" name="choixtest">
<option value="tous">tous</option>
<option value="id">id</option>
<option value="username">username</option>
<option value="password">password</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
<table id="table">
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "company");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM login";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>username</th>";
echo "<th>password</th>";
echo "</tr>";
if(isset($_POST['submit'])){
$selected_val = $_POST['choixtest']; // Storing Selected Value In Variable
while($row = mysqli_fetch_array($result)){
switch($_POST['choixtest']){
case 'tous':
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
break;
case 'id':
echo "<tr>";
echo "<td>" . $row[$selected_val] . "</td>";
echo "<td>" . "</td>";
echo "<td>" . "</td>";
echo "</tr>";
break;
case 'username':
echo "<tr>";
echo "<td>" . "</td>";
echo "<td>" . $row[$selected_val] . "</td>";
echo "<td>" . "</td>";
echo "</tr>";
break;
case 'password':
echo "<tr>";
echo "<td>" . "</td>";
echo "<td>" . "</td>";
echo "<td>" . $row[$selected_val] . "</td>";
echo "</tr>";
break;
default:
// Something went wrong or form has been tampered.
}
}
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
</table>
<textarea id="erreur" rows="20" cols="100"></textarea>
</form>
</div>
</body>
</html>
For a table with <table id="myTable">to get the first column value into a textbox which is defined as
<input type="text" placeholder="Customer Name" id="cus_name">
You can add the following js code.
<script>
var table = document.getElementById('myTable');
for(var i = 1; i < table.rows.length; i++)
{
table.rows[i].onclick = function()
{
document.getElementById("cus_name").value = this.cells[0].innerHTML;
};
}
</script>
Here the cus_name is the id of the text field while in the this.cells[0], the value 0 represents the first column. You can change it accordingly and also can add more columns with the code document.getElementById("cus_name").value = this.cells[0].innerHTML; by only changing the cell value and the textbox id.
Every row in your html table must correspond to an id in the login table.
you have to place somewhere in the row the id corresponding to that row. You can achieve this in many ways.
one way to do it is using a data-something attribute to put the id, for instance
<tr data-id="<?php echo $current_row['id']; ?>">
<td>column1</td>
<td>column2</td>
<td>etcetera</td>
</tr>
Once you have the data-id atttribute set you can access it via Javascript everytime a user clicks on the row. For this you need to capture the click event on the row. I'll show you an example using jQuery:
$('.myTable tr').click(function(){
let id = $('this').data('id');
console.log('id of selected row: ' + id);
});
Now the 'id' variable contains the id of the row the user selected (check console output to make sure the id is the one you expect). The next step would be to use that id to make a request to the server, via Ajax or via regular http request. If this works successfully i suggest you to research, or make another post to find out how to reload the page with the content with the data you want. Since the more specific a question is the higher is the chance that it will be answered

Renaming input Fields IDs in a form created with a while loop

I am trying to POST a form that is generated through a while loop that iterates on some elements of a table and they can vary in number according to the different companies. Because the IDs of the input/edit fields are created by a while-loop they all have the same id and this is creating a problem when I try to submit the form, is there a way to change automatically the IDs? I tried to use Javascript with no success. Is there a way to name the IDs according to the loop cycle?
Also is there a better way to create a number of input/edit fields according to the number of items to be edited in an SQL table?
echo '<form id="myForm" action="edit.php" method="POST">';
echo '<div class="leftcontact">';
$i=0;
while($query_data = mysqli_fetch_row($result)) {
$_SESSION["List"]=$query_data[2];
$_SESSION["List_company_id"]=$query_data[0];
$result2 = mysqli_query($connection, "SELECT * FROM `calling_lists` WHERE `calling_list_id`='".$_SESSION["List"]."'");
$query_data2 = mysqli_fetch_row($result2);
$_SESSION["list_name"]=$query_data2[1];
echo '<div class="form-group">';
echo '<p>List number</p>';
echo '<select class="dropdown" name="ID">';
echo "<option selected = 'selected' value=\"".$_SESSION["List_company_id"]."\">".$_SESSION["list_name"]."</option>";
$query1="SELECT * FROM `calling_lists`";
$result1=mysqli_query($connection,$query1) or die ("Query to get data from list table failed: ".mysql_error());
while ($row1=mysqli_fetch_array($result1)) {
$list_name=$row1["calling_list_name"];
$list_description=$row1["calling_list_description"];
$list_id=$row1["calling_list_id"];
echo "<option value=\"$list_id\">"
. $list_name .
"</option>";
};
$i=$i+1;
};
Just use your $i variable to create the id attribute of the controls you emit from your while loop. So they would be named for example “select1”, “select2” and so on.
it was late and I could not figure out the answer, but with a fresh look I found it. In the code there are actually 2 problems, one is that the operator is missing and there was a problem with " ' ".
The working code is here below, in case anybody needs it. It creates a number of dropdown menu, as many as my $results rows.
Hope it helps anybody.
$i=0;
while($query_data = mysqli_fetch_row($result)) {
$_SESSION["List_company_id"]=$query_data[3];
$_SESSION["List"]=$query_data[1];
$_SESSION["list_name"]=$query_data2[2];
echo '<div class="form-group">';
echo "<p>List number $i</p>";
echo '<span class="icon-case hidden-xs"><i class="fa fa-home"></i></span>';
echo "<select class='dropdown' name=\"".$_SESSION['List_company_id']."\">";
echo "<option selected = 'selected' value=\"".$_SESSION["List_company_id"]."\">".$query_data[2]."</option>";
$query1="SELECT * FROM `calling_lists`";
$result1=mysqli_query($connection,$query1) or die ("Query to get data from list table failed: ".mysql_error());
while ($row1=mysqli_fetch_array($result1)) {
$list_name=$row1["calling_list_name"];
$list_description=$row1["calling_list_description"];
$list_id=$row1["calling_list_id"];
echo "<option value=\"$list_id\">
$list_name
</option>";
};
echo "</select>";
echo "</div>";
$i=$i+1;
};

Display data in tabular manner in PHP form

I want to do something like this..
I have a update form in PHP, which contains around 15 fields,
I want to show some data in tabular manner while you open the page for update..
Senario:
If you want to update a record of a person on day to day basis, I have the feature with me to search out that person form database, and when you click on update data..
it should show in a manner that,
field1.....----------
field2.....----------
field3.....----------
and now,
for field4,5,6,7
if these fields contain the data then show in a table manner with fields 4,5,6,7 as coloums and no of rows depending upon the number of entries (should be non editable)(assume the case of multiple input)
then form will continue.
field4.....--------
field5.....--------
field6.....--------
field7.....--------
typically these field(4,5,6,7) gets their value update frequently and I have to maintain the previous inputs also.
help me out with some solution, as I am unable to figure out how to do this?
before that i was using this.. here i was having a form field which can generate 5 options, and will populate the fields with data if u have entered before, now i want to change the display as I don't want user to play with the old data, so I want to show old data into a table and enter new data via a simple text field,
as their is no point to keep these multiple input form field as user is updating only one value at a time.
<script>
function Submitted_Date(value,object)
{
value = document.getElementById("subdate_num").value;
if (value > -1) {
var input= new Array(value);
var p;
//alert("Enter The fields You Know ");
var ele = document.getElementById("subdate");
if(ele.hasChildNodes())
{
var len = ele.childNodes.length;
while(ele.childNodes.length - value > 1)
{
ele.removeChild(ele.lastChild);
}
}
for (var i=len-1;i<value;i++)
{
p= i+1;
input[i] = document.createElement('div');
input[i].innerHTML = 'Submitted_Date' + p + ': <input type="text" value="" name="subdate' + p + '" id="subdate' + p +'" size = 25 onclick="javascript: showCalendar("Submitted_Date'+ p +'")"/>';
document.getElementById("subdate").appendChild(input[i]);
var subdate = 'subdate' + p;
}
}
document.getElementById("subdate1").focus();
}
</script>
<tr>
<td> Submitted Date :
<select name="subdate_num" id="subdate_num" onChange="return Submitted_Date(this.value,this.form)">
<?php
for ($i=0;$i<=5;$i++)
{
if ($i==0) { $temp = "<option value=$i> - </option>"; echo $temp; }
else { $temp = "<option value=$i>$i</option>";
echo $temp; }
}
?>
</select></td>
</tr>
</table>
<table>
I think this is what you want, this will show the information in a table with as many (or few) columns as you want
$query = "SELECT * FROM announcements ORDER BY announceid";
$result = mysql_query($query);
echo "<table border='1'>
<tr>
<th>Announcement ID</th>
<th>Announcement Name</th>
<th>Announcement Category</th>
<th>Approved?</th>
<th></th>
</tr>";
if ($result && mysql_num_rows($result) > 0) {
$x = 0;
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['announceid'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['category_name'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo '<td>Edit</td>';
echo "</tr>";
$x ++;
};
echo "</table>";
Let me know if that is what you are looking for, or if there is any other way I can help. Thanks!

Categories

Resources