Sorting table from database through PHP - php

I need to sort my table from database by clicking on the header where is Price to sort it from Low to High and if I click again then sort High to Low. I am missing how to include this in php. I tried this solution but for some reason it is not working. Feel free repair this solution or offer something else
I am using PDO method
<?php
include 'inc/header.php';
$cars = $db->prepare("SELECT name, price FROM cars");
$cars->execute();
$cars = $cars->fetchAll(PDO::FETCH_ASSOC);
?>
Here is HTML:
<table>
<thead>
<tr>
<th>Name</th>
<th>
Price
</th>
</tr>
</thead>
<tbody>
<?php foreach($cars as $car): ?>
<tr>
<td>
<?php echo $car['name'];?>
</td>
<td>
<?php echo $car['price'];?>€
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php
$orderBy = array('price');
$order = 'type';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
}
$query = 'SELECT * FROM cars ORDER BY '.$order;
)
?>

Just use Jquery DataTables, add an ID to your table then initialize Data Tables, it will allow you to sort your data by any column you want. Super easy to use and also it add cool features, such as pagination in your table for it not to be too long if you have many results. also options to export your table to pdf and other types easy.
<?php
include 'inc/header.php';
$cars = $db->prepare("SELECT name, price FROM cars");
$cars->execute();
$cars = $cars->fetchAll(PDO::FETCH_ASSOC);
?>
<!-- include dataTables from the cdn -->
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.js"></script>
<table id="dataTable">
<thead>
<tr>
<th>Name</th>
<th>
Price
</th>
</tr>
</thead>
<tbody>
<?php foreach($cars as $car): ?>
<tr>
<td>
<?php echo $car['name'];?>
</td>
<td>
<?php echo $car['price'];?>€
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
$('#dataTable').DataTable();
} );
</script>
DataTable Results :
Your results will look like.
As you can see in the image you can easily sort by anything, with less hustle.

use table sorter plugin
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="http://tablesorter.com/__jquery.tablesorter.min.js"></script>
$(document).ready(function()
{
$("#myTable").tablesorter();
}
);
<table id="myTable" >
<thead>
<tr>
<th>Name</th>
<th>
Price //u can give the table heading
</th>
</tr>
</thead>
<tbody>
<?php foreach($cars as $car): ?>
<tr>
<td>
<?php echo $car['name'];?>
</td>
<td>
<?php echo $car['price'];?>€
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
u can achieve table sorter by jquery in easiest way

Related

How to make this table with expandable rows to display other rows when expanded?

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>

Jquery Data Table Filter Custom Sorting onload?

I am Using Data table jQuery plugin for filtring my data.
this is my sql query
$sql = "SELECT * FROM project_task where emp_id ='$empid' AND task_type ='random' ORDER BY task_id DESC";
code of table is
<table id="example" class="table table-hover" style="width:100%">
<thead>
<tr style="background: #064769;color: #FFF;">
<th>id</th>
<th>Task</th>
<th > Description </th>
<th> Hours </th>
<th> Date </th>
<th> Rating </th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td> <?php echo $row["task_id"] ?></td>
<td> <?php echo $row["task"] ?></td>
<td> <?php echo $row["description"]; ?> </td>
<td> <?php echo $row["hours"]; ?> </td>
<td> <?php echo $row["submit_date"];?></td>
<td> <?php echo $row["submit_date"];?></td>
</tr>
</tbody>
</table>
as per I am Using DESC in sql query. task has been shown in DESC order but not when i remove data table plugin it is working fine. I think there is filter which is working on page load. I was using cdn now i installed it into local i am finding that there is any function which is sorting it but i have not pro in JS if you know how to solve it please tell me.
I am attaching screen shot also: which is showing tasks are sorted automatically ASC order.
this is my jQuery function for data table:
$(document).ready(function() {
$('#example').DataTable();
});
in your jquery code you have pass arguments like:
$(document).ready(function() {
$('#example').DataTable( {
"paging": true,
"ordering": false,
"info": true
} );
});
you can also see documentation for the more clarity .

PHP - Displaying database data in 2 different tables

