Add a button in MySQL results using PHP - php

while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
$colNames = array_keys(reset($rows));
echo "<thead>";
echo "<tr>";
foreach ($colNames as $colName) {
echo "<th>$colName</th>";
}
echo "<th>SEND EMAIL</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody >";
echo "<tr>";
foreach ($rows as $row) {
foreach ($colNames as $colName) {
echo "<td>" . $row[$colName] . "</td>";
}
echo "<td><a href='Email.php?StudentID=???' class='btn btn-default left-margin'>Email</a></td>";
echo "</tr>";
}
echo "</tbody>";
I am trying to add a button to each of the rows, So when a user clicks the button the correct ID is sent by POST. The column Name that the id resides is called StudentID

Related

Why Session Variable Sending only last row data to another page

here the problem is i need to view the whole row data in view details page when the button is clicked but when i click the last row data is sent through session variable and last row is displayed but i need particular row to display fully when the name is selected of the employee. please help me out in this .
thank you
index.php
<?php
include('connection.php');
$sql = "SELECT empname,salary,contact_no,department,empdesg FROM add_emp ORDER BY empname ASC ";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
?>
<center>
<table cellpadding="20px" cellspacing="40px;" border="2px" align="center" width="device">
<tr>
<th>Name</th>
<th>Department</th>
<th>Designation</th>
<th> </th>
<?php
while($row = $result->fetch_assoc())
{
$name='';
$name=$row["empname"];
$department=$row["department"];
$designation=$row["empdesg"];
?>
</tr>
<td>
<?php echo $name?>
</td>
<td>
<?php echo $department?>
</td>
<td>
<?php echo $designation?>
</td>
<td><a href="viewdetails.php">
<button>Details</button>
</a>
<?php
$_SESSION['name']= $name;
?>
</td>
<?php
}
}
else {
echo "0 results";
}
?>
viewdetails.php
<?php
session_start();
include('connection.php');
$sql="select * from add_emp where empname='$_SESSION[name]'";
$result = mysqli_query($con, $sql); // First parameter is just return of "mysqli_connect()" function
echo "<br>";
echo " <table cellpadding='5px' cellspacing='10px' border='1px' align='center'>";
while ($row = mysqli_fetch_assoc($result))
{ // Important line !!! Check summary get row on array ..
echo "<tr>";
echo "<th>" ."Name". "</th>";
echo "<td>" .$row['empname']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."DOB". "</th>";
echo "<td>" .$row['eage']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Adhaar Number". "</th>";
echo "<td>" .$row['adhaar']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Address". "</th>";
echo "<td>" .$row['address']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Salary". "</th>";
echo "<td>" .$row['salary']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Phone Number". "</th>";
echo "<td>" .$row['contact_no']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Department". "</th>";
echo "<td>" .$row['department']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Designation". "</th>";
echo "<td>" .$row['empdesg']. "</td>";
echo "</tr>";
echo "<tr>";
}
echo "</table>";
?>

Adding datatables to a table (wordpress)

I'm trying to add datatables to my project and it shows up with the arrows and the search bar, but neither work.
I tried testing the code on a page with just the scripts and table and it worked but when I moved it to my wordpress site I run into problems.
Any suggestions? I have the jquery and datatables scripts and css in the header page, as well as the script for the table itself after the /head:
<script>
$(document).ready(function(){
$('#myTable').DataTable();
});
</script>
My table itself looks like
echo "<table id='myTable' class='display table' width='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Foster ID</th>";
echo "<th>Foster's Name</th>";
echo "<th>City</th>";
echo "<th>E-Mail</th>";
echo "</tr>";
echo "</thead>";
if(!empty($result)){
foreach ($result as $results){
$fosterId = $results->memberId;
$fosterName = $results->memberName;
$city = $results->city;
$email = $results->email;
echo "<tbody>";
echo "<tr>";
echo "<td>$fosterId</td>";
echo "<td>$fosterName</td>";
echo "<td>$city</td>";
echo "<td>$email</td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td colspan='5'>No Fosters</td>";
echo "<tr>";
echo "</tbody>";
}
echo "</table>";
Your PHP code contains tbody element in the loop. Also DataTables doesn't support colspan attribute in table body.
Corrected PHP code:
echo "<table id='myTable' class='display table' width='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Foster ID</th>";
echo "<th>Foster's Name</th>";
echo "<th>City</th>";
echo "<th>E-Mail</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($result as $results){
$fosterId = $results->memberId;
$fosterName = $results->memberName;
$city = $results->city;
$email = $results->email;
echo "<tr>";
echo "<td>$fosterId</td>";
echo "<td>$fosterName</td>";
echo "<td>$city</td>";
echo "<td>$email</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
Corrected JavaScript code:
$(document).ready(function(){
$('#myTable').DataTable({
"language": {
"emptyTable": "No Fosters"
}
});
});

Drupal SQL table how to insert edit button on each row

I've got a table in Drupal with the following code:
$db = mysql_connect("localhost", "root", "moocow");
mysql_select_db("vedb", $db);
$result = mysql_query("SELECT NodeID,NodeDesc,NodeZone,Fusion,DSLID FROM `nodeidtable` WHERE DSLID != '' AND `NodeZone` = 'CLOSED' ORDER BY NodeID ASC");
$num_rows = mysql_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>CLOSED SITES</th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "</tr>";
echo "<tr>";
echo "<th>Node ID</th>";
echo "<th>Node Address</th>";
echo "<th>Node Zone</th>";
echo "<th>Fusion Status</th>";
echo "<th>Service Number</th>";
echo "</tr>";
//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
echo "<br>";
echo "<tr>";
echo "</table>";
mysql_free_result($result);
mysql_close($db);
?>
Now I can change it renders the td to include the individual columns, but I really want to add a little edit button on the right-hand side which will let me edit that particular row fields.
Any ideas?
Replace your while loop with the following code:
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
//create link for current node edit
echo "<td align='center'>". l(t('Edit this node'), 'node/' . $row['NodeId'] . '/edit') ."</td>";
echo "</tr>";
}
Remove echo "<br>"; and echo "<tr>"; below to while loop. This will resolve the issue for you

