OsakaWebbie
Programmer
I build an HTML table based on data from a database, and depending on various user options, which columns are included changes quite a bit. I used to repeat a complex conditional structure three times: for an array of the class names and a parameter used to set up jQuery code, for the column headers, and for the data. But I'm tired of that mess, so I added the header text to the array, and thought I would do the same thing for the data. But of course the data is dynamic...
The following code is simplified, but imagine a lot more than these examples, intertwined in a labyrinth of if statements based on user selections. The array has values like this:
And after I display the headers I loop through my data and do this:
The first way I thought of to combine them was something like this (slapped together for this example - untested):
But then I'd have to run eval("echo...") for every cell in my <tbody>! I'm sure that would be horribly slow, and I would have to carefully check all string values for hackware. The other idea I had was something like this:
It's a safer solution, but I don't know if it's much faster, as it would still have to run through a big switch (16 cases, only some of will be in any given table) for every cell.
Thoughts?
The following code is simplified, but imagine a lot more than these examples, intertwined in a labyrinth of if statements based on user selections. The array has values like this:
PHP:
$cols[] = array("ddate",1,"<th class=\"ddate\">"._("Date")."</th>");
$cols[] = array("amount-for-csv",1,"<th class=\"amount-for-csv\" style=\"display:none\">"._("Amount")."</th>");
$cols[] = array("amount-for-display",0,"<th class=\"amount-for-display\">"._("Amount")."</th>");
PHP:
echo "<td class=\"ddate\">".$row->DonationDate."</td>\n";
echo "<td class=\"amount-for-csv\" style=\"display:none\">".number_format($row->Amount,$_SESSION['currency_decimals'],".","")."</td>\n";
echo "<td class=\"amount-for-display\"><span style=\"display:none">".sprintf("%012s",$row->Amount)."</span>".$_SESSION['currency_mark']." ".number_format($row->Amount,$_SESSION['currency_decimals'])."</td>\n";
PHP:
$cols[] = array("ddate",1,"<th class=\"ddate\">"._("Date")."</th>","<td class=\"ddate\">".$row->DonationDate."</td>");
$cols[] = array("amount-for-csv",1,"<th class=\"amount-for-csv\" style=\"display:none\">"._("Amount")."</th>","<td class=\"amount-for-csv\" style=\"display:none\">".number_format($row->Amount,$_SESSION['currency_decimals'],".","")."</td>");
$cols[] = array("amount-for-display",0,"<th class=\"amount-for-display\">"._("Amount")."</th>","<td class=\"amount-for-display\"><span style=\"display:none">".sprintf("%012s",$row->Amount)."</span>".$_SESSION['currency_mark']." ".number_format($row->Amount,$_SESSION['currency_decimals'])."</td>");
PHP:
foreach ($cols as $col) {
switch ($col[0]) {
case "ddate":
echo "<td class=\"ddate\">".$row->DonationDate."</td>\n";
break;
case "amount-for-csv":
echo "<td class=\"amount-for-csv\" style=\"display:none\">".number_format($row->Amount,$_SESSION['currency_decimals'],".","")."</td>\n";
break;
...etc...
Thoughts?