[eluser]LeonardoGaiero[/eluser]
Greetings, all. I have been working on a personalized table generation library, using CI 1.7.2, to create a table which changes layout depending on values pulled from a database. It's currently structured like this:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Table extends CI_Table {
function generate($table_data = NULL)
{
// (truncated for length, up until here it's the same as the default lib)
// Build the table rows (start of edits)
if (count($this->rows) > 0)
{
$i = 1;
foreach($this->rows as $row)
{
if ( ! is_array($row))
{
break;
}
if(array_key_exists('tic_id', $row)) {
$row['tic_id']='<span class="bold">'.$row['tic_id'].'</span>';
}
if(array_key_exists('tic_subject', $row)) {
$row['tic_subject']='<span class="bold">'.$row['tic_subject'].'</span>';
}
if(array_key_exists('tic_status', $row)) {
$row['tic_status']=$ci->helpdesk->getReadableStatus($row['tic_status']);
}
if(array_key_exists('tic_type', $row)) {
$row['tic_type']=$ci->helpdesk->getReadableType($row['tic_type']);
}
// We use modulus to alternate the row colors
$name = (fmod($i++, 2)) ? '' : 'alt_';
$out .= $this->template['row_'.$name.'start'];
$out .= $this->newline;
if(array_key_exists('tic_priority', $row)) {
$row['tic_priority']=$ci->helpdesk->getReadablePriority($row['tic_priority']);
switch ($row['tic_priority']) {
case "bassa":
$row['tic_priority']='<div class="lopri logview">'.$row['tic_priority'].$row.'</div>';
break;
case "media":
$row['tic_priority']='<div class="medpri logview">'.$row['tic_priority'].$row.'</div>';
break;
case "alta":
$row['tic_priority']='<div class="hipri logview">'.$row['tic_priority'].$row.'</div>';
break;
default:
echo ('<div></div>');
}
}
foreach($row as $cell)
{
$out .= $this->template['cell_'.$name.'start'];
if ($cell === "")
{
$out .= $this->empty_cells;
}
else
{
$out .= $cell;
}
$out .= $this->template['cell_'.$name.'end'];
}
$out .= $this->template['row_'.$name.'end'];
$out .= $this->newline;
}
}
$out .= $this->template['table_close'];
return $out;
}
}
What's going on here is that the new code intercepts the specified rows and modifies their values, calling a getReadable function which simply assigns a human-readable text to the retrieved value.
My problem, however, as a lot of people will no doubt notice from the less-than-elegant styling to the tic_priority cell, is that I don't have a clean way to conditionally style the cells directly. What this code does is to open a div within the cell (in place of the query results, actually) and use a class to set the background color, and another one to set the div height manually, since if I don't only the div text ends up with a recolored background. Of course this is a very crude hack, and I'm looking for a better solution. Rather than winding up with this code:
Code:
<td><div class="lopri logview">bassa</div></td>
I would like to have something like this instead:
Code:
<td class="lopri">bassa</td>
My problem is, however, that I don't have the foggiest on how to intercept the proper cell number, and style that one. I can find out if the tic_priority key exists already, but how would I go to assign it to a numeric value showing the code which cell to process? Code reusability is not a strict priority, I will be using this function for three views where I already know which values I should expect. Many thanks in advance for your advice, I hope I’ve provided adequate information on my problem.