0

I'm totally new with PHP and all that. I want to make a sortable table using a CSV file. I can get the file in PHP, but my table has four columns, but its shows all contents of my CSV file in the first column in my table.

My CSV file looks like this:

Timestamp;Company;Employee;Type
06/02/2013;4298;2001;start
......... and so on....

This is my php for now:

<?php
echo "QUERY_STRING is ".$_SERVER['QUERY_STRING']."<p>"; 
$rev=1;
$sortby=0;
if(isset($_GET['sortby']))$sortby = $_GET['sortby']; 
if(isset($_GET['rev']))$rev = $_GET['rev'];



$i=-1;
$ff=fopen('...file.csv','r') or die("Can't open file");
while(!feof($ff)){
$i++;
$Data[$i]=fgetcsv($ff,1024);
}


foreach ($Data as $key => $row) {
$Timestamp[$key]  = $row['Colname']; 
$Company[$key] = $row['Colname']; 
$Employee[$key] = $row['Colname'];
$Type[$key] = $row['Colname'];} 



switch ($sortby){
case "0":
if($rev=="1")array_multisort($Timestamp, SORT_NUMERIC, $Data); 
else array_multisort($Timestamp, SORT_NUMERIC, SORT_DESC,$Data);
$rev=$rev*-1;
break;

case "1":
if($rev=="1")array_multisort($Company,  $Data);
else array_multisort($Company, SORT_DESC, $Data);
$rev=$rev*-1;
break;

case "2":
if($rev=="1")array_multisort($Employee, SORT_NUMERIC, $Data);
else array_multisort($Employee, SORT_NUMERIC, SORT_DESC,$Data);
$rev=$rev*-1;
break;

case "3":
if($rev=="1")array_multisort($Type,   $Data);
else array_multisort($Type,  SORT_DESC, $Data);
$rev=$rev*-1;
break;}

echo ("<table border=2>");     
echo ("<th><a href=\"realtime3.php?sortby=0&rev=$rev\" title=\"Click to Sort/Reverse           sort\">Timestamp</a></th>");
echo ("<th><a href=\"realtime3.php?sortby=1&rev=$rev\" title=\"Click to Sort/Reverse sort\">Company</a></th>");
echo ("<th><a href=\"realtime3.php?sortby=2&rev=$rev\" title=\"Click to Sort/Reverse sort\">Employee</a></th>");
echo ("<th><a href=\"realtime3.php?sortby=3&rev=$rev\" title=\"Click to Sort/Reverse sort\">Type</a></th>");

$i=0;
foreach($Data as $NewDat){
if($NewDat[0]!=""){ //error trap
$str="<tr>";
foreach($NewDat as $field)$str.="<td>$field</td>";
$str.="</td>\n";
echo $str;}}
fclose($ff);
echo "</table>";
?>  

If somebody could help my in the good direction, much appreciated. And is it possible to add a search field/drop down field/...? This would allow for a user to easily get a better look at the table, when he typed or selected an input to search for? And I tried to make a pagination to for the table, with lets say shows 15 rows at a time. I have made it separately but I can't get it to work correctly.

4

1 に答える 1

0

あなたのコードは混乱しているように見えますが、おそらく問題なく動作します$str.="</td>\n";$str.="</tr>\n";

于 2013-02-19T18:03:25.523 に答える