Mindaugėlis Pakasyk
Mindaugėlis Pakasyk

Reputation: 11

Joomla 3 JDatabase how to echo rows

I can't find valid statement for fetching rows and displaying items in JDatabase.

My code looks like this:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('item_id, item_name'));
$query->from('#__items');

How to print out these items in table?

Upvotes: 1

Views: 13151

Answers (3)

Bakual
Bakual

Reputation: 2731

The docs page is here: http://docs.joomla.org/Accessing_the_database_using_JDatabase/3.0

you need to add something like this:

$db->setQuery($query);
$results = $db->loadObjectList();

This will give you an array of objects where each object is a row.

This page: http://docs.joomla.org/Accessing_the_database_using_JDatabase/1.5 is for Joomla! 1.5, but still has (imho) the best list of the possible functions to get your data. Most are still valid I think.

To output the $results array you use something like this:

foreach ($results as $row) :
    echo $row->item_id;
    echo $row->item_name;
endforeach;

Upvotes: 4

Lodder
Lodder

Reputation: 19733

You don't add the array to your select query, you simply add the values, separated by a comma, like so:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('item_id, item_name')
$query->from('#__items');
$db->setQuery($query);
$results = $db->loadObjectList();

Upvotes: 0

Toretto
Toretto

Reputation: 4711

Try like this:

// Get a database object
$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('item_id, item_name');
$query->from('#__items');

// sets up a database query for later execution
$db->setQuery($query);

// fetch result as an object list
$result = $db->loadObjectList();

For more detail see the link Accessing the database using JDatabase/3.0 . For more methods of how to fetch result you can use loadResult(),loadRow(),loadAssoc(),loadObject(),loadResultArray(),loadRowList(),loadAssocList().

You can also refer this Accessing the database using JDatabase/1.5 .

You can also refer this link for how to

Developing a Model-View-Controller Component/3.0/Introduction

Developing a Model-View-Controller Component/2.5

Hope it help you.

Upvotes: 1

Related Questions