Calc Element - HTML Table

nbradshaw

Active Member
I created a Calc Element to echo out into an HTML Table - however, the results appear on TOP of the Form and List. Is there a workaround to this?

PHP:
$ticket_nbr = '{uniti_simplex_alert___ticket_nbr}';
$id = '{rowid}';
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select(array('company', 'Count_Outage_Sites','Count_Simplex_Sites'))
    ->from('remedy_xnetworks_count_sites')
    ->where('IncidentVisibleID_Trim = ' . $db->quote($ticket_nbr));
$db->setQuery($query);
$rows = $db->loadObjectList();
$list = array();
echo '<table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2">';
echo '<tbody>';
echo '<tr>';
echo '<td style="vertical-align: top;">Customer<br>';
echo '</td>';
echo '<td style="vertical-align: top;"># Sites Outage<br>';
echo '</td>';
echo '<td style="vertical-align: top;"># Sites Simplex<br>';
echo '</td>';
echo '</tr>';
    foreach ($rows as $row){
        echo'<tr>';
        echo'<td>'. $row->company ."</td>";
        echo'<td>'. $row->Count_Outage_Sites .'</td>';
        echo'<td>'. $row->Count_Simplex_Sites .'</td>';
        echo'<tr>';
    }
    echo '</tbody>';
    echo '</table>';
 

Attachments

  • html_table_simplex.JPG
    html_table_simplex.JPG
    52.7 KB · Views: 141
That's because you are echo'ing the results, not returning them.

So instead of echo, do ...

Code:
$myStr = '';

... at the top, then anywhere you are currently echoing it ...

Code:
$myStr .= 'whatever blah blah';

.. and as the last line ...

Code:
return $myStr;

You can call $myStr anything you want, just try and make it something which is likely to be unique, and not used in our code. Prefixing it with 'my' is a good convention, as we guarrantee never to use variables with that prefix in our code.

I also STRONGLY suggest changing $id to $myId, $db to $myDb, etc. as those are very common variable names which you could wind up stomping on in the code that evals yours.

-- hugh
 
Works great - thanks Hugh!

Here it is for anyone else needing this:

PHP:
$ticket_nbr = '{uniti_simplex_alert___ticket_nbr}';
$myid = '{rowid}';
$mydb = JFactory::getDbo();
$query = $mydb->getQuery(true);
$query
    ->select(array('company1', 'sum_Count_Outage_Sites','sum_Count_Simplex_Sites'))
    ->from('simplex_alert_system_site_counts')
    ->where('IncidentVisibleID_Trim = ' . $mydb->quote($ticket_nbr));
$mydb->setQuery($query);
$rows = $mydb->loadObjectList();
$list = array();
$mytable = '<table style="text-align: center; width: 100%;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="vertical-align: top;"><b>Customer</b></td>
<td style="vertical-align: top;"><b>Sites Outage</b></td>
<td style="vertical-align: top;"><b>Sites Simplex</b></td>
</tr>';


  foreach ($rows as $row){
        $mytable .='<tr>';
        $mytable .='<td>' . $row->company1 . '</td>';
        $mytable .='<td>' . $row->sum_Count_Outage_Sites . '</td>';
        $mytable .='<td>' . $row->sum_Count_Simplex_Sites . '</td>';
        $mytable .='<tr>';
    }
    $mytable .= '</tbody>';
    $mytable .= '</table>';
  
    return $mytable;
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top