0

How can I sort this table by columns - when I click on choosen header (names of headers would be links), table would sort by that header - column, in my PHP:

<?php

open text file (first row is of headers which are divided by ;)

$fp = fopen('C:\...\usedlicences.txt','r') or die("can't open file");

create table with border

echo "<table id='MyTable' border='1'>\n";

create header row for table with headers

echo "<td><b>id</b><td><b>name</b></td><td><b>surname</b></td><td><b>address</b></td><td><b>state</b></td><td><b>phone</b></td><td><b>city</b></td><td><b>date</b></td><td><b>color</b></td>";                          
$length = 1000;
$delimiter = ";";      
$k=1;

from file create rows and populate them with data (skip first (header) row) and add first column where id of row is written

$csv_line = fgetcsv( $fp, $length, $delimiter); 
while($csv_line = fgetcsv( $fp, $length, $delimiter ) ) {
echo "<tr>";            
echo "<td>$k</td>";
$k++;   
for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
echo '<td>'.$csv_line[$i].'</td>';  
}
}
echo "<tr>";   
echo '</table>';
fclose($fp) or die("can't close file");
?>

for showing alert how much licences are used (all rows minus header row)

<script language="JavaScript">
var oRows = document.getElementById('MyTable').getElementsByTagName('tr');
var iRowCount = oRows.length-1;
alert('Licences used: ' + ((iRowCount)-1)+'!');
</script>

test.txt looks like:

id;name;surname;address;state;phone;city;date;color
1;John;Simts;Yellow 12;Greenik;1234567;Mannds;12/3/1234;blue    
3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.