<?php
function f__table ($table,$cols,$order,$desc) {
$comma=implode(",",$cols);
$query = 'select '.$comma.' from '.$table.' order by '. $order.' '. $desc;
$result = mysql_query($query);
if (!$result) {
$message = 'ERROR:' . mysql_error();
return $message;
}
else {
$i = 0;
echo '<table class="tablesorter" width="960px;"><thead><tr>';
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
//take table title and remove the F_prefix
$title=$meta->name;
$title=str_replace('F_','',$title);
$title=str_replace('_',' ',$title);
echo '<th>' . $title . ' </th>';
$i = $i + 1;
}
echo '</tr></thead><tbody>';
$i = 0;
while ($row = mysql_fetch_row($result))
{
echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row); $y = $y + 1;
}
echo '</tr>';
$i = $i + 1;
}
echo '</tbody></table>';
mysql_free_result($result);
}
// close connection
mysql_close();
// close function
}
?>
I have this table function which is working well for my needs as a simple display if data from my mysql database to screen using some jQuery to add some functionality i.e. sorting columns, zebra row styling etc.
However I now wish to explicitly extend my function to include a link in the html markup so for example in column 1 there might be values 1, 2, 3, 4 I wish to have a link to http://www.something.com/page/1 , /2 , /3 , /4 etc but in another example it might not be column 1 depending on my sql. I know in each case based on the fieldname which need to be links to other pages, a table might have 15 columns of which 6 of them may contain links in each td element of the table to a different page based on the value,
I can specify this in an array perhaps, but how do I build this into a function so it is dynamic and extensible across my website?
I have over 40 plus pages of data tables, charts and graphs on my website that I have coded by hand, I now wish to improve as my PHP skills have developed
I believe I will need some if statements based on the array key (fieldname) as an indicator and if the array key exists in another array pull in the value from that array… if that makes sense.
If anyone could provide an example of code or some logic and a pointer in the right direction that would be greatly appreciated.
create a function for each part of the table creation, header and display, with functions for different display properties e.g a link or image.
create an array for fieldnames akin to:
(name, width, alignment, behaviour).