Reusable Table Functions

Ross wrote:

I am seeking your advice on the programming language for such an action. i prefer a web table capable of sorting by size, length and so on, therefore built-in function for sorting is appreciated.

Down below are a couple of nice PHP functions that may be of use. I’ve taken them from my CMS at work and munged them a little to make them independent of it. I’ve not tested these munged ones, so they may need a little adjusting.

Below the PHP code are some instructions on how they should be used.

$caption

\n”;
print “

\n”;
if ($caption!=” && $options[‘h2’]!=1) print “

\n”;
print “

“;
$i = 0;
foreach ($headings as $h)
{
print “

“;
$i++;
}
print “

\n

\n”;

$lines = explode(“\n”,$data);
$i = 0;
while ($l = array_shift($lines))
$s[$i++] = explode(“|”,$l);

if(isset($_GET[‘sort’]))
usort($s,”insert_datatable_cmp”);

foreach ($s as $S)
{
print “

“;
for($i=0;$S[$i];$i++)
{
if ($options[’email:’.$i]==1)
{
$S[$i] = ‘‘ . $S[$i] . ‘‘;
}
elseif ($options[‘web:’.$i]==1)
{
$S[$i] = ‘‘ . $S[$i] . ‘‘;
}

if ($options[‘join:’.$i.’:’.($i+1)]==1)
{
print ‘

‘;
$i++;
}
else
{
print ‘

‘;
}
}
print “

\n”;
}

print “

$caption
$h
‘ . $S[$i] . ‘ ‘ . $S[$i+1] . ‘ ‘ . $S[$i] . ‘

\n”;
}
1?>

To create the table you posted, you would do this:

1);
$c = ‘My Caption’;

insert_datatable($d, $h, $o, $c);
1?>

As you can see, the first argument for the insert_datatable() function is a pipe-seperated table of data. The second argument is a PHP array of headings. The third argument is an array of “options” (explained later). The last argument is a caption for the table. The first two arguments are required. The last two are optional. (But if you include a caption, you must also include the options array!)

The options are set like this:

1, ‘option2’=>1, ‘option3’=>1);
1?>

so that the “=>1” means “switch this option on”.

What options can be used? “web:X” means that column number X is a web link. “email:X” means that column X is an e-mail address. “join:X:Y” means that columns X and Y should be joined (useful if X is a person’s first name and Y is their surname!). Column numbers start from 0, not 1.

That’s about it!