how to get ID value from looped table using php and javascript - php

I've been trying to work out how to create a table using php and mysql, this is what i've got so far...
PHP :-
foreach($result as $res){
echo '<tr><td style="display:none;"><p id="ID">' . $ID . ' </p></td><td>' . $res['NAME'] . '</td>
<td>' . $res['option2'] . '</td>
<td>' . $res['Option3'] . '</td>
<td>' . $res['Option4'] . '</td>
<td>' . $res['Option5'] . '</td>
<td class="deleteBtn"><button class="btn btn-danger btn-xs" id="deleteBtn">Delete</button></td></tr>';
}
Javascript:-
$('button#deleteBtn').click( function(){
data = $('p#ID').html();
alert(data);
// window.location('admin.php?link=LF1'+data);
});
when i click deleteBtn i want to delete the row the button is on, therefore i need the ID...
How do I get the value of $ID. my jquery alerts only the first ID for every button...
Thanks!

First of all, id should be unique, so you cannot do id="ID" for every row.
Try with:
foreach($result as $res){
echo '<tr data-id="' . $ID . '">';
// ...
echo '</tr>';
}
And in JavaScript:
$('button#deleteBtn').click( function(){
var id = $(this).parents('tr').data('id');
alert(id);
}

It looks like if you have more than 1 row, you'll inevitably end up with a number of elements with the same ID. A cleaner up version would be:
More easily, you can simply use .closest()
PHP:
echo '<tr id='.$ID.'><td style="display:none;"><p id="ID">' . $ID . ' </p></td><td>' . $res['NAME'] . '</td>
<td>' . $res['option2'] . '</td>
<td>' . $res['Option3'] . '</td>
<td>' . $res['Option4'] . '</td>
<td>' . $res['Option5'] . '</td>
<td class="deleteBtn"><button class="delete btn btn-danger btn-xs">Delete</button></td></tr>'
jQuery:
$('button.delete').click( function(){
var toDelete = $(this).closest('tr').attr('id');
alert(toDelete);
});

You can use data attribute to keep your id. like data-id="1"
foreach($result as $res) {
echo '<tr><td style="display:none;"><p data-id="'.$ID.'" id="ID"'.$ID.' data-id="">' . $ID . ' </p></td><td>' . $res['NAME'] . '</td>
<td>' . $res['option2'] . '</td>
<td>' . $res['Option3'] . '</td>
<td>' . $res['Option4'] . '</td>
<td>' . $res['Option5'] . '</td>
<td class="deleteBtn"><button onclick="delete("ID'.$ID.')" class="btn btn-danger btn-xs" id="deleteBtn">Delete</button></td></tr>';
}
Now you can Id like this:-
$('button#deleteBtn').click( function(){
data = $(this).parents('tr').data('id');
alert(data);
});

Related

My code is outputting all data in 1 row, how do i make it output seperate data into seperate rows?

When my data is outputted from my server onto a website it outputs it onto 1 row instead of going down a row and outputting the next row of data
!
session_start();
$view = new stdClass();
$view->pageTitle = 'Camp Sites';
require_once('views/CampSites.phtml');
require_once('Models/campData.php');
require_once('Models/campDataSet.php');
$campDataSet = new campDataSet();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$search = $_POST['search'];
$_SESSION['search'] = $search;
$sqlQuery = "SELECT * FROM campsites ='$search'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<tr><td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td></tr>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
You need to wrap tags around the code in your foreach like above.

Why is the button not working only for the first row nor it posting any value?

