Get the sum of selected columns - php

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.

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>

PHP echo without a loop. SQL Query

Here is the subject matter: Link, Click a players name to see.
I want to have the players name as a header so that the players name isn't repeated over and over again for each resulting row in the Roster table. I know how to loop through results for rows, but I only want the name once at the top and don't know how to do that. This is how I print the table code wise
<?php
$pid = $_POST['PID'];
$query = "SELECT Rosters.PID, Rosters.Goals, Rosters.Assists, Rosters.PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname,
(Rosters.Goals + Rosters.Assists) AS Points
FROM Rosters
INNER JOIN Players
ON Rosters.PID = Players.pid
WHERE Rosters.PID = $pid
ORDER BY Points DESC, Goals DESC";
$result = $conn->query($query);
$query2 = "SELECT Rosters.PID, SUM( Rosters.Goals ) Goals, SUM( Rosters.Assists ) Assists, SUM( Rosters.PIM ) PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname,
SUM((Rosters.Goals + Rosters.Assists)) AS Points
FROM Rosters
INNER JOIN Players ON Rosters.PID = Players.pid
WHERE Rosters.PID =$pid
ORDER BY Points DESC , Goals DESC";
$result2 = $conn->query($query2);
echo
if (!empty($_POST["PID"])) {
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Season</th>";
echo "<th class='hover'>Num</th>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
$i = 13;
$j = 14;
while ($row = $result->fetch_assoc()) {
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
while ($row = $result2->fetch_assoc()) {
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
}
?>
I just want Players.firstname and Players.lastname printed once before any of the tables are generated.
Try this:
if (!empty($_POST["PID"])) {
$row = $result->fetch_assoc();
echo "<span class='style35'>" . $row["firstname"] . " " . $row["lastname"] . "</span><br />";
$result->data_seek(0);
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Season</th>";
echo "<th class='hover'>Num</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
$i = 13;
$j = 14;
while ($row = $result->fetch_assoc()) {
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
while ($row = $result2->fetch_assoc()) {
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
}
?>
I don't know if you wanted the name of the player in the totals, so I left it there, but you can delete it if you want. I also applied to the player name the same style as the total.
You could get the first row of the result before your loop, then just reset the cursor, and completely ignore the players name inside your loop.
$first = $result->fetch_assoc();
// print out first name
echo($first['name'] . " " . $first['surname']);
// reset cursor
$result->data_seek(0);
// do your loops and stuff.
while ($row = $result->fetch_assoc()) {
...
}
You can do like this.If you do not want to change the sql query.
$ctr = 0;
while ($row = $result->fetch_assoc()) {
$ctr++;
if($ctr == 1) {
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
echo "<tr><td>" . $row["firstname"] . " " . $row["lastname"] . "</td> </tr>";
}
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}

how to use 2 fetch_assoc in one while loop?

i need list something like this.
First table is display By using the this code.
<?php
if (isset($_REQUEST['asign'])) {
include 'includes/connection.php';
$sql12= $cid->query("SELECT name FROM user WHERE designation='Technical' AND status='1'");
$query = "SELECT * FROM allinone WHERE flag=3 ORDER BY id ASC";
$rs = $cid -> query($query);
$n = $rs -> num_rows;
echo "<br /><span style='color:red;'><center>$n records found</center></span>";
echo "<div id='record'>";
echo "<table border='1' width='80%' align='center' cellpadding='0' cellspacing='0' id='unsolTable'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Ticket Number</th>";
echo "<th>KOID</th>";
echo "<th>PROBLEM</th>";
echo "<th>COMMENT</th>";
echo "<th>DATE</th>";
echo "<th>TIME</th>";
echo "<th>STATUS</th>";
echo "<th>SOLVED BY</th>";
echo "<th>Assign</th>";
echo "<th>Assigned to</th>";
echo "</tr>";
while ($row = $rs -> fetch_assoc() ) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['token'] . "</td>";
echo "<td>" . $row['koid'] . "</td>";
echo "<td>" . $row['problem'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['solvedBy'] . "</td>";
echo "<td><a href='assign.php?qType=technical&name=anshul&id=" . $row['id'] . "'>anshul</a>
<br />
<a href='assign.php?qType=technical&name=kiran&id=" . $row['id'] . "'>kiran</a>
<br />
<a href='assign.php?qType=technical&name=akhilesh&id=" . $row['id'] . "'>akhilesh</a></td>";
echo "<td>" . $row['assTo'] . "-" . $row['assTime'] . "-" . $row['assDate'] . "</td>";
echo "</tr>";
}
echo"</table>";
echo "</div>";
}
?>
On the another side is list on the assign i don't want to write it manually just get from the database how can i do this??
$sql= $cid->query("SELECT name FROM user WHERE designation='Technical' AND status='1'");
while ($row=$sql->fetch_assoc()) {
$name= $row['name'];
}
this is the another code i want to add in first code.
Nothing prevents you from executing another SQL query inside you while loop; just replace the echo...anshul...echo...kiran... part with another while loop containing the code you already provided.
You just have to make sure that you use different variables, otherwise your $row variable is overwritten by the second query. Hence, you can do something like this:
// ...
echo "<td>" . $row['solvedBy'] . "</td>";
$resultSetAssignedUsers = $cid->query("SELECT name FROM user WHERE designation = 'Technical' AND status = '1'");
while ($rowAssignedUsers = $resultSetAssignedUsers->fetch_assoc()) {
echo "<td><a href='assign.php?qType=technical&name=" . $rowAssignedUsers['name'] . "&id=" . $row['id'] . "'>" . $rowAssignedUsers['name'] . "</a>";
}
echo "<td>" . $row['assTo'] . "-" . $row['assTime'] . "-" . $row['assDate'] . "</td>";
// ...

mysql_fetch_array with dates separated

I am trying to loop through MySQL results and return same dates & agents in a separate . The table containing this data has the number of tickets each agent works on a specific day. Each group of dates should be separated by a blank row in the table. Below is the code I am working with. I believe I have to do a foreach, but not sure how to get it working.
Here is a screenshot to a final table layout I am looking to achieve.
if($res && mysql_num_rows($res))
{
while($row = mysql_fetch_array($res))
{
if ($row['total_updated'] > 0) {
print "<tr>";
print "<td align=center>" . $row['date_added'] . "</td>";
print "<td nowrap>" . $row['agent'] . "</td>";
print "<td nowrap>" . $row['agent_location'] . "</td>";
print "<td align=center>" . number_format($row['total_updated']) . "</td>";
print "<td align=center>" . number_format($row['total_notes']) . "</td>";
print "<td align=center>" . number_format($row['total_closed']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app1_updated']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app1_notes']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app1_closed']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app2_updated']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app2_notes']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app2_closed']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app3_updated']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app3_notes']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app3_closed']) . "</td>";
print "</tr>";
}
}
}
print "</table>";
If you sort your results by date_added you don't need any foreach, just compare previous date with current one:
while($row = mysql_fetch_array($res))
{
if (!isset($lastdate))
$lastdate = $row['date_added'];
if ($lastdate != $row['date_added']) {
?><tr><td colspan="15">---blank line---</td></tr> <?php
}
//paste all of your prints here
$lastdate = $row['date_added'];
}

unexpected results from jQuery loop on dynamic HTML table

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).

Categories