[jcms] Displaying data from database to the site

941 views
Skip to first unread message

Jonathan Seni

unread,
Apr 22, 2012, 10:32:01 AM4/22/12
to joomla-...@googlegroups.com
Greetings,

Am kinda new to Joomla development, 

I followed the tutorial from official website on how to create the MVC based component, but according to my requirements, I failed n displaying the contents to the front site.

in lesson 6 (Using the Database) it shows how to display only one message passed from the database. What I want to do is to take all data (ie select * from .....)  then to display them on the front end with something like $this->item->title, $this->item->fullbody; etc.

Also I have to mention, while digging, I found some classes like JModel, JModelItem, etc, what are the difference and which class should I extend here, in model.

Thanks in advance.

--
Jonathan Seni.

There are 10 kinds of people in this world,
Those who knows numbers, and
Those who wait for the other 8 people to be listed
----------------------------------------------------

SimplyMepis, SuSE 9.2, openSuSE 10.2 /10.3/11.0/11.1/11.2 /11.3/11.4/12.1(x86-64).

Chad Windnagle

unread,
Apr 22, 2012, 3:27:48 PM4/22/12
to joomla-...@googlegroups.com
Not sure about your question on extending those classes, but setting up a query isn't too difficult:


// Get a database object
$db =& JFactory::getDBO();
 
$query = "SELECT * FROM #__example_table WHERE id = 999999;";
$db->setQuery($query);

$result = $db->loadObject();

At that point the resultes of the query are loaded in the $results as an object. Then you can do something like:

$result->item->id

You can use a few different JDatabase methods to load the query results in different ways. Like loadAssoc, loadResultArray etc.. 

Read more here:


Regards,
Chad Windnagle
Fight SOPA


--
You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group.
To post to this group, send an email to joomla-...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-cm...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-cms?hl=en-GB.

Jonathan Seni

unread,
Apr 22, 2012, 7:20:07 PM4/22/12
to joomla-...@googlegroups.com
Thanks,

I think I managed to get it working, and I would like to share so that others can also gain from this.

First, in the model I extended JModel and then create the following function:

public function getItem()
{
$id = JRequest::getInt('id');

$row = JTable::getInstance('jsstp','JssTpTable');
$row->load($id);

return $row;
}

Second under admin/tables/ I created another file, extending JTable and redeclaring the constructor with tablename, primary key and database reference as follow:

function __construct(&$db) 
{
parent::__construct('#__jsstp', 'id', $db);
}

Third, Under view.html.php I extended JView and also extend the function display which pass values of the entire entity to $this->item as follow:

$this->item = $this->get('Item');

then call the parent display: parent::display($tpl);

Then lastly, I called any field I want in tmpl/display.php as follow:

$this->item->firstname; etc.
Reply all
Reply to author
Forward
0 new messages