I am presently working on a project in php mysql to display content from db with edit option against each row. In the below is the code, I am not able to return any value only for the first row, Rest of the row buttons are working fine. I am not able to figure out what is issue. It would be great if you could help me out.
Code snippet of the table display page
$sql1 = "SELECT * FROM `computation` WHERE `compute_month` = '$month' and `compensation_type` = '$allowance' and order by `emp_id` ASC";
}
$result1 = mysqli_query($con, $sql1);
echo "
<form action='confirmallowance.php' method='POST'>
<table class=data-table border='1' overflow-x=auto>
<caption class=title><b>Allowance Sheet<b></caption>
<thead>
<tr>
<th>Date</th>
<th>Allowance Month</th>
<th>Attendance Date</th>
<th>Day</th>
<th>Employee Code</th>
<th>Employee Name</th>
<th>InTime</th>
<th>OutTime</th>
<th>Work Duration</th>
<th>Team</th>
<th>Approver</th>
<th>Allowance type</th>
<th>Eligibility</th>
<th>Approved_amount</th>
<th>Status</th>
</tr>
</thead>
<tbody>";
if (mysqli_num_rows($result1) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result1)) {
echo '
<tr id="' . $row['id'] . '">
<td>' . $row['computation_date'] . '</td>
<td>' . $row['compute_month'] . '</td>
<td>' . $row['attendance_date'] . '</td>
<td>' . $row['attend_day'] . '</td>
<td>' . $row['emp_id'] . '</td>
<td>' . $row['emp_name'] . '</td>
<td>' . $row['time_in'] . '</td>
<td>' . $row['time_out'] . '</td>
<td>' . $row['duration'] . '</td>
<td>' . $row['team'] . '</td>
<td>' . $row['manager'] . '</td>
<td>' . $row['compensation_type'] . '</td>
<td>' . $row['eligibility'] . '</td>
<td>' . $row['amount'] . '</td>
<td>' . $row['status'] . '</td>
<td><input type="checkbox" name="checkbox[]" value="' . $row['id'] . '" checked="checked"></td>
<td>
<form action="editallowance.php" method="POST"><input type="hidden" name="tempid"
value="' . $row['id'] . '"/><input type="submit"
name="submit"
value="Edit"/>
</form>
</td>
</tr>
';
}
}
// Code snippet of the table value acceptance page on edit button click
if (isset($_POST["submit"]) && $_POST["submit"]) {
$editrow = $_POST['tempid'];
$sql = "SELECT * FROM `computation` WHERE `id` ='$editrow'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$att_date = $row['attendance_date'];
$att_day = $row['attend_day'];
$emp_id = $row['emp_id'];
$emp_name = $row['emp_name'];
$comptype = $row['compensation_type'];
$eligibility = $row['eligibility'];
$comment = $row['comment'];
$status = $row['status'];
echo "<P>Emp ID : " . $emp_id . "</p> <p>Emp Name :" . $emp_name . "</p><p>Attendance Date : " . $att_date . "</p><p>Day : " . $att_day . "</p>";
}
}
} else {
echo "No data";
If you need any addition info, please let me know.
Your html is not well formed. You are adding unused form open tag before table opening tag (there is not closing tag of it as well) in the first file:
<form action='confirmallowance.php' method='POST'>
<table class=data-table border='1' overflow-x=auto>
Please remove this form open tag html and it will work fine. It is interfering with the html forms that come later.

Two values in the same column

I have two columns with different values:
And I need to echo those two values into one column, like that:
Printing method:
if($result->num_rows > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<tr>
<td scope="row">' . $row["id"].'</td>
<td>' . $row["data"] .'</td>
<td>' . $row["ip"] .'</td>
</tr>';
}
}
Any suggestions on how I could do that?
Do you mean like this
echo '<tr>
<td scope="row">' . $row["id"].'</td>
<td>' . $row["data"] . '(' . $row["ip"] . ')</td>
</tr>';
Merge the two cells:
<td>' . $row["data"] .'</td>
<td>' . $row["ip"] .'</td>
Into one:
<td>' . $row["data"] . '(' . $row["ip"] . ')</td>

How to execute PDO in a variable PHP

