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;
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
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);
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'm trying to build a simple select list in Joomla 2.5 with php and mysql, but I've got a problem: it doesen't load the options name... are there any error?
<?php
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('optValue');
$query->from('#__sobipro_field_option');
$query->where("fid='38'");
$db->setQuery((string)$query);
$results = $db->loadObjectList();
if ($results){
echo "<select>";
foreach($results as $result){
foreach($result as $value) {
$query->select('sValue');
$query->from('#__sobipro_language');
$query->where("fid='38' and language='it-IT' and sKey='".$value."'");
$db->setQuery((string)$query);
$name = $db->loadResult();
echo "<option value=\"$value\">".$name."</option>";
}
}
echo "</select>";
}
else {
echo 'Error';
}
?>
Whenever you're trying to debug Joomla! code turn on debug mode (Global Configuration->System->Debug Settings) and turn Error Reporting up to Development (Global Configuration->Server->Error Reporting), then you would have seen your problem straight away.
$query is a actually an object of type JDatabaseQuery and should be used as is in the $db->setQuery($query) call without casting to a (string).
When referencing a database, table or column name it's a good idea to use Joomla's mechanism for wrapping the name is the correct quotes e.g.
$query-select($db->quoteName('optValue'))
Amongst other things this will insure that SQL keywords aren't a problem.
Likewise for values, use the Joomla! provided $db->quote() method. e.g.
$query->where($db->quoteName('fid') . '=' . $db->quote('38'));
When building a WHERE query with multiple values ANDed together you can simply add each item as it's own ->where() clause.
So your where before I would tend to write as:
$query->where($db->quoteName('fid') . '=' . $db->quote('38'));
$query->where($db->quoteName('language') . '=' . $db->quote('it-IT'));
$query->where($db->quoteName('sKey') . '=' . $db->quote($value));
Finally, as you've said both of your $queries work by themselves, but when put together in your code as shown they don't.
The reason for this is that after you configure $query for your first statement (and run it), you continue to add to the second statement to the original $query object. So, that when you do the second, third etc setQuery($query) the SQL is getting longer and longer and unlikely to return any results.
As you don't test for success of each $db->loadResult() in the inner-most foreach loop you never see the error.
At the very least you need to add a new $db->getQuery(true) at the beginning of your inner loop, like this:
<?php
// Get default database object
$db = JFactory::getDBO();
// Get a new JDatabaseQuery object
$query = $db->getQuery(true);
// Build the query
$query->select($db->quoteName('optValue'));
$query->from($db->quoteName('#__sobipro_field_option'));
$query->where($db->quoteName('fid') .'=' . $db->quote('38'));
// Set the query for the DB oject to execute
$db->setQuery($query);
// Get the DB object to load the results as a list of objects
$results = $db->loadObjectList();
if ($results){
echo "<select>";
foreach($results as $result)
{
foreach($result as $value)
{
// Get a new query object
$query = $db->getQuery(true);
// Build the query
$query->select($db->quoteName('sValue'));
$query->from($db->quoteName('#__sobipro_language'));
$query->where($db->quoteName('fid') .'=' . $db->quote('38'));
$query->where($db->quoteName('language') .'=' . $db->quote('it-IT'));
$query->where($db->quoteName('sKey') .'=' . $db->quote($value));
$db->setQuery($query);
$name = $db->loadResult();
echo "<option value=\"$value\">$name</option>";
}
}
echo "</select>";
}
else
{
echo 'Error';
}
This is going to potentially create a lot of objects, so, you should consider if there's a way to restructure it all into one SQL query this will make it significantly faster and reduce the load on your server.
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;