4

I come from a .NET background where you can create grids with headers and data in columns/rows to easily display your data. Is there a Joomla Extension that can do the same? I want to query MySQL and have the results from the query returned to the page in a format similar to the below (varying columns tho, not just 3 could be more or less)

Name --- ID --- Age
A --- 1 ---- 12
B --- 2 ---- 13
C --- 3 ---- 22

3

To query the database, you can use the following:

$db = JFactory::getDbo();

$query = $db->getQuery(true);

$query->select($db->quoteName(array('id', 'name', 'age')))
      ->from($db->quoteName('#__table')); 

$db->setQuery($query);

$results = $db->loadObjectList();

$results is now your variable that contains an object of results.

Note: If you want to get all data from all columns, then replace:

$query->select($db->quoteName(array('id', 'name', 'age')))

with:

$query->select(array('*'))

You can then add a foreach loop and construct your table:

<table>
    <tr>
        <th>Name</th>
        <th>Id</th>
        <th>Age</th>
    </tr>
    <?php 
        foreach ($results as $row)
        {
            echo '<tr>';
            echo '<td>' . $row->name . '</td>';
            echo '<td>' . $row->id . '</td>';
            echo '<td>' . $row->age . '</td>';
            echo '</tr>';
        }
    ?>  
</table>
2

As an alternative to Lodder's answer, another option is to use a third party extension such as ChronoConnectivity (free) or Alter Reports (paid) or similar which help you formulate your query and display the results in the front-end.

  • Have you used ChronoConnectivity before? I see how to create the connection and build out the query, but how do I actually add the returned results to my article? – NadineSmithJonesPicard Apr 18 '16 at 1:28
  • I tend to use the ChronoConnectivity plugin to insert the results into my articles. – Neil Robertson Apr 18 '16 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.