My project is how to showing cell of table not in text, but in select option.
I have simple code:
<form method="post">
<textarea name="input"></textarea>
<button type="submit" name="button">button</button>
<table class="table table-bordered" id="tableAll">
<thead>
<tr>
<th class="col-md-1">name</th>
</tr>
</thead>
<tbody>
<tr>
<?php
if(isset($_POST['button']))
{
echo "<td>".$_POST["input"]."</td>";
}
?>
</tr>
</tbody>
</table>
</form>
<style>
td { white-space:pre }
</style>
I was input in textarea like this:
Andy
Alex
John
etc...
So, from input I want convert cell of table to select option in my page like this:
<table class="table table-bordered" id="tableAll">
<thead>
<tr>
<th class="col-md-1">name</th>
</tr>
</thead>
<tbody>
<tr>
<select>
<option>Andy</option>
<option>Alex</option>
<option>John</option>
<option>etc...</option>
</select>
</tr>
</tbody>
</table>
You can do it like below:-
<?php
if(isset($_POST["input"]))
{
$select_data = explode("\n",$_POST["input"]); // explode input data with new line
}
?>
<?php if(count($select_data) >0){?>
<select>
<?php
foreach($select_data as $select_dat){?>
<option value = "<?php echo $select_dat;?>"><?php echo $select_dat;?></option>
<?php }?>
</select>
<?php }?>
Related
I have a datatable in HTML which i am populating with dynamic data from PHP. each row is expandable but what I want is to have child rows for each parent row. So, for example there is a device type with the number 4 which contains 20 different devices, so I want to display in the parent row Device number 4 and when I expand that row display the 20 devices (with headers such as IMEI, Serial no., etc.)
Here is the table I have right now:
See screenshot of my table
EDIT: Added current table (no child rows)
<table id="example" class="table table-bordered table-striped" cellspacing="0" width="100%">
<thead>
<th>Device</th>
<th>Dev.no</th>
<th>Barcode</th>
<th>IMEI</th>
<th>Serial no.</th>
<th>Date added on stock</th>
</thead>
<tbody>
<?php if (!empty($devices)) { ?>
<?php foreach ($devices as $device) : ?>
<tr>
<td>
<?php echo $device["name"] ?>
</td>
<td>
<?php echo $device["device_no"] ?>
</td>
<td>
<?php echo $device["barcode"] ?>
</td>
<td>
<?php echo $device["serial_imei"] ?>
</td>
<td>
<?php echo $device["serial_no"] ?>
</td>
<td>
<?php echo $device["created_date"] ?>
</td>
</tr>
<?php endforeach; ?>
<?php } ?>
</br>
</tbody>
</table>
I think you're looking for something like this https://datatables.net/reference/api/row().child().
Then you can add a table in the child row with more information.
Note: It looks like you have the responsive option enabled (green plus button that hides/shows hidden columns). If you have a child row open and try to show the hidden columns, it might overwrite your child row.
Edit:
You need to specify the HTML you want to put in the child. I would personally use ajax to retrieve the children when I wanted to display them. Otherwise the data needs to be hidden somewhere in the page.
I've created a codepen that puts the HTML in a data attribute on the row. Then you can click a button to view the child rows (devices). Obviously, there are many other ways to store the data on the page.
$(document).on("click", ".viewChildren", rowClickHandler);
$("#example").DataTable();
function rowClickHandler() {
var btn = $(this);
var tr = btn.closest('tr');
var dtRow = $("#example").DataTable().row(tr);
if (dtRow.child.isShown()) {
dtRow.child.hide();
btn.text('Show Children');
} else {
var children = tr.data("children");
dtRow.child(children).show();
btn.text('Hide Children');
}
}
th{
text-align: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.20/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" />
<table id="example" class="table table-bordered table-striped" cellspacing="0" width="100%">
<thead>
<tr>
<th>Device</th>
<th>Dev.no</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr data-children='<table>
<thead>
<tr>
<th>barcode</th>
<th>imei</th>
<th>serial_no</th>
<th>created_date</th>
</tr>
</thead>
<tbody>
<tr>
<td>123456</td>
<td>11324</td>
<td>654654</td>
<td>2020-01-17</td>
</tr>
<tr>
<td>1234987</td>
<td>1654</td>
<td>654687</td>
<td>2020-04-30</td>
</tr>
</tbody>
</table>'>
<td>Laptop</td>
<td>2</td>
<td><button class = 'viewChildren'>See Children</button></td>
</tr>
</tbody>
</table>
In form I have two table in form, for example table#1, table#2. both tables are sent only one $_POST. How to get data form e.g table#1 ? Is it possible to select in some way in $ _POST ['table # 1']?
<form id="prepForm" action="" method="POST">
<table id="1" class="table tabele-conclusions table-bordered">
<thead>
<tr>
<th>Lp.</th>
<th>desc</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td><input name="row1_desc" value=""/></td>
</tr>
</tbody>
</table>
<table id="2" class="table tabele-conclusions table-bordered">
<thead>
<tr>
<th>Lp.</th>
<th>desc</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">2</th>
<td><input name="row2_desc" value=""/></td>
</tr>
</tbody>
</table>
Simple examples:
Html
<form action="/post.php" method="POST">
<input type="text" name="testinput" value="Hello world!" />
</form>
Php:
<?php
echo $_POST["testinput"]; // Hello world!
?>
Edit: When I gave this reply, there was no sample code in the question.
Table_sup |
+-------------------+
| supid=>int |
| scompany=>varchar |
| sstate=>varchar |
| scity=>varchar |
| scat=>varchar |
PHP Code
<?php
include "db_connect.php"; // including configuration file
?>
<form name="frmdropdown" method="post" action="sample.php">
<center>
<h2 align="center">Select State</h2>
<strong> Select State : </strong>
<select name="getData">
<option value=""> -----------ALL----------- </option>
<?php
$dd_res=mysqli_query($con,"Select DISTINCT sstate from sup");
while($r=mysqli_fetch_row($dd_res))
{ echo "<option value='$r[0]'> $r[0] </option>";}
?>
</select>
<input type="submit" name="Select" value="Select"/>
<br><hr>
<table id="exampleL" class="display table table-striped table-bordered dt-responsive nowrap" cellpadding="1" cellspacing="1" width="" style="font-size:small;">
<thead>
<tr>
<th style=" background-color:lightblue">Company</th>
<th style=" background-color:lightgreen">City</th>
<th style=" background-color:lightgreen">State</th>
<th style=" background-color:lightgreen">Category</th>
</tr>
</thead>
<tbody>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$des=$_POST["getData"];
if($des=="") // if ALL is selected in Dropdown box
{
$res=mysqli_query($con,"Select * from sup");
}
else
{
$res=mysqli_query($con,"Select * from sup where sstate='".$des."'");
}
//echo "<tr><td colspan='5'></td></tr>";
while($row3=mysqli_fetch_array($res)){
?>
<tr>
<td><?php echo $row3['scompany'];?></td>
<td><?php echo $row3['scity'];?></td>
<td><?php echo $row3['sstate'];?></td>
<td><?php echo $row3['scat'];?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</center>
</form>
By Default there is no data populated in the table.
When I press "select", then all data populate in the table.
What I want: by default, the table populated all data
I have a default table image after running the code.If required I can upload the image
<?php
include "db_connect.php"; // including configuration file
?>
<form name="frmdropdown" method="post" action="sample.php">
<center>
<h2 align="center">Select State</h2>
<strong> Select State : </strong>
<select name="getData">
<option value=""> -----------ALL----------- </option>
<?php
$dd_res=mysqli_query($con,"Select DISTINCT sstate from sup");
while($r=mysqli_fetch_row($dd_res))
{ echo "<option value='$r[0]'> $r[0] </option>";}
?>
</select>
<input type="submit" name="Select" value="Select"/>
<br><hr>
<table id="exampleL" class="display table table-striped table-bordered dt-responsive nowrap" cellpadding="1" cellspacing="1" width="" style="font-size:small;">
<thead>
<tr>
<th style=" background-color:lightblue">Company</th>
<th style=" background-color:lightgreen">City</th>
<th style=" background-color:lightgreen">State</th>
<th style=" background-color:lightgreen">Category</th>
</tr>
</thead>
<tbody>
<?php
$res=mysqli_query($con,"Select * from sup");
// If select was clicked, then only where condition applies, and so $res will be replaced.
if(isset($_POST['getData'] ) || !empty($des))
{
$des=$_POST["getData"];
$res = mysqli_query($con,"Select * from sup where sstate='".$des."'");
}
//echo "<tr><td colspan='5'></td></tr>";
while($row3=mysqli_fetch_array($res)){
?>
<tr>
<td><?php echo $row3['scompany'];?></td>
<td><?php echo $row3['scity'];?></td>
<td><?php echo $row3['sstate'];?></td>
<td><?php echo $row3['scat'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
</center>
</form>
Need to display retrieved data from mysql table in table format, but not getting the right display i planned for. This is how i want it to look like
Target display
But this is what am getting based on my code
Current display
This is the html code
<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff first name, last name, department and action (edit and delete links)
?>
<tr>
<td>
<?php
echo $staff["first_name"];
}
?>
</td>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff last name
?>
<td>
<?php
echo $staff["last_name"];
}
?>
</td>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff department
?>
<td>
<?php
echo $staff["department"];
}
?>
</td>
<td>
<span class="glyphicon glyphicon-pencil"> Edit</span>
 
<span class="glyphicon glyphicon-trash"> Delete</span>
</td>
</tr>
</tbody>
</table>
</div>
Here is the function that am using to find the list of all employee from the my staff table
function find_all_employee() {
global $connection;
$query = "select * from staff";
$staff_set = mysqli_query($connection, $query);
confirm_query($staff_set);
return $staff_set;
}
Is there a better way to write the loop and display my data in the right way? I have looked up similar threads but still not able to grasp.
I dont understand why you calling so much tie function and also why making so much loops. Let me try fi your code:
<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff first name, last name, department and action (edit and delete links)
?>
<tr>
<td>
<?php echo $staff["first_name"]; ?>
</td>
<td>
<?php echo $staff["last_name"]; ?>
</td>
<td>
<?php echo $staff["department"]; ?>
</td>
<td>
<span class="glyphicon glyphicon-pencil"> Edit</span>
 
<span class="glyphicon glyphicon-trash"> Delete</span>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Since OP asked for a varying technique in a comment:
<?php
// Select all staff first name, last name, and department info
$staff_set = find_all_employee();
$staff_results = array();
while ($staff = mysqli_fetch_assoc($staff_set)) {
$staff_results[] = $staff;
}
?>
<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php if (!empty($staff_results)): ?>
<?php foreach($staff_results as $staff_member): ?>
<tr>
<td>
<?= $staff_member["first_name"]; ?>
</td>
<td>
<?= $staff_member["last_name"]; ?>
</td>
<td>
<?= $staff_member["department"]; ?>
</td>
<td>
<span class="glyphicon glyphicon-pencil"> Edit</span>
 
<span class="glyphicon glyphicon-trash"> Delete</span>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr><td colspan="4">No Staff Members Found.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
This also introduces the short tag syntax (<?=) for replacing calls to echo. Read this thread if your PHP version is < 5.4. This also introduces control structure alternate syntax, for better readability with HTML. The above approach separates the concern of selecting data from the DB from the presentation of the HTML. This allows for easier debugging, as well as future adaptation and modularization of your application.
Can I use a div inside the table given here. I can't use the div tag inside foreach.
here is my code
<table border="1" class="table table-stripped table-bordered">
<thead>
<tr>
<td></td>
<?php foreach($tests as $test){?>
<td><?php echo $test->name;?></td>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($tests[0]->test_attributes as $test_attribute){?>
<tr>
<td>
<?php echo $test![enter image description here][1]_attribute->attribute_name;?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<table border="1" class="table table-stripped table-bordered">
<thead>
<tr>
<td> </td>
<?php foreach($tests as $test){?>
<td><div><?php echo $test->name;?></div></td>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($tests[0]->test_attributes as $test_attribute){?>
<tr>
<td>
<div><?php echo test_attribute->attribute_name;?></div>
</td>
</tr>
<?php } ?>
</tbody>
</table>