Why does PHP nested foreach loop displays only first row from sql database

I am trying to display a table from database using two nested foreach loops with some criteria. here is the code-
echo "<div class='container'>";
echo "<table class='table table-hover table-bordered table-condensed' style='width:95%' align='center'>";
echo "<tr>";
echo "<th>";
echo "Edit";
echo "</th>";
echo "<th>";
echo "Delete";
echo "</th>";
echo "<th>";
echo "Sl. No.";
echo "</th>";
echo "<th>";
echo "Group";
echo "</th>";
echo "<th>";
echo "Component";
echo "</th>";
echo "<th>";
echo "Quantity";
echo "</th>";
echo "</tr>";
if($rslt->rowCount() > 0)
{
foreach($rslt as $item)
{
foreach($rslt3 as $item3)
{
/*echo $item3['component'];
if($item3['component']===$item['component'])
{
if($Qty>=$item3['Qty'])
{
$item3[Qty]=$item3[Qty]-$item[Qty];
*/
//will implement this after the second loop starts working
$id = $item['entry_id'];
$Qty = $item['Qty'];
$group_ID = $item['group_ID'];
$component = $item['component'];
$vendor_ID = $item['vendor_ID'];
echo "<tr>";
echo "<td>";
echo "<a href='production_edit.php?id=$id&Qty=$Qty&group_ID=$group_ID&component=$component&vendor_ID=$vendor_ID'>Edit</a>";
echo "</td>";
echo "<td>";
echo "<a href='production_delete.php?id=$id&vendor_ID=$vendor_ID'>Delete</a>";
echo "</td>";
echo "<td>";
echo $item['entry_id'];
echo "</td>";
echo "<td>";
echo $item['group_ID'];
echo "</td>";
echo "<td>";
echo $item['component'];
echo "</td>";
echo "<td>";
echo $item['Qty'];
echo "</td>";
echo "</tr>";
}
}
}
echo "</table></div><br>";
}
Now the problem here is when I use the second foreach loop the table displays the first entry in each table row... I am curious where I am at fault with this second foreach loop.. Thanks in advance

Create table with foreach without repeating table headings

I want to create a html table dynamically with php and include data from two arrays.
However, the code I have written repeats the table headings for each row (pun not intented).
How can I create a table that only has the table heading tags at the top?
Here is my code:
foreach (array_combine($even, $odd) as $products => $numbers) {
echo "<table border='1'>";
echo "<tr>";
echo "<th>Product name</th>";
echo "<th>Sold</th>";
echo "</tr>";
echo "<tr>";
print("<td>" . ($products) . "</td>");
print("<td>" . ($numbers) . "</td>");
echo "</tr>";
echo "</table>";
}
}
Just put the table header out of your foreach look:
echo "<table border='1'>";
echo "<tr>";
echo "<th>Product name</th>";
echo "<th>Sold</th>";
echo "</tr>";
foreach (array_combine($even, $odd) as $products => $numbers) {
echo "<tr>";
print("<td>" . ($products) . "</td>");
print("<td>" . ($numbers) . "</td>");
echo "</tr>";
}
echo "</table>";

Categories