In Joomla how do I iterate over a database object - php

Sorry. I am very new to Joomla.
After I load data from the database like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('user_id')));
$query->from($db->quoteName('#__user_profiles'));
$db->setQuery($query);
$results = $db->loadObjectList();
How to I show the results?

Try
foreach ($results as $result) {
echo $result->user_id.'<br />';
}

Related

Output multiple rows in json in Zend Framework 1

I've searched and can't find an answer to my question.
I have the following code which is to loop through an array and then fetch back results for the different $id's.
The output when using echo json_encode($row); returns all results but the zend layout displays.
However when using $this->_helper->json($row,true); the layout doesn't display but only one result returns.
How can I return more than one result?
Any help would be much appreciated.
public function testAction()
{
//Get latest revision from database and loop through $id's
$id = array('308', '307', '306');
//Connect to database
foreach($id as $lId) {
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select('')
->from('LinktagRevisions')
->where('linktagId = ?', $lId)
->order('updated DESC')
->limit(1);
$stmt = $select->query();
while ($row = $stmt->fetch()) {
$this->_helper->json($row,true);
//Encode as json and echo result
// echo json_encode($row);
}
}
}
I think you can try this:
$result = array();
foreach($id as $lId) {
....
$stmt = $select->query();
$result[$lId] = $stmt->fetchAll();
}
$this->_helper->json($result,true);

Defining and calling custom function

I have a block of code that works perfect:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('date')));
$query->from($db->quoteName('#__webfoot_minutes_and_agendas'));
$query->order('date DESC');
$db->setQuery($query);
$results = $db->loadObjectList();
Until I try to assign it to a function:
function call_db() {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('date')));
$query->from($db->quoteName('#__webfoot_minutes_and_agendas'));
$query->order('date DESC');
$db->setQuery($query);
$results = $db->loadObjectList();
return $results;
}
It breaks when I try to call the function, I get an error for $results I get, "Invalid argument supplied for foreach()...:
call_db();
foreach ($results as $result) {
$dateArr = explode('-', $result->date);
if (!in_array($dateArr[0], $already_echoed)) {
echo '<li>' . $dateArr[0] . '</li>';
$count++;
}
$already_echoed[] = $dateArr[0];
}
Anyone have any suggestions on how I can straighten out this syntax?
You have to assign result of your function to variable.
$results = call_db();
Though it is awful design:
foreach (call_db() as $result) { ... }
You haven't assigned return value of call_db() to anything. You have to assign it first to $results
$results = call_db();
And, after that it is better if you check that the $results is an array. See this.

joomla 2.5 module: how to foreach loop put in tmpl/default.php

in joomla module to get data from database we use the code
public static function getdb($params)
{
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__categories');
// sets up a database query for later execution
$db->setQuery($query);
// fetch result as an object list
$result = $db->loadObjectList();
foreach ( $result as $row ) {
echo "$row->extension .<br>";
}
}
my question is how to use this foreach loop in tmpl/default.php ? and then wat will be my helper.php code?
foreach ( $result as $row ) {
echo "$row->extension .<br>";
}
if i use this foreach loop into default.php then it will better for me. pls someone help
helper.php
public static function getdb($params) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__categories');
$db->setQuery($query);
$result = $db->loadObjectList();
return $result;
}
default.php:
//call the function from the helper.php
$result = modHelloWorldHelper::getdb($params);
//display the results
foreach ( $result as $row ) {
echo $row->extension . "<br>";
}

how to show all data from joomla table?

recently i learned how to get data from database by this method
public static function getdb($params)
{
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__categories');
// sets up a database query for later execution
$db->setQuery($query);
// fetch result as an object list
$result = $db->loadObjectList();
foreach ( $result as $row ) {
echo "$row->extension .<br>";
}
}
by this line
echo "$row->extension .<br>";
we get single row value. how to we get all row value of #__categories? by any short code.
The foreach loop should give you all the rows for your query. Try making a few changes like so:
helper.php
public static function getdb($params) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__categories');
$db->setQuery($query);
$result = $db->loadObjectList();
return $result;
}
default.php
$result = modHelloWorldHelper::getdb($params);
foreach ( $result as $row ) {
echo $row->extension . "<br>";
}
You can use another foreach inside
foreach ( $result as $row ) { // it is like foreach($objects as $object)
foreach ($row as $r) { // it is like foreach($object as $values)
echo "$r .<br>";
}
}
But you should print the code in the view not in the model, controller, etc.

Query database in Joomla 2.5

I'm using Joomla 2.5 and I need to retrieve the field 'avatar' for the current user.
This is the code I'm using:
$user = JFactory::getUser();
$id = $user->get('id');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('avatar')
->from('#__discuss_users')
->where('id = $id');
$db->setQuery($query);
$results = $db->loadObjectList();
But it isn't displaying any data. I tried changing the last line to:
$results = $db->loadResult();
But it doesn't work either.
If you are using it as you posted, you won't include the actual value of $id in your query. You need to append it to the string like this:
$query->select('avatar')
->from('#__discuss_users')
->where('id = '.$id);
I am usually not using the $query->select stuff, but a plain old query. Thus, you might try to do it like this (this might not be best practice though):
$user = JFactory::getUser();
$id = $user->get('id');
$db = JFactory::getDBO();
// $query = $db->getQuery(true);
$query = "SELECT ".$db->quoteName('avatar')
." FROM ".$db->quoteName('#__discuss_users')
." WHERE ".$db->quoteName('id')
." = ".$db->quote($id).";";
// $query->select('avatar')
// ->from('#__discuss_users')
// ->where('id = $id');
$db->setQuery($query);
$results = $db->loadObjectList();
Thanks for your help. The following code works:
$user = JFactory::getUser();
$id = $user->get('id');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select($db->quoteName('avatar'))
->from($db->quoteName('#__discuss_users'))
->where('id = '.$id);
$db->setQuery($query);
$results = $db->loadResult();
echo $results;

Categories