Linux Hosting
NT/2000 Hosting
DYO Internet Access
HTMLez.com
JAVASCRIPTez.com
ASPez.com
|
Sortable Tables Using a .csv File and PHP
At HTMLez.com I shared information
that I had learned at http://builder.cnet.com/webbuilding/pages/Programming/Scripter/080999/?tag=st.bl.7264cd3.plbl
about creating sortable tables on a web page. This javascript file that you can
obtain from their site.
Here I will show how to make this sortable tables become generically useful on any php enabled
web server. This example, available for downloading in .txt format (simply
save as a .php, .php3, or .php4 file as required on your server). Click here to view a sample of this
Generic Sortable Tables using PHP.
We will automate by enabling this one php file to process any .csv (comma delimited file). These
.csv files can easily be created in Excel and exported as a comma delimited file. Make the first row the Headers
for the table, being sure there is one header for each column in the table. Data fields in some columns may be
blank but be sure each column has a header value in the A row.
This php generic program functions as follows:
- Define the values to be received from the invoking link:
$tableName = $fname;
$tableWidth = $twidth;
$tableAlign = $talign;
$borderSize = $bsize;
$tableNumerics = $tnums;
- Then create javascript code from php.
echo "<scr" . "ipt language=\"javascript\">\n";
- Open the .csv file and setup some values
$fp = fopen($tableName,"r");
$needTitles = true;
$rows = 0;
$cols=0;
$continue = true;
$buffer = fgets($fp,4096);
if ($buffer == false) $continue = false;
- Start the loop for reading lines in the file and fix each line by removing white space
(the carriage return) and forcing a acomma at the end of each line (which helps establish in the first row
how many columns exist in this .css file.
while ($continue != false) {
$buffer = trim($buffer);
/* get rid of carriage returns */
$buffer = $buffer . ",";
- On the first pass, read this line, counting the commas
if ($needTitles) {
$start = 1;
$comma = ",";
$temp = $buffer;
$pos = strpos($temp,$comma);
while ($pos > 0) {
$cols = $cols + 1;
$start = $pos + 1;
$temp = substr($temp,$start);
/* bug doesn't recognize start parm in strpos */
$pos = strpos($temp,$comma);
}
$cols = $cols;
/* becaue the last column doesn't end in a comma */
}
- Initialize the output line for javascript, and create the javascript array values.
$rows = $rows + 1;
$theLine = "";
$tok = strtok($buffer,",");
while ($tok) {
$theLine = $theLine . "\""
. htmlspecialchars($tok) . "\",";
$tok = strtok(",");
}
- Remove the trailing comma so that javascript won't fail, read the next line of the .csv file, and set the conditional
if (strlen($theLine) > 0) $theLine =
substr($theLine,0,(strlen($theLine) - 1));
$buffer = fgets($fp,4096);
$continue = $buffer;
- If processing the first line, setup the Titles. Otherwise, output the javascript array statement. Close the file.
if ($needTitles) {
$needTitles = false;
$Titles = $theLine;
}
else {
if ($continue == false)
echo "new Array(" .
$theLine . ")\n";
else
echo "new Array(" .
$theLine . "),\n";
}
}
fclose($fp);
This produces a file such as this:
var data = new Array(
new Array("George","Wade","676 781-5181","5"),
new Array("Rebecca","Wade","676 781-5181","9"),
new Array("Christia","Lewis","676 375-3775","21"),
new Array("Kalamazoo","SDA School","342-8983"),
new Array("Matthew","lewis","676 375-3991","15")
)
var buttons = new Array("First Name","Last Name",
"Phone Number"," Age")
where the first javascript array is the data array and the 2nd array is the Header/Button array
We allow javascript to create the buttons and the data table with the following assistance:
var buttons = new Array(<?php echo $Titles; ?>)
var table = new SortableTable("<?php echo $tableName; ?>",
<?php echo $rows; ?>,<?php echo $cols; ?>,
<?php echo $borderSize; ?>,
"<?php echo $tableWidth; ?>%",
"<?php echo $tableAlign; ?>")
/*id,rows,columns,border,width,align*/
And we are almost finished with our customization to the Sortable Tables. We still need to setup the
definitions for sorting a column numerically (since sortable tables sorts alphabetically by default).
We do this with the following php code:
<?php
$v = $tableNumerics;
for ($i=0;$i<strlen($v);$i=$i + 2) {
$c = substr($v,$i,2);
if ($c >= 0)
echo "table.setNumeric(" . ($c - 1) . ")\n";
}
?>
And our customization is complete.
Oh, you want to know how to call this generical Sortable Tables page? Create this link:
<a href="genSortTables.php3?fname=phonelist.csv&twidth=90&talign=right&bsize=1">
MyLink</a>
or for numeric columns
<a href="genSortTables.php3?fname=phonelist.csv&twidth=90&talign=right
&bsize=1&tnums=00000004">MyLink</a>
The &tnums=00000004 parameter contains 00 for each column to be sorted alphabetically and 04 for the
fourth column to be sorted numerically.
Enjoy using sortable Tables in the future. Just download this example as instructed above
and use this code without changing anything within the script tags. Just link to the page using the parms shown.
|





|