In my example I will show in other module the prices of each article.
I use this code (hv try several codes) but it only shows one price on all listings. I think it's first in sql ..
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select ($db->quoteName('jr_price'));
$query->from($db->quoteName('#__jreviews_content'));
$db->setQuery($query);
$result = $db->loadResult();
print_r($result);
?>
You should do the following:
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select ($db->quoteName('jr_price'));
$query->from($db->quoteName('#__jreviews_content'));
$db->setQuery($query);
$prices = $db->loadColumn();
foreach ($prices as $price) {
echo $price.'<br />;
}
?>
loadResult() only loads a single result. I would suggest you use loadObjectList or loadColumn and then use a foreach loop to display your results.
The following documentation page will ve very helpful for you:
https://docs.joomla.org/Selecting_data_using_JDatabase
i found solution to add to code above
$result = $db->loadResult();
print_r($result);
Related
If I were to echo the result of this query:
//Find membertype from community builder
$db->setQuery('Select cb_membertype From #__comprofiler Where id='.$user->id);
$membertype = $db->loadResult(); //load the result from the query
I would use:
echo "Your member type is: $membertype";
I'd rather not use a new query for every variable thought since they are all in the same table.
I'd rather run a single query like this:
//Find all 3 from one query
$db->setQuery('Select cb_membertype, cb_2015vopac, cb_2015llf
From #__comprofiler Where id='.$user->id);
$result = $db->loadResult(); //load the result from the query
How do I echo the specific fields from that query when using the single $result variable?
First, let me just say that should should quote your values/columns/table names.
Secondly, if you want to get multiple values, you need to use loadObjectList instead of loadResult.
The following will do it for you:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('cb_membertype', 'cb_2015vopac', 'cb_2015llf')))
->from($db->quoteName('#__comprofiler'))
->where($db->quoteName('id') . ' = '. $db->quote($user->id));
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result)
{
echo $result->cb_membertype;
echo $result->cb_2015vopac;
echo $result->cb_2015llf;
}
So loadObjectList give you an object, then you can loop though it using foreach loop.
Hope this helps
I am trying to get all the values (numeric) of the column Bruel_ID from database mpctz_rsform_bruels to display them in a RSForm dropdown box. I'm using this code:
$db = JFactory::getDbo();
$db->setQuery("SELECT Bruel_ID FROM mpctz_rsform_bruels");
return $db->loadResult();
Howeve, I am only pulling the lowest value of the column. How can I get all the values?
Thanks,
Dani
Edit: I have been searching and I've found this code which was supposed to work:
//<code>
// Prepare the empty array
$items = array();
// Prepare the database connection
$db = JFactory::getDbo();
// Keep this if you'd like a "Please select" option, otherwise comment or remove it
$items[] = "|Selecciona un número[c]";
// Run the SQL query and store it in $results
$db->setQuery("SELECT Bruel_ID, Bruel_ID FROM #__rsform_bruels");
$results = $db->loadObjectList();
// Now, we need to convert the results into a readable RSForm! Pro format.
// The Items field will accept values in this format:
// value-to-be-stored|value-to-be-shown
// Eg. m|M-sized T-shirt
foreach ($results as $result) {
$value = $result->your_value;
$label = $result->your_label;
$items[] = $value.'|'.$label;
}
// Multiple values are separated by new lines, so we need to do this now
$items = implode("\n", $items);
// Now we need to return the value to the field
return $items;
//</code>
However, it displays nothing in the dropdown box, just the default value. Any help?
You can use loadColumn() to get an array of all the result in a single column. The below shows just that and a foreach loop so that each result displays on a new line:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('Bruel_ID'))
->from($db->quoteName('#__rsform_bruels'))
->order($db->quoteName('Bruel_ID') . 'DESC');
$db->setQuery($query);
$results = $db->loadColumn();
$items[] = "|Selecciona un número[c]";
foreach ($results as $id) {
$items[] = $id.'|'.$id;
}
$items = implode("\n", $items);
return $items;
This uses the most up to date Joomla coding standards for a database query.
Also notice I have replaced mpctz_ with #__ which is a built in Joomla feature that will automatically get the table prefix. It saves you having to manually define it which is bad as in the future, you may decide to change the prefixx.
Hope this helps
hi i need to make a drop down list field in a form (RSForm joomla 2.5), that will drew its values from virtuemart categories names.
i have this block of code that i need to customize to my needs but since i dont know php all my improvisations ended up in fatal error and need to reinstall the form again:(
the name of my table in mysql is xxx_virtuemart_categories_he_il
the names of the categories are listed here category_names
their id's are here virtuemart_category_id
this is the block of code how do i change it?
//<code>
// Prepare the empty array
$items = array();
// Prepare the database connection
$db = JFactory::getDbo();
// Run the SQL query and store it in $results
$db->setQuery("SELECT your_value, your_label FROM #__your_table");
$results = $db->loadObjectList();
// Now, we need to convert the results into a readable RSForm! Pro format.
// The Items field will accept values in this format:
// value-to-be-stored|value-to-be-shown
foreach ($results as $result) {
$value = $result->your_value;
$label = $result->your_label;
$items[] = $value.'|'.$label;
}
// Multiple values are separated by new lines, so we need to do this now
$items = implode("\n", $items);
// Now we need to return the value to the field
return $items;
//</code>
Add:
$db->query();
before:
$results = $db->loadObjectList();
The fast and simple solution to connect to a bd in virtuemart is:
$db = JFactory::getDbo();
$db->setQuery("SELECT * FROM ... WHERE ...");
$db->query();
$results = $db->loadObjectList();
echo var_dump($results);
I have in using joomla, have a input field and must add on result page of com_search a additional query, if i run this query in a sql editor i get 2 results, but in joomla is my foreach every time skipping.
If i do $db->getPrefix(); i see table prefix, so i thing to $db and $query are true.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__k2_categories');
$query->where("`name` LIKE '%MY_SEARCH_WORD%'");
$db->setQuery($query);
$results = $db->loadObjectList();
foreach($results as $entry) :
echo $results->name . "<br />";
endforeach;
I use this code to read from data base:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('category_name', 'category_short_description'));
$query->from('#__redshop_category');
$query->where('category_id = 1');
$db->setQuery($query);
$results = $db->loadObjectList();
How can i read once element, for example extra *category_name* and extra *category_short_description*
Here is also offical joomla wiki, but without single output: http://docs.joomla.org/J2.5:Accessing_the_database_using_JDatabase
Here what i have doing, but that's not work:
echo $results; ->output:ARRAY
echo $results['category_name']; -> without output, empty (In database rows are not empty)
How can i do this?
You can use loadObject() instead of loadObjectList() to retrieve just the first row:
$results = $db->loadObject();
echo $results->category_name;
If you want an associative array instead of an object, there is loadAssoc():
$results = $db->loadAssoc();
echo $results['category_name'];
To fetch single row you can use
$result = $db->loadObject()
instead of
$results = $db->loadObjectList()
and then you can access data by
$result->category_name;
$result->category_short_description;