I'm trying to get the category details from the joomla database for RSEvents. Can anyone shed any light on why this isn't working:
function _getCategorySlug($value) {
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all articles for users who have a username which starts with 'a'.
// Order it by the created date.
// Note by putting 'a' as a second parameter will generate `#__content` AS `a`
$query
->select($db->quoteName(array('a.*', 'b.id', 'b.ide')))
->from($db->quoteName('#__categories', 'a'))
->join('INNER', $db->quoteName('#__rseventspro_taxonomy', 'b')
. ' ON (' . $db->quoteName('a.id') . ' = ' . $db->quoteName('b.id') . ')')
->where($db->quoteName('b.ide') . ' = '.$db->quote($value));
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
}
I think this may help you. Using the below function you can get the categories of a particular event.
public function getCategories($id) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->clear()
->select($db->qn('id'))
->from($db->qn('#__rseventspro_taxonomy'))
->where($db->qn('type').' = '.$db->q('category'))
->where($db->qn('ide').' = '.$id);
$db->setQuery($query);
return $db->loadColumn();
}
Thanks! That was a great help and got me to where I need (with a little tweaking):
function getCategories($id) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->clear()
->select($db->qn('id'))
->from($db->qn('#__rseventspro_taxonomy'))
->where($db->qn('type').' = '.$db->q('category'))
->where($db->qn('ide').' = '.$id);
$db->setQuery($query);
$categories = $db->loadColumn();
return implode("_", $categories);
}
Also, would be great to find out what was wrong with my original query to get category's alias from the #__categories table
Related
I've created a form using the component SEBLOD for joomla, it's all right, I can properly view the data entered, but i would like to manipulate the data, however I am not able to connect to the database, so I tried this:
<?php
defined( '_JEXEC' ) or die;
$catId = 11;
$catId2 = 12;
$sql = "SELECT * FROM #__cck_store_form_add_compras";
$con = &JFactory::getDBO();
$con->setQuery($sql);
$resultado = $con->loadObjectList();
$query = "SELECT * FROM #__content WHERE catid ='" . $catId . "' OR catid ='" . $catId2 . "'";
$db = JFactory::getDBO();
$db->setQuery($query);
$articles = $db->loadObjectList();
foreach($articles as $article){
echo '<br/><br/>ID:'. $article->id
.'<br/>Titulo: '. $article->title
.'<br/>Quantidade Comprada: '.$resultado[0]->add_compras_quantidade_comprada
.'<br/>Unidade de Medida: '.$resultado[0]->add_compras_unidade_de_medida
.'<br/>Preço Por Kilo/Unidade: '.$resultado[0]->add_compras_preco_por_unidade_kilo
.'<br/>Data da Compra: '.$resultado[0]->add_compras_data_da_compra
.'<br/>Total da Compra: '.$resultado[0]->add_compras_total_da_compra
.'<br/>Local da Compra: '.$resultado[0]->add_compras_local_da_compra
;
}
?>
What I want is to get the id of the article, article name, article category that are saved in the table #__content, and the other fields that are saved in the table #__cck_store_form_add_compras, but only articles with category id = 11 or category id = 12
When i use this code (above) I see the results however some of them appears repeated.
.
When i use this below, i see only the id of article and article name, but i cant see the other fields:
$catId = 11;
$catId2 = 12;
$query = "SELECT * FROM #__content WHERE catid ='" . $catId . "' OR catid ='" . $catId2 . "'";
$db = JFactory::getDBO();
$db->setQuery($query);
$articles = $db->loadObjectList();
.
if i use the code below, i can see only other fields, but i can't see the id of article and name of article:
$sql = "SELECT * FROM #__cck_store_form_add_compras";
$con = &JFactory::getDBO();
$con->setQuery($sql);
$resultado = $con->loadObjectList();
.
looking at the documentation I found this piece of code:
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all articles for users who have a username which starts with 'a'.
// Order it by the created date.
// Note by putting 'a' as a second parameter will generate `#__content` AS `a`
$query
->select($db->quoteName(array('a.*', 'b.username', 'b.name')))
->from($db->quoteName('#__content', 'a'))
->join('INNER', $db->quoteName('#__users', 'b') . ' ON (' . $db->quoteName('a.created_by') . ' = ' . $db->quoteName('b.id') . ')')
->where($db->quoteName('b.username') . ' LIKE \'a%\'')
->order($db->quoteName('a.created') . ' DESC');
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
I think it's exactly what I need, but i don't know how can I change to that fits my needs, someone could help me?
Sorry for my bad english, I'm using google translator to help me.
The best way to display a filtered list of SEBLOD contents is to use a SEBLOD search and display it through a menu item, a module or inside a SEBLOD content (use a CCK module or CCK list field).
In your SEBLOD search, you can set the 'article category id' as filter with "11,12" as default value so only articles in these 2 categories will be retrieved.
Please find the full answer to your question on SEBLOD forums http://www.seblod.com/community/forums/general-discussions/connecting-to-database#post22181
Thanks.
Sebastien.
With JTable::getInstance("content"); you may get data from Joomla content table and leave SEBLOD content for the query.
Example:
$query = "SELECT * FROM #__cck_store_form_produto";
$db = &JFactory::getDBO();
$db->setQuery($query);
$values = $db->loadObjectList();
foreach($values as $val){
echo $val->id; // The article ID is inside SEBLOD table too.
echo $val->your_field; // will write the value on a SEBLOD field called "your_field"
// now to get joomla values
$maindata =& JTable::getInstance("content");
$maindata->load($val->id);
echo $maindata->title; //will write article title
echo $maindata->alias; //will write article alias
echo $maindata->catid; //will write article Category ID
}
Or you can also use "INNER JOIN" between two tables just use the ID to link the values:
https://www.w3schools.com/sql/sql_join_inner.asp
I'm trying to query from a DB to get joomla articles from a certain category (52), and that are published.
I want to return these as the newest created, but I don't know where to add in the ORDER BY for the query.
I did try to add ->orderby('created'); just above $db->setQuery($query); but that didn't seem to work, it still only seems to be displaying by ascending order of ID.
Can anyone help?
<?php
$i = 0;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from('#__content')
->where('catid = 52 AND state = 1');
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ( $rows as $row ) {
if(++$i > 1) break;
echo "<li><a href='/why-us/news/".$row->id."-".$row->alias."'>".$row->title."</a></li>";
}
?>
You can simply use ->order('created DESC') to achieve your desired results.
Check the documentation page, here
Can you explain me why my code isnt working? Ive been thinking about it for a while and I cant find it. obviously I want to print some columns from rows where column F1 is equal to user's username.
$db = JFactory::getDBO();
$user = JFactory::getUser();
$query = "SELECT * FROM qwozh_visforms_1 WHERE F1 = ".$user->username;
$db->setQuery($query);
$result = $db->query();
while($row = mysqli_fetch_object($result))
{
print $row->F1;
}
It works when I remove condition from select command and I cant figure out how to make it work with it
$query = "SELECT * FROM qwozh_visforms_1";
Now Im getting this error:
UNKNOWN COLUMN 'ADMIN' IN 'WHERE CLAUSE' SQL=SELECT * FROM
QWOZH_VISFORMS_1 WHERE F1 = ADMIN RETURN TO PREVIOUS PAGE
Thanks
All it takes if a quick read of the Joomla documentation. The following is the same as your query but making full use of Joomla's up to date database class:
$db = JFactory::getDbo();
$user = JFactory::getUser();
$query = $db->getQuery(true);
$query->select(array('*'))
->from($db->quoteName('#__visforms_1'))
->where($db->quoteName('F1') . ' = '. $db->quote($user->username));
$db->setQuery($query);
$results = $db->loadObjectList();
// Display the results
foreach($results as $result){
// echo what you want here
}
Note, I've used the prefix #__ rather than manually defining qwozh, assuming your table belong to a Joomla extension.
I know PHP and MySQL, but not Joomla. But the problem is that your username needs to be quoted because it is probably a string.
Try this:
$query = "SELECT * FROM qwozh_visforms_1 WHERE F1 = '{$user->username}'";
or
$query = "SELECT * FROM qwozh_visforms_1 WHERE F1 = ".$db->quote($user->username);
You need to wrap the name in quotes:
$query = "SELECT * FROM qwozh_visforms_1 WHERE F1 = '".$user->username . "'";
As pointed out in the comments my answer has a pretty bad quality, you may want to look at prepared statements, expecially using bindParam, which takes care of quotes for you and protects you agains SQL injection attacks.
Unfortunately I cannot suggest you Joomla based approach since I never used it, somebody else can suggest you a more appropriate solution.
Hey guys I need your help I have this code in a localhost file :
$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');
$data = array();
$results = $db->query("SELECT * from YOUR_TABLE_NAME");
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$data[$row['prov_name']][] = $row;
}
But when I try to integrate this file in Joomla it doesn't work! Do you know how can I "transform" this into Joomla connection?
I always use this one for some select
$db =& JFactory::getDBO();
$query = 'SELECT CA_id FROM compras_activos where STAT_name = "Solicitado"';
$db->setQuery($query);
$result = $db->loadObjectList();
$CA_id = $result[0];
But that is just for one specific value but now I need all(*) the table.
Thanks
You should really read the documentation that I provided. If you are able to write a query as shown in your question, then this should not be too taxing. You can use the following:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('*'))
->from($db->quoteName('#__compras_activos'))
->where($db->quoteName('STAT_name') . ' = '. $db->quote('Solicitado'));
$db->setQuery($query);
$result = $db->loadObjectList();
If your database table do not belong to an extension associated with Joomla, then remove the #__ prefix in the above code.
Hope this helps
I want to fire one complex query in my joomla site. I have written below code for it.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('`chr`.`characteristic_id`,`chr`.`characteristic_value`,`prc`.`price_value`');
$query->from('`#___hikashop_product` AS `pdt`');
$query->join('inner', '`#__hikashop_variant` AS `vari` ON `pdt`.`product_id` = `vari`.`variant_characteristic_id`');
$query->join('inner', '`#__hikashop_characteristic` AS `chr` ON `vari`.`variant_characteristic_id` = `chr`.`characteristic_id`');
$query->join('inner', '`#__hikashop_price` AS `prc` ON `pdt`.`product_id` = `prc`.`price_product_id`');
$query->where('`pdt`.`product_id` = 68');
$db->setQuery($query);
Query is executing in my local mysql.
any help will be appreciated
You can try this
$db->setQuery("Your query");
$result = $db->query();
//if you need the count
$rowcount = $db->getNumRows();
//if the result is multiple rows
$result_array = $db->loadAssocList() or $db->loadObjectList();
//if the result is single row you can use
$result = $db->loadAssoc() or $db->loadObject();
To fire the query, all you need to do is:
$rows = $db->loadAssocList(); // or loadObjectList()
The above will put all of the rows into $rows
You can also fire the query without grabbing the rows with:
$db->query();