while($row =$result->fetch())
{
$output .= '
<tr>
<td>' . $row["ID_personne"] . '</td>
<td>' . $row["Nom"] . '</td>
<td>' . $row["Prenom"] . '</td>
<td>' . $row["Telephone"] . '</td>
<td>' . $row["Mail"] . '</td>
<td>' . $row["Categorie"] . '</td>
<td>' . $row["Type"] . '</td>
'.$q2 = $db->prepare("SELECT * FROM entretiens WHERE ID_entretien=:id");
$q2->bindValue(":id",$row["Entretien"]);
$q2->execute();
$entretien=$q2->fetch().'
<td>'.$entretien["Date"].'</td>
<td width=250>
<a class="btn btn-primary" href="update_personnes.php?ID_personne='.$row["ID_personne"].'"><span class="glyphicon glyphicon-pencil"></span> Modifier</a>
<a class="btn btn-danger" href="delete_personnes.php?ID_personne='.$row["ID_personne"].'"><span class="glyphicon glyphicon-remove"></span> Supprimer</a>
</td>
</tr>';
}
$output .= '</table>';
echo $output;
I have always an Error: ( ! ) Catchable fatal error: Object of class PDOStatement could not be converted to string in C:.......... on line 61('.$q2 = $db->prepare("SELECT * FROM entretiens WHERE ID_entretien=:id");
$row["Entretien"] it's a foreign key
There was some syntax error in your code as $q2 is being concat with html string. Improved code should be:
while ($row = $result->fetch()) {
$output .= '
<tr>
<td>' . $row["ID_personne"] . '</td>
<td>' . $row["Nom"] . '</td>
<td>' . $row["Prenom"] . '</td>
<td>' . $row["Telephone"] . '</td>
<td>' . $row["Mail"] . '</td>
<td>' . $row["Categorie"] . '</td>
<td>' . $row["Type"] . '</td>';
$q2 = $db->prepare("SELECT * FROM entretiens WHERE ID_entretien=:id");
$q2->bindValue(":id", $row["Entretien"]);
$q2->execute();
$entretien = $q2->fetch();
$output .= '<td>' . $entretien["Date"] . '</td>
<td width=250>
<a class="btn btn-primary" href="update_personnes.php?ID_personne=' . $row["ID_personne"] . '"><span class="glyphicon glyphicon-pencil"></span> Modifier</a>
<a class="btn btn-danger" href="delete_personnes.php?ID_personne=' . $row["ID_personne"] . '"><span class="glyphicon glyphicon-remove"></span> Supprimer</a>
</td>
</tr>';
}
$output .= '</table>';
echo $output;

php change rows color with same value

I have a page which displays some data imported in MySQL database from Excel file
I want to change background color of rows with same values (grouped) on col "Billing Doc" like this picture:
Here is a part of my php code:
<form action="uploadExcel.php" method="POST" enctype="multipart/form-data">
<div>
<input type="file" name="file">
<input type= "submit" value ="Upload" >
</div>
</form>
<table width="100%" border="1" cellpadding="5">
<tr>
<th>ID</th>
<th>Billing Doc</th>
<th>Invoice Date</th>
<th>Ordered Parts</th>
<th>Shipped Parts</th>
<th>Qty</th>
<th>F</th>
<th>G</th>
<th>Amount</th>
<th>Delivery No</th>
<th>D/O Creation Date</th>
<th>Description</th>
<th>P/O No</th>
<th>Ship-to</th>
<th>Tracking No</th>
<th>Obs</th>
</tr>
<?php
while ($row = $result->fetch_object()) {
if (substr( $row->tracking_no, 0, 3 ) === "534") {
$row->tracking_no = "<a href='http://www.dhl.be/en/express/tracking.html?pageToInclude=RESULTS&AWB=$row->tracking_no&type=fasttrack' title='DHL'>$row->tracking_no</a>";
}
if (substr( $row->tracking_no, 0, 3 ) === "730") {
$row->tracking_no = "<a href='http://www.tnt.com/webtracker/tracker.do?navigation=1&respLang=en&respCountry=gb&cons=$row->tracking_no' title='TNT'>$row->tracking_no</a>";
}
$colour = '';
echo '
<tr style="background-color:' .$colour. '">
<td>' . $row->id_factura . '</td>
<td>' . $row->billing_doc . '</td>
<td>' . $row->invoice_date . '</td>
<td>' . $row->ordered_parts . '</td>
<td>' . $row->shipped_parts . '</td>
<td>' . $row->qty . '</td>
<td>' . number_format($row->F, 2) . '</td>
<td>' . number_format($row->G, 2) . '</td>
<td>' . number_format($row->amount, 2) . '</td>
<td>' . $row->delivery_no . '</td>
<td>' . $row->d_o_creation_date . '</td>
<td>' . $row->description . '</td>
<td>' . $row->po_no . '</td>
<td>' . $row->ship_to . '</td>
<td>' . $row->tracking_no . '</td>
<td>' . $row->obs . '</td>
</tr>
';
}
?>
</table>
It seems that the only variable to consider is Billing Doc, if so, inside your while loop use an array to store the billing id as a key and then check every value to see if the key already exists then print the color you want to:
$billing_docs = [];
while ($row = $result->fetch_object()) {
echo '<tr style="background-color:' . (array_key_exists($row->id_factura, $billing_docs) ? '#666' : '#DDD') . '">...';
$billing_docs[$row->id_factura] = '';
}
You could create a variable with the first billing_doc value: $billing = $row->billing_doc and use an if else to echo the same color row if the $billing_doc value is the same. The else would declare the new color variable and echo rows of that color.

Categories