unexpected results from jQuery loop on dynamic HTML table - php

I have a table that is created in PHP. This table is the result of a query and it generates several rows. Inside these rows are quite a few different elements, such as drop down menus (< select >s) check boxes, and text fields. I made a loop to go through the elements in jQuery and get the values, but it's not returning what I thought it would. This is a fairly complex situation, so I'm going to post the code and a fiddle to perfectly illustrate exactly what is happening, and what I expect to happen.
PHP
if(isset($_POST['aid'])) {
$aid = $_POST['aid'];
$projects = getProjects($db);
$members = getUserName($db);
try {
$project_info = $db->prepare("
SELECT projects.id,
projects.project_name,
projects.pm,
projects.apm,
projects.est_start,
projects.est_end,
projects.contact,
projects.trips,
projects.tasks,
projects.perc_complete,
projects.bcwp,
projects.actuals,
projects.cpi,
projects.bcws,
projects.bac,
projects.comments,
projects.status,
projects.project_revenue,
projects.profit_margin,
projects.pm_perc,
projects.audited
FROM projects
WHERE account_id = ?
");
$project_info->bindValue(1, $aid, PDO::PARAM_STR);
$project_info->execute();
echo "<table class='projects_contentTable'>";
echo "<thead class='projects_editable_thead'>";
echo "<th class='content_th'>" . "Job #" . "</th>";
echo "<th class='content_th'>" . "Project Name" . "</th>";
echo "<th class='content_th'>" . "PM" . "</th>";
echo "<th class='content_th'>" . "APM" . "</th>";
echo "<th class='content_th'>" . "Est. Start" . "</th>";
echo "<th class='content_th'>" . "Est. End" . "</th>";
echo "<th class='content_th'>" . "Contact" . "</th>";
echo "<th class='content_th'>" . "Trips" . "</th>";
echo "<th class='content_th'>" . "Tasks" . "</th>";
echo "<th class='content_th'>" . "% Complete" . "</th>";
echo "<th class='content_th'>" . "BCWP" . "</th>";
echo "<th class='content_th'>" . "Actuals" . "</th>";
echo "<th class='content_th'>" . "CPI" . "</th>";
echo "<th class='content_th'>" . "Comments" . "</th>";
echo "<th class='content_th'>" . "Status" . "</th>";
echo "<th class='content_th'>" . "Project Revenue" . "</th>";
echo "<th class='content_th'>" . "Profit Margin" . "</th>";
echo "<th class='content_th'>" . "PM%" . "</th>";
echo "<th class='content_th'>" . "Audited" . "</th>";
echo "</thead>";
echo "<tbody class='projects_editable_tbody'>";
while ($row = $project_info->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td class='projects_editable_content_td'>" . "<a href='#'>" . $row['id'] . "</a>" . "</td>";
echo "<td class='projects_editable_content_td' contenteditable='true'>" . $row['project_name'] . "</td>";
echo "<td class='projects_editable_content_td'>" .
"<select id='table_edit_project_pm'>" .
"<option value=''>" . $row['pm'] . "</option>" .
"<option>" . "-----" . "</option>";
foreach($members as $key => $value) {
echo "<option value='".$key."'>" . $value . "</option>";
}
echo "</select>";
echo "</td>";
echo "<td class='projects_editable_content_td'>" .
"<select id='table_edit_project_apm'>" .
"<option value=''>" . $row['apm'] . "</option>" .
"<option>" . "-----" . "</option>";
foreach($members as $key => $value) {
echo "<option value='".$key."'>" . $value . "</option>";
}
echo "</select>";
echo "</td>";
echo "<td class='projects_editable_content_td'>" .
"<input type='text' id='table_edit_project_start' value='". $row['est_start'] ."'/>";
echo "</td>";
echo "<td class='projects_editable_content_td'>" .
"<input type='text' id='table_edit_project_end' value='". $row['est_end'] ."'/>";
echo "</td>";
echo "<td class='projects_editable_content_td' contenteditable='true'>" . $row['contact'] . "</td>";
echo "<td class='content_td'>" . $row['trips'] . "</td>";
echo "<td class='content_td'>" . $row['tasks'] . "</td>";
echo "<td class='content_td'>" . $row['perc_complete'] . "</td>";
echo "<td class='content_td'>" . $row['bcwp'] . "</td>";
echo "<td class='content_td'>" . $row['actuals'] . "</td>";
echo "<td class='content_td'>" . $row['cpi'] . "</td>";
echo "<td class='projects_editable_content_td' contenteditable='true'>" . $row['comments'] . "</td>";
echo "<td class='projects_editable_content_td'>" .
"<select>" .
"<option value=''>" . $row['status'] . "</option>" .
"<option>" . "-------" . "</option>" .
"<option>" . "On Hold" . "</option>" .
"<option>" . "In Progress" . "</option>" .
"<option>" . "Open" . "</option>" .
"<option>" . "Complete" . "</option>" .
"</select>";
echo "</td>";
echo "<td class='content_td'>" . $row['project_revenue'] . "</td>";
echo "<td class='content_td'>" . $row['profit_margin'] . "</td>";
echo "<td class='content_td'>" . $row['pm_perc'] . "</td>";
echo "<td class='projects_editable_content_td'>";
if ($row['audited'] == 'true') {
echo "<input type='checkbox' checked='checked'/>";
} else {
echo "<input type='checkbox'/>";
}
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
} catch(PDOException $e) {
die($e->getMessage());
}
} else {
echo 'could not load projects table';
}
jQuery
$('.projects_editable_tbody tr').each(function() {
$('.projects_editable_content_td:not(:has(select)),option:selected').each(function() {
saveEdits.push($(this).text());
});
});
$.each(saveEdits, function(index, value) {
alert(index + ': ' + value);
});
OK end wall, so here is the fiddle to play with http://jsfiddle.net/U3nna/14/
Inside the fiddle I actually list the values I'm expecting and all you have to do is click the save button to see what is actually happening.
For those of you who don't want to use the fiddle, or perhaps can't here is your very brief summary.
I need to loop through the table and go through each table row. Inside each table row I need the text value from the < td >'s with the class " .projects_editable_content_td". Each of these values will be put into an array. However some of the values are not being stored, and also it seems that this loop does an extra iteration on the table. So sometimes I'm expecting 9 or 18 values, and I end up with 27 or 36, and the more I run it the worse it gets.
I'm wracking my brain but I can't seem to figure out why it wouldn't be picking up some of these elements text values. If anyone has the time to lend me a hand, i'd really appreciate it.

The problem is you are getting only $(this).text() which misses the input (text/checkboxes) values
Also one thing i notice that you have assigned the same id for morethan one elements , id is supposed to be unique for the html elements table_edit_project_start , table_edit_project_end
Try this
var saveEdits = [];
$('#save').click(function() {
$('.projects_editable_tbody tr').each(function() {
$('.projects_editable_content_td:not(:has(select))>input')
.each(function() {
saveEdits.push($(this).val());
});
$('.projects_editable_tbody tr > .projects_editable_content_td:not(:has(select))')
.each(function() {
saveEdits.push($(this).text());
});
});
$.each(saveEdits, function(index, value) {
alert(index + ': ' + value);
});
});
For duplicate value you can use the selector like :eq(index),:gt(index) so in the loop it will fetch the current tr's fields
var saveEdits = [];
$('#save').click(function() {
$('.projects_editable_tbody tr').each(function(i) {
$('.projects_editable_tbody tr:eq('+i+') > .projects_editable_content_td:not(:has(select))>input')
.each(function() {
saveEdits.push($(this).val());
});
$('.projects_editable_tbody tr:eq('+i+') >.projects_editable_content_td:not(:has(select))')
.each(function() {
saveEdits.push($(this).text());
});
});
$.each(saveEdits, function(index, value) {
alert(index + ': ' + value);
});
});
Fiddle

Change it so that you supply correct option values with each option (i.e. if you want Jane Doe to output Jane Doe then just use it for both the value and the content within the option).
You then need to add a third possibility for other inputs, so I'd probably just rearrange the way you're going about this to:
$('td', this).each(function() {
var $input = $("select, input", this);
if($input.length) {
saveEdits.push($input.val());
}
else saveEdits.push($(this).text());
});
Note that I changed the selector too. It looks like you're using classes a lot where you don't need them. If you really need to use classes so much, maybe try and make them a little nicer to read too (use .projects-editable-content e.g., use dashed not underscores, and don't make them longer than they need to be).

Related

Open a link in a child window

when either Project or No is clicked, they open in a new window with other details under that clicked row, but i want them to open in a child window instead. is there a way to do it?
this is my code
<table id="keywords" class="container">
<thead>
<tr>
<td><span>Project</span></td>
<td><span>No</span></td>
<td><span>Sub ID</span></td>
<td><span>Name</span></td>
<td><span>Requested Amount</span></td>
<td><span>Paid Amount</span></td>
<td><span>Amount To be Paid</span></td>
<td><span>Description</span></td>
<td><span>State</span></td>
</tr>
</thead>
<?php
/* showing table */
$sql = "SELECT * FROM memo ORDER BY No DESC, SubID ASC"
or die("Failed to query database" .mysqli_error());
$result = $link->query($sql);
while ($row = $result->fetch_assoc()) {
print "<tr>";
print "<td >" . $row['Project'] . "</td>";
print "<td >" . $row['No'] . "</td>";
print "<td >" . $row['SubID'] . "</td>";
print "<td >" . $row['Name'] . "</td>";
print "<th >" . $row['RequestAmount'] . "</th>";
print "<th >" . $row['PaidAmount'] . "</th>";
print "<th >" . $row['AmountToPay'] . "</th>";
print "<td >" . $row['Description'] . "</td>";
print "<td >" . $row['State'] . "</td>";
print "</tr>";
}
// print "</table>";
?>
</table>

Get the sum of selected columns

In this sheet, I want to search for a time period and the location ad get the search result in another page.
this is the page i get after I search. (search.php)
<table class="table" id="keywords" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th><span>ID</span></th>
<th><span>Name</span></th>
<th><span>Location</span></th>
<th><span>Date</span></th>
<th><span>Catagory</span></th>
<th><span>Labour-Supplier</span></th>
<th><span>In-time</span></th>
<th><span>Out-time</span></th>
<th><span>Day</span></th>
<th><span>Day Rate</span></th>
<th><span>Salary</span></th>
<th><span>OT-hours</span></th>
<th><span>OT-rate</span></th>
<th><span>OT-amount</span></th>
<th><span>Allowance II</span></th>
<th><span>TotalSalary</span></th>
<th><span>Advance</span></th>
<th><span>Salary-to-hand</span></th>
</tr>
</thead>
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
$Location=$_POST['Location'];
$query = mysql_query("SELECT ID,Name,Location,Date,Category,LabourSupplier,Day,DayRate,Salary,OTHours,OTrate,OTAmount,Allowance2,TotalSalary,Advance,SalaryToHand FROM attendance WHERE Date BETWEEN '".$_POST["FDate"]."' AND '".$_POST["TDate"]."' AND Location LIKE '%" . $Location . "%' ORDER BY location DESC, Date DESC",$connection)
or die("Failed to query database" . mysql_error());
while ($row = mysql_fetch_array($query)) {
print "<tr>";
print "<td >" . $row['ID'] . "</td>";
print "<td >" . $row['Name'] . "</td>";
print "<td >" . $row['Location'] . "</td>";
print "<th >" . $row['Date'] . "</th>";
print "<td >" . $row['Category'] . "</td>";
print "<td >" . $row['LabourSupplier'] . "</td>";
print "<th >" . $row['InTime'] . "</th>";
print "<th >" . $row['OutTime'] . "</th>";
print "<th >" . $row['Day'] . "</th>";
print "<th >" . $row['DayRate'] . "</th>";
print "<th >" . $row['Salary'] . "</th>";
print "<th >" . $row['OTHours'] . "</th>";
print "<th >" . $row['OTrate'] . "</th>";
print "<th >" . $row['OTAmount'] . "</th>";
print "<th >" . $row['Allowance2'] . "</th>";
print "<th >" . $row['TotalSalary'] . "</th>";
print "<th >" . $row['Advance'] . "</th>";
print "<th>" . $row['SalaryToHand'] . "</th>";
print "</tr>";
}
}
}
print "</table>";
?>
I want to get the sum of the columns Day, Salary,OT hours, OT amount, Total Salary and Salary To Hand at the bottom of the table. Is there a possible way to do that or should i get the sums in another table.
PS i tried to get the sum in a new table but this didn't work.
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
$Location=$_POST['Location'];
$query = mysql_query("SELECT ID,Name,Location,sum(Day),sum(Salary),sum(OTHours),sum(OTAmount),sum(TotalSalary),sum(Advance),sum(SalaryToHand) FROM attendance WHERE Date BETWEEN '".$_POST["FDate"]."' AND '".$_POST["TDate"]."' AND Location LIKE '%" . $Location . "%' ORDER BY location DESC, Date DESC",$connection)
or die("Failed to query database" . mysql_error());
while ($row = mysql_fetch_array($query)) {
print "<tr>";
print "<td >" . $row['ID'] . "</td>";
print "<td >" . $row['Name'] . "</td>";
print "<td >" . $row['Location'] . "</td>";
print "<th >" . $row['Day'] . "</th>";
print "<th >" . $row['Salary'] . "</th>";
print "<th >" . $row['OTHours'] . "</th>";
print "<th >" . $row['OTAmount'] . "</th>";
print "<th >" . $row['TotalSalary'] . "</th>";
print "<th >" . $row['Advance'] . "</th>";
print "<th>" . $row['SalaryToHand'] . "</th>";
print "</tr>";
}
}
}
print "</table>";
?>
Try this SQL query :
// Get parameter
$fDate = $_POST["FDate"];
$tDate = $_POST["TDate"];
// Build SQL query
$sql = <<<SQL
SELECT '' AS ID,
'' AS Name,
'' AS Location,
SUM(Day) AS Day,
SUM(Salary) AS Salary,
SUM(OTHours) AS OTHours,
SUM(OTAmount) AS OTAmount,
SUM(TotalSalary) AS TotalSalary,
SUM(Advance) as Advance,
SUM(SalaryToHand) as SalaryToHand
FROM attendance
WHERE Date BETWEEN '{$fDate}' AND '{$tDate}'
AND Location LIKE '%{$Location}%'
ORDER BY location DESC, Date DESC
SQL;
// Excecute it
$query = mysql_query($sql ,$connection) or die("Failed to query database" . mysql_error());
// Handle result
while ($row = mysql_fetch_array($query)) {
...
}
Keep in mind that is not safe query (don't put $_POST param directly into SQL, use prepare statement).
# Strawberry is right, mysqli is a safer driver than mysql. Think of a migration when you have time.

foreach loop highlight cells from database

I'm wondering if my code could be easier with a foreach loop. my code thusfar:
the purpose is to read the values in the MYSQL table, and if the anwser is "NEE" display the background color in RED. my code works but it is very long..
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is
the password
mysql_select_db('heijsDB');
$query = "SELECT * FROM hygieneaanvoer"; //You don't need a ; like you do in
SQL
$result = mysql_query($query);
//List the Columns for the Report
if(! $result ) {
die('Could display data: ' . mysql_error());
}
echo "<table border='1' class='w3-panel'>
<fieldset>hygiëne/GMP aduit Aanvoer</fieldset>
<tr>
<th>Datum</th>
<th>Controleur</th>
<th>controleur</th>
<th>Revisie</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
<th>14</th>
<th>15</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['datum'] . "</td>";
echo "<td>" . $row['controleur'] . "</td>";
echo "<td>" . $row['codering'] . "</td>";
echo "<td>" . $row['revisie'] . "</td>";
if($row['q1']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q1']."</td>";
else echo "<td>".$row['q1']."</td>";
if($row['q2']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q2']."</td>";
else echo "<td>".$row['q2']."</td>";
if($row['q3']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q3']."</td>";
else echo "<td>".$row['q3']."</td>";
if($row['q4']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q4']."</td>";
else echo "<td>".$row['q4']."</td>";
if($row['q5']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q5']."</td>";
else echo "<td>".$row['q5']."</td>";
if($row['q6']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q6']."</td>";
else echo "<td>".$row['q6']."</td>";
if($row['q7']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q7']."</td>";
else echo "<td>".$row['q7']."</td>";
if($row['q8']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q8']."</td>";
else echo "<td>".$row['q8']."</td>";
if($row['q9']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q9']."</td>";
else echo "<td>".$row['q9']."</td>";
if($row['q10']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q10']."</td>";
else echo "<td>".$row['q10']."</td>";
if($row['q11']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q11']."</td>";
else echo "<td>".$row['q11']."</td>";
if($row['q12']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q12']."</td>";
else echo "<td>".$row['q12']."</td>";
if($row['q13']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q13']."</td>";
else echo "<td>".$row['q13']."</td>";
if($row['q14']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q14']."</td>";
else echo "<td>".$row['q14']."</td>";
if($row['q15']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q15']."</td>";
else echo "<td>".$row['q15']."</td>";
echo "</tr>";
}
echo "</table>";
?>
Here is solution
$result = mysql_query("SELECT * FROM hygieneaanvoer")or die('Could display data: ' . mysql_error());;
echo "<table border='1' class='w3-panel'>
<fieldset>hygiëne/GMP aduit Aanvoer</fieldset>
<tr>
<th>Datum</th>
<th>Controleur</th>
<th>controleur</th>
<th>Revisie</th>";
for ($i=1;$i<=15;$i++){
echo '<th>'.$i.'</th>';
}
echo "</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['datum'] . "</td>";
echo "<td>" . $row['controleur'] . "</td>";
echo "<td>" . $row['codering'] . "</td>";
echo "<td>" . $row['revisie'] . "</td>";
for ($j=1;$j<=15;$j++){
echo "<td ".(($row['q'.$j] == 'NEE') ? "style='background-color: #e21010;'" : "" ).">".$row['q'.$j]."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
What you could do, is just create a single variable that you'd use for setting the style of all rows:
while($row = mysql_fetch_array($result))
{
$style_string = "";
if($row['q1']=='NEE') {
$style_string = "style='background-color: #e21010;'";
}
echo "<tr>";
echo "<td>" . $row['datum'] . "</td>";
echo "<td>" . $row['controleur'] . "</td>";
echo "<td>" . $row['codering'] . "</td>";
echo "<td>" . $row['revisie'] . "</td>";
echo "<td " . $style_string . ">" . $row['q1'] . "</td>";
echo "<td " . $style_string . ">" . $row['q2'] . "</td>";
echo "<td " . $style_string . ">" . $row['q3'] . "</td>";
echo "<td " . $style_string . ">" . $row['q4'] . "</td>";
echo "<td " . $style_string . ">" . $row['q5'] . "</td>";
echo "<td " . $style_string . ">" . $row['q6'] . "</td>";
echo "<td " . $style_string . ">" . $row['q7'] . "</td>";
echo "<td " . $style_string . ">" . $row['q8'] . "</td>";
echo "<td " . $style_string . ">" . $row['q9'] . "</td>";
echo "<td " . $style_string . ">" . $row['q10'] . "</td>";
echo "<td " . $style_string . ">" . $row['q11'] . "</td>";
echo "<td " . $style_string . ">" . $row['q12'] . "</td>";
echo "<td " . $style_string . ">" . $row['q13'] . "</td>";
echo "<td " . $style_string . ">" . $row['q14'] . "</td>";
echo "<td " . $style_string . ">" . $row['q15']. "</td>";
echo "</tr>";
}
(Or just set the background color of the <tr>)
Best of luck!
You can simply add the class to the td like this
echo "<td class='". $row['q15'] . "'>".$row['q15']."</td>";
Add css like this
.NEE {background-color: #e21010;}

How to echo text to cell, based on the value of another cell

I have a PHP code, showing me raw data from a MySQL database in a table.
I want to add some text to one of the existing cells, depending on a value in another column which is not displayed in the table.
My code looks like this:
while($row = mysqli_fetch_array($rs))
{
echo '<tr class="lokbes">';
echo "<td class='blaa'>" . $row['Navn'] . "</td>"; // I want the extra text here.
echo "<td>" . $row['Stilling'] . "</td>";
echo "<td>" . $row['Institution'] . "</td>";
echo "<td><a href='mailto:$row[Email]'>" . $row['Email'] . "</a></td>";
echo "<td>" . $row['Mobiltelefon'] . "</td>";
}
echo "</tr>";
echo "</table>";
This outputs a table consisting of Name, job, workplace etc.
In the cell displaying the name, I would like to add some text if a column in my MySQL DB has the value 1 in the row.
What to do? I've tried using if, as seen below - but that doesn't seem to work.
echo "<td class='blaa'>" . $row['Navn'] .
if ($row['Formand'] == 1) {
echo "(Formand)";
} "</td>";
You have to do multiple echos :
echo "<td class='blaa'>" . $row['Navn'];
if ($row['Formand'] == 1) {
echo "(Formand)";
}
echo "</td>";
Or, with ternary operator :
echo "<td class='blaa'>" . $row['Navn'] . ($row['Formand'] == 1 ? "(Formand)" : "") . "</td>";
update like this.
echo "<td class='blaa'>" . $row['Navn'];
if ($row['Formand'] == 1) {
echo " (Formand)";
}
echo "</td>";
or you can use short PHP tag in HTML code.
?>
<td class="blaa">
<?php echo $row['Navn']?><?php echo ($row['Formand'] == 1)?' (Formand)':'';?>
</td>
<?php
Try below code.
while($row = mysqli_fetch_array($rs))
{
echo '<tr class="lokbes">';
echo "<td class='blaa'>" . $row['Navn']." ".($row['Formand'] == 1 ? "(Formand)" : ""). "</td>"; // I want the extra text here.
echo "<td>" . $row['Stilling'] . "</td>";
echo "<td>" . $row['Institution'] . "</td>";
echo "<td><a href='mailto:$row[Email]'>" . $row['Email'] . "</a></td>";
echo "<td>" . $row['Mobiltelefon'] . "</td>";
}
echo "</tr>";
echo "</table>";

How to make an dropdown menu to sort data from an database

I am working on an application to use on an website. The application let you add job offers to a database to post it on the website and on social media. I have done a lot already but now I am trying to make an sort menu. In my database I have 1 row that is called Status. The Status is an enum with the data Open and Closed so I would like to have a sort option. I would like to sort on Open, Closed and all.
I already have a table and I would like to see the changes on that existed table if that is possible but I have no idea how to do this. Can somebody help me with this? I am using HTML, PHP and as database PHPMyAdmin. Here is my code:
<!DOCTYPE HTML>
<html>
<head>
<style>
table {
border-collapse: collapse;
border: 1px solid black;
}
td {
border: 1px solid black;
width: 120px;
}
</style>
</head>
<body>
<h2>Jobs Overview (Admin)</h2>
<form action="/action_page.php">
<select id="sorting">
<option value="All" >All</option>
<option value="Open">Open</option>
<option value="Closed">Closed</option>
</select>
<br><br>
<?php
include 'Connection.php';
echo "<table>";
echo "<tr>";
echo "<th>" . "jid" . "</th>";
echo "<th>" . "role" . "</th>";
echo "<th>" . "type" . "</th>";
echo "<th>" . "availability" . "</th>";
echo "<th>" . "location" . "</th>";
echo "<th>" . "status" . "</th>";
echo "<th>" . "Verwijder?" . "</th>";
echo "</tr>";
$select = "SELECT * FROM test";
$result = mysql_query($select);
if($result) {
} else {
echo "Error! <br>";
echo mysql_error();
}
while($data = mysql_fetch_assoc ($result)) {
echo "<tr>";
echo "<td>" . $data["jid"] . "</td>";
echo "<td> <a href='jobsupdate.php?jid=" . $data["jid"] . "'>" . $data["role"] . " </a></td>";
echo "<td>" . $data["type"] . "</td>";
echo "<td>" . $data["availability"] . "</td>";
echo "<td>" . $data["location"] . "</td>";
echo "<td>" . $data["status"] . "</td>";
echo "<td> <center>Verwijder</center></td>";
}
echo "</tr>";
echo "</table>";
?>
<br><br>
Vacatures Toevoegen
</body>
</html>
Thanks in advance
You can add HTML class to open and closed jobs.
while($data = mysql_fetch_assoc ($result)) {
echo "<tr>";
echo "<td>" . $data["jid"] . "</td>";
echo "<td> <a href='jobsupdate.php?jid=" . $data["jid"] . "'>" . $data["role"] . " </a></td>";
echo "<td>" . $data["type"] . "</td>";
echo "<td>" . $data["availability"] . "</td>";
echo "<td>" . $data["location"] . "</td>";
if ($data["status"] == 0) {
echo "<td class='open'>" . $data["status"] . "</td>";
} else {
echo "<td class='closed'>" . $data["status"] . "</td>";
}
echo "<td> <center>Verwijder</center></td>";
}
echo "</tr>";
echo "</table>";
After, this you have to write a jquery script to hide the jobs .on change.
$('#sorting').on('change', function(){
var status = $('#sorting:selected').val();
if (status == "Open") {
$('.closed').hide();
} else if (status == "Closed") {
$('.open').hide();
} else if (status == "All"){
$('.closed').show();
$('.open').show();
}
});​
I hope this solves your problem.
You can make a Ajax post request on select change to get the records and put those records on success in your table.
More about how to do this with Jquery / Ajax here
More about Jquery on change here

Categories