Categories

Tools

bitdefender

1&1 Web Hosting

Create a sortable table with php and javascript


The following example shows how to create a sortable table, using php and javascript.

<style>
	#products-table {
		border: 1px solid #ccc;
		padding: 0;
		width: 300px;
	}
	
	#products-table th {
		background-color: #efefef;
		border: none;
	}
	
	#products-table td {
		border: none;
		border-bottom: 1px dotted #333;
	}
</style>

<script src="js/sorttable.js"></script>

<?php
// Create a multidimensional array
// You can load data from database
$products  = array(	array(  "Motherboard",
						"CPU",
						"RAM",
						"Hard Disk",
						"DVD",
						"SSD",
						"Case",
						"Monitor",
						"Video Card",    
						"Audio Card"
						),
				array(56,
					88,
					35,
					60,
					15,
					90,
					70,
					140,
					68,
					44)
            );
?>

<table id="products-table" class="sortable" border="1">
	<tr>
		<th style="text-align: left;">Product</th>
		<th style="text-align: right;">Price</th>
	</tr>
	<?php
		for($n=0; $n < count($products[0]); $n++)
		{
		echo "<tr>";
		echo "<td style='text-align: left;'>" .  $products[0][$n] . "</td>";
		echo "<td style='text-align: right;'>" .  "€ " .  $products[1][$n] . "</td>";
		echo "</tr>";
		}
	?>
</table>

<script>
	$('tbody').sortable();
</script>

Examples:

Sortable html table

You can find more details and download the sorttable.js library code on this page:

http://www.kryogenix.org/code/browser/sorttable/

 

Animated gif created with http://gifcreator.me/

Posted in PHP by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.