Forums

Displaying fields from database in a module

firestarter 08 Nov, 2009
Hi, I'm trying to display some selected fields from the database in a module. To do this, I created a module and I added new fields to submitcontent form. when I retrieve the 'title' field of submitcontent form, everything is ok, but I was unable to get the other fields and my new added fields into the module. my code is below;

<?php
defined('_JEXEC') or die('Restricted access');
$number = $params->get('number', 2);
$db =& JFactory::getDBO();

$query="SELECT title FROM #__content";

$db->setQuery( $query, 0, $number );
$rows = $db->loadObjectList();

foreach($rows as $row){
    echo "$row->title"."<br />";
}
?>


what might be the problem? everything seems ok when I look into the database.
GreyHead 08 Nov, 2009
Hi firestarter,

From a quick look your code looks OK except that you have used $row & $rows. Joomla and other extensions sometimes use these as well and the results can get confused. Switch these for something 'unique' to your module.

The next debug step is to turn on Site Debug and see if any error is being returned from this MySQL query and/or copy the query string into PHPMyAdmin or EasySQL and see if you get the result you expect.

Bob
firestarter 08 Nov, 2009
Thanks Bob, I found the problem. in jos_content table, there were 9 entry before new submissions and new added fields shown empty indeed and return empty view without error. As you noticed in my code, I would like to display first 2 entry of the selected field but started with the 10th entry of the jos_content table. how can I modify the code to do that?
GreyHead 08 Nov, 2009
Hi firestarter,

Probably the best way is to add a WHERE clause to the query to define which entries you want to include.

Otherwise I think you can replace the '0' in your $db->setQuery() with '9' but this doesn't feel like a good solution long term.

Bob
firestarter 08 Nov, 2009
Hi again,

changing 0 in $db->setquery() with 9 did not work. I am a newbie in php and mysql so I have no idea about writing the appropriate WHERE sql to accomplish this. can you help me on that?
This topic is locked and no more replies can be posted.