CSV to Html tables and sorted into Categorys (headings) By Alphabet

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


Apple1 Apple2 Apple3 Apple4 Apple5 Apple6
Banana1 Banana2 Banana3 Banana4 Banana5 Banana6
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…

$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]);

Albert's Delimited Text to HTML Table Converter <?php for ($i=0; $i<count($column); $i++) echo ""; ?> <?php for ($i=0; $i < count($line); $i++) { echo ""; for ($j=0; $j < count($line[$i]); $j++) { echo ""; } echo ""; } fclose($fp); ?>

Heres the code for the sorting of the table data from the array


if(isset($_GET[‘sortby’]))$sortby = $_GET[‘sortby’];
if(isset($_GET[‘rev’]))$rev = $_GET[‘rev’];

//Open and read CSV file example has four columns
$ff=fopen(‘widgets2.csv’,‘r’) or die(“Can’t open file”);

//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);

case “1”:
if($rev==“1”)array_multisort($Two, $Data);
else array_multisort($Two, SORT_DESC, $Data);

case “2”:
if($rev==“1”)array_multisort($Three, SORT_NUMERIC, $Data);
else array_multisort($Three, SORT_NUMERIC, SORT_DESC,$Data);

case “3”:
if($rev==“1”)array_multisort($Four, $Data);
else array_multisort($Four, SORT_DESC, $Data);

echo ("

"); //$rev is reverse variable
echo ("");
echo ("");
echo ("");
echo ("");

foreach($Data as $NewDat){
if($NewDat[0]!=""){ //error trap

foreach($NewDat as $field)$str.="";
echo $str;}}
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
Sponsor our Newsletter | Privacy Policy | Terms of Service