I need to display data from a database in a table, and I was wondering if you can create 2 separate tables based on a variable.
I have code to display it all in one table:
<table>
<tr>
<th> Name </th>
<th> Event </th>
</tr>
<?php
while($row = $result->fetch_assoc())
{?>
<tr>
<td> <?php echo $row["name"] ?> </td>
<td> <?php echo $row["event"] ?> </td>
</tr>
<?php } ?>
</table>
So each person has an option of 2 fundraisers. Is there a way to create 2 separate tables based on the event variable? I know I could create another table in the database, but I want to keep all the data in one area.
I only know basic PHP and any help is appreciated.
<table>
<tr>
<th> Name </th>
<th> Event </th>
</tr>
<?php
while($row = $result->fetch_assoc())
{?>
<tr>
<td> <?php echo $row["name"] ?> </td>
</tr>
<?php } ?>
</table>
<table>
<tr>
<th> Name </th>
<th> Event </th>
</tr>
<?php
while($row = $result->fetch_assoc())
{?>
<tr>
<td> <?php echo $row["event"] ?> </td>
</tr>
<?php } ?>
</table>

Convert multiline in cell table to select option

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 }?>

How to sum values for multiple table column based on class or id [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have number of table in one page .
I want to calculate sum of all price column from all different tables & want to display that sum value as Grand total.
For example:- I have page like: Item you purchased
<!-- table for fruit items -->
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Apple</td>
<td>50</td>
</tr>
<tr>
<td>Banana</td>
<td>40</td>
</tr>
</table>
<!-- table for cloth items -->
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Tshirt</td>
<td>500</td>
</tr>
<tr>
<td>Jeans</td>
<td>600</td>
</tr>
</table>
.
.
.
<!--multiple tables for different categories -->
.
.
.
<!-- table for home items -->
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>TV</td>
<td>5000</td>
</tr>
</table>
<!-- Grand total of all item price for all tables-->
<div>
<label>Grand Total:</label>
<div>6190</div>
</div>
declare the variable in above table.
<?php $grand_total = 0 ?>
<!-- table for fruit items -->
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Apple</td>
<td><?php $grand_total+= 50 ?> 50</td>
</tr>
<tr>
<td>Banana</td>
<td><?php $grand_total+= 40 ?>40</td>
</tr>
</table>
<!-- table for cloth items -->
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Tshirt</td>
<td><?php $grand_total+= 500 ?> 500</td>
</tr>
<tr>
<td>Jeans</td>
<td><?php $grand_total+= 600 ?> 600</td>
</tr>
</table>
.
<!--multiple tables for different categories -->
<!-- table for home items -->
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>TV</td>
<td><?php $grand_total+= 5000 ?> 5000</td>
</tr>
</table>
<div>
<label>Grand Total:</label>
<div><?php echo $grand_total ?></div>
</div>
Before your <table> tag, create a variable in PHP (initially set to 0) and increment it by each value in the Price column.
Your code would look like this:
<?php $total = 0; ?>
<!-- table for fruit items -->
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Apple</td>
<td>50</td>
<?php $total += 50; ?>
</tr>
<tr>
<td>Banana</td>
<td>40</td>
<?php $total += 40; ?>
</tr>
</table>
<!-- table for cloth items -->
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Tshirt</td>
<td>500</td>
<?php $total += 500;?>
</tr>
<tr>
<td>Jeans</td>
<td>600</td>
<?php $total += 600; ?>
</tr>
</table>
.
.
.
<!--multiple tables for different categories -->
.
.
.
<!-- table for home items -->
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>TV</td>
<td>5000</td>
<?php $total += 5000; ?>
</tr>
</table>
<!-- Grand total of all item price for all tables-->
<div>
<label>Grand Total:</label>
<div>$<?php echo $total;?></div>
</div>
You can also look into PHP functions such as number_format to better format the total value.
Give individual classNames to field names.
For example:
<td class="banana">50</td>
Then you have to write something like this in JavaScript.
var bananaArray = document.getElementsByClassName("banana");
var bananaSum = 0;
for(var i=0; i<bananaArray.length; i++)
{
$bananaSum +=bananaArray[i].innerHtml or bananaArray[i].value;
}
Then create additional row for sum and append it to table.
var tr = document.createElement("tr");
var td1 = document.createElement("td");
td1.innerHTML = "Sum";
var td2 = document.createElement("td");
td2.innerHTML = bananaSum; //this is bananaSum variable from for loop
tr.appendChild(td1);
tr.appendChild(td2);
Then get your table id and append this row to it:
var table = document.getElementById("tableId)";
table.appendChild(tr);
That's all :)

Categories