i’ve been able to load up a csv file and display it as a html table but what i’m having trouble with is sorting it out with headings a to z but what i am trying to do is have 4 columns like this.
Surgestions about how to do what im after i was hoping i could span the say A across all columns and centering them in the future also but thats not really the problem mainly the getting the array of the csv file and code right iguess. any help would be appreciated
[code]
A | |||||
---|---|---|---|---|---|
Apple1 | Apple2 | Apple3 | Apple4 | Apple5 | Apple6 |
B | Banana1 | Banana2 | Banana3 | Banana4 | Banana5 | Banana6 |
C | Cold1 | Cold2 | Cold3 | Cold4 | Cold5 | Cold6 |
This is the code for inputting of the csv file i also have some other code i was wanting to use for the sorting of the array data from the csv file i’ll past below…
[code]<?php
$file = “widgets.csv”;
$delimiter = “,”;
$enclosure = ‘"’;
$column = array("", “”, “”, “”);
@$fp = fopen($file, “r”) or die(“Could not open file for reading”);
while (!feof($fp))
{
$tmpstr = fgets($fp, 100);
$line[] = preg_replace("/r/", “”, $tmpstr);
}
for ($i=0; $i < count($line); $i++)
{
$line[$i] = explode($delimiter, $line[$i]);
}
?>
".$column[$i]." | "; ?>
---|
".$line[$i][$j]." | "; } echo "
Heres the code for the sorting of the table data from the array
[code]<?php
echo “QUERY_STRING is “.$_SERVER[‘QUERY_STRING’].”
”;
$rev=1;
$sortby=0;
if(isset($_GET[‘sortby’]))$sortby = $_GET[‘sortby’];
if(isset($_GET[‘rev’]))$rev = $_GET[‘rev’];
//Open and read CSV file example has four columns
$i=-1;
$ff=fopen(‘widgets2.csv’,‘r’) or die(“Can’t open file”);
while(!feof($ff)){
$i++;
$Data[$i]=fgetcsv($ff,1024);
}
//Make columns sortable for each sortable column
foreach ($Data as $key => $row) {
$One[$key] = $row[0]; // could be $row[‘Colname’]
$Two[$key] = $row[1]; //numeric example
$Three[$key] = $row[2];
$Four[$key] = $row[3];} //numeric example
//Case Statements for Sorting
switch ($sortby){
case “0”:
if($rev==“1”)array_multisort($One, SORT_NUMERIC, $Data); //SORT_NUMERIC optional
else array_multisort($One, SORT_NUMERIC, SORT_DESC,$Data);
$rev=$rev*-1;
break;
case “1”:
if($rev==“1”)array_multisort($Two, $Data);
else array_multisort($Two, SORT_DESC, $Data);
$rev=$rev*-1;
break;
case “2”:
if($rev==“1”)array_multisort($Three, SORT_NUMERIC, $Data);
else array_multisort($Three, SORT_NUMERIC, SORT_DESC,$Data);
$rev=$rev*-1;
break;
case “3”:
if($rev==“1”)array_multisort($Four, $Data);
else array_multisort($Four, SORT_DESC, $Data);
$rev=$rev*-1;
break;}
echo ("
<a href=“SortTable.php?sortby=0&rev=$rev” title=“Click to Sort/Reverse sort”>One | ");<a href=“SortTable.php?sortby=1&rev=$rev” title=“Click to Sort/Reverse sort”>Two | ");<a href=“SortTable.php?sortby=2&rev=$rev” title=“Click to Sort/Reverse sort”>Three | ");<a href=“SortTable.php?sortby=3&rev=$rev” title=“Click to Sort/Reverse sort”>Four | ");
---|
$field | ";