Saturday, 03 August 2013 22:47

how to get data from database and display in joomla

Written by 
Rate this item
(2 votes)

It's quite common to get data from joomla database table.
By the following way i show how to get data as a objects
list and loop through the list to display data in web page -

First i get data from table using joomla class methods
as follows -

$database = JFactory::getDBO();
$database->setQuery("SELECT record,name FROM `#__tablename` ORDER BY id ASC");
$orderitems = $database->loadObjectList();

By the code it's easy to guess how to load data from table as
objects list in $orderitems.Then by the following way i loop
through the list and display each record(table row) data

<? if (count($orderitems)>0) { ?>
<div style="overflow:auto;border:1px solid lightgray;">
<table border="0" cellspacing="0" style="width:100%;">
<tbody class="bodycontent">
<?php
$cnt = 1;
foreach ($orderitems as $item) { ?>
<tr class="ms_row<?php echo ($cnt%2); ?>">
<td> <?php echo $item->record; ?></td> <td> <?php echo $item->name; ?></td>

</tr>
<?php $cnt++; } ?>
</tbody>
</table>
</div>
<? } else { ?> <div style="border:none;">No Items Found</div> <? } ?>

Hope it helps very much.

Read 14460 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.