variable not work in where clause php joomla - php

I have function and my function works good only I do not understand this:
<?php
// $category output is 23 when I echo $category; and there is no records
->where('d.category_id = ' . (int) $category)
// also this method not work
->where('d.category_id = ' . $category)
// but this method works
->where('d.category_id = 23')
?>
this is full code:
$category = $params->get('title');
//echo $category;
public static function getSuggested($category) {
$db = JFactory::getDBO();
$query = $db->getQuery(true);
//$now = $jdate->toSql();
//$nullDate = $db->quote($db->getNullDate());
$query->select('d.*');
$query->from('#__deals_deals AS d');
$query->select('c.title AS category_title');
$query->join('LEFT', '#__deals_categories AS c ON c.id = d.category_id')
->where('(d.is_market = 0 AND d.state = 1)','AND')
->where('d.category_id = 23')
->order('id DESC');
//$query->where('(d.publish_up = ' . $nullDate . ' OR d.publish_up <= ' . $query->quote($now) . ')');
//$query->where('(d.publish_down = ' . $nullDate . ' OR d.publish_down >= ' . $query->quote($now) . ')');
$db->setQuery($query,0,10);
$results = $db->loadObjectList();
return $results;
}
this function works good only need to get category data.
this is joomla module, and this function is in helper.php file in default.php file I am get some data from this function.
I found solution:
Add in mod_suggested_deals.php
$category = (int) $params->get('fieldvalue');
then:
$results = modsuggested_dealsHelper::getSuggested($category);

I found soluton for this question:
in my function I Add param $category
public static function getSuggested($category) {...}
in mod_suggested_deals.php I add code:
$category = $params->get('title');
then is code (it already was here).
$results = modsuggested_dealsHelper::getSuggested($category);

The mistake you are doing is you are defining a variable value outside a function but calling the variable inside it. If your class is not abstract class you can use $this->category or if your class is an abstract class then use the $category variable inside of the function like this
public static function getSuggested() {
$module = JModuleHelper::getModule('mod_yourmodulename');
$catid = new JRegistry($module->params);
$category = (int) $catid['title'];
$db = JFactory::getDBO();
$query = $db->getQuery(true);
//$now = $jdate->toSql();
//$nullDate = $db->quote($db->getNullDate());
$query->select('d.*');
$query->from('#__deals_deals AS d');
$query->select('c.title AS category_title');
$query->join('LEFT', '#__deals_categories AS c ON c.id = d.category_id')
->where('(d.is_market = 0 AND d.state = 1)','AND')
->where('d.category_id = '.$category)
->order('id DESC');
//$query->where('(d.publish_up = ' . $nullDate . ' OR d.publish_up <= ' . $query->quote($now) . ')');
//$query->where('(d.publish_down = ' . $nullDate . ' OR d.publish_down >= ' . $query->quote($now) . ')');
$db->setQuery($query,0,10);
$results = $db->loadObjectList();
return $results;
}

Related

How can i return an array in php function

I need a return array in function. When i was use that returning this Array ( ):
function menuOlustur($ana_kategoriler){
global $db;
$alt_kategori_durum = '';
$alt_kategori;
foreach ($ana_kategoriler as $kategori) {
$alt_kategori = $db->rawQuery('select * from s_kategoriler where kategori = ' . $kategori['id'] . ' and durum=1 order by sira asc');
}
return $alt_kategori;
}
and i was use this returning one more index in array.
function menuOlustur($ana_kategoriler){
global $db;
$alt_kategori_durum = '';
$alt_kategori;
foreach ($ana_kategoriler as $kategori) {
$alt_kategori[] = $db->rawQuery('select * from s_kategoriler where kategori = ' . $kategori['id'] . ' and durum=1 order by sira asc');
}
return $alt_kategori;
}
My $ana_kategoriler is an array its a calling query like that
$ana_k_cek = $db->rawQuery('select id, kategori, adi_' . $dil . ' as adi,link_' . $dil . ' as link from s_kategoriler where m_id=5 and durum=1 order by sira asc ');
whats can i do ?
EDIT: fixed query, should not be kategori = 1,2,3,4, but kategory IN(1,2,3,4)
I suggest to change your code to:
function menuOlustur($ana_kategoriler){
global $db;
/* first - collect all ids */
$ids = array();
foreach ($ana_kategoriler as $kategori) {
$ids[] = $kategori['id'];
}
/* return empty array if there is no ids, or return result of single query matching all results with given ids */
return count($ids) > 0 ? $db->rawQuery('select * from s_kategoriler where kategori IN( ' . implode(",",$ids) . ') AND durum=1 order by sira asc') : array();
}

Get Attribute Value in Opencart

How do I get the value of a specific attribute? I use the code:
$attributes = $this->model_catalog_product->getProductAttributes($product['product_id']);
if (!empty($attributes)) {
foreach ($attributes as $attr) {
if($attr['attribute_id'] == 15) {
$attr_text = $attr['text'] ;
$attr = $this->dd->createElement('param');
$attr->setAttribute('code', 'color');
$attr->setAttribute('name', 'Color');
$attr->appendChild($this->dd->createTextNode($attr_text));
$e->appendChild($attr);
}
}
}
But it does not work. How to get the value of a variable in OpenCart 2.3
$attr['text']
First off all, new get grouped attributes from getProductAttributes(), you need to create new function in model for attribute information by ID.
public function getProductAttributeById($product_id, $attribute_id) {
$product_attribute_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_attribute WHERE product_id = '" . (int)$product_id . "' AND attribute_id = '" . (int)$attribute_id . "'");
return $query->row;
}
Code not tested!!!
And then you use it like this:
$attributes_by_id = $this->model_catalog_product->getProductAttributeById($product['product_id'], 15);
if (!empty($attributes_by_id )) {
$attr_text = $attributes_by_id ['text'] ;
$attr = $this->dd->createElement('param');
$attr->setAttribute('code', 'color');
$attr->setAttribute('name', 'Color');
$attr->appendChild($this->dd->createTextNode($attr_text));
$e->appendChild($attr);
} else {
// default code
}

SQL SUM() values in model Joomla Component

I have this function
public function getItem($idOn = false)
{
if($idOn) {
$id = $idOn;
} else {
$id = JRequest::getInt('id', $idOn);
}
if(!$id) return false;
$select = $this->_db->getQuery(true);
$select->select('a.*, b.title AS categoryTitle, c.email AS userEmail, SUM(s.view_item) AS hits');
$select->from('#__cddir_content AS a');
$select->join('INNER','#__cddir_categories AS b ON a.categories_id = b.id');
$select->join('LEFT','#__users AS c ON a.users_id = c.id');
$select->join('LEFT','#__cddir_statistic as s ON a.id=s.item_id');
$select->where('a.id = '.$this->_db->quote($id));
$select->where('a.published = 1');
$user=JFactory::getUser();
$aclgroups = implode(',', $user->getAuthorisedViewLevels());
$select->where('a.access IN (' . $aclgroups . ')')
->where('b.access IN (' . $aclgroups . ')');
$this->_db->setQuery($select);
$item = $this->_db->loadObject();
return $item;
}
I need to sum again the hits that I already added "SUM(s.view_item) AS hits", because some items have the same alias (a.alias) in #__cddir_content
So I need first sum all the s.view_items as hit that have the same id
Then I have to sum the saved hits that have the same alias.
I hope I made myself clear
Thank you for you help

calling a function inside a class from another class php

I am very new to oops in php. Can anyone tell me how can i use a function
public static function getCategories($id_lang = false, $active = true,$order = true, $sql_filter = '', $sql_sort = '', $sql_limit = '')
{
if (!Validate::isBool($active))
die(Tools::displayError());
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT *
FROM `' . _DB_PREFIX_ . 'category` c
' . Shop::addSqlAssociation('category', 'c') . '
LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON c.`id_category` = cl.`id_category`' . Shop::addSqlRestrictionOnLang('cl') . '
WHERE 1 ' . $sql_filter . ' ' . ($id_lang ? 'AND `id_lang` = ' . (int)$id_lang : '') . '
' . ($active ? 'AND `active` = 1' : '') . '
' . (!$id_lang ? 'GROUP BY c.id_category' : '') . '
' . ($sql_sort != '' ? $sql_sort : 'ORDER BY c.`level_depth` ASC, category_shop.`position` ASC') . '
' . ($sql_limit != '' ? $sql_limit : '')
);
if (!$order)
return $result;
$categories = array();
foreach ($result as $row)
$categories[$row['id_parent']][$row['id_category']]['infos'] = $row;
return $categories;
}
getCategories() is inside a class named class CategoryCore i want to use this getcategory into a new class totalDiscount in which a function called configure_products();
How can i use getcategory() inside the configure products?
include the class file on the page
You can create a object of the class inside another class
function configure_products(){
$categories = new CategoryCore();
$categories->getcategory();
// use $categories to do stuff
......
.....
}
OR
You can call it directly
function configure_products(){
$categories = CategoryCore::getCategories();
.....
....
}
Your function getCategories() is a static function.
So, it can be called without creating object on teh class CategoryCore.
You can use it as (using scope resolution operator):
$categories = CategoryCore::getCategories(YOUR_ARGUMENTS)
Reference

How to update table within joomla module or backend?

I working on some module and want to have ability to update table with image sizes - there already two columns in table (width, height). Its my first approach to joomla module develop. All code works except last foreach with update. Where I'm wrong?
And how to trigger this function in module or in backend module settings to button or link? Code in helper.php:
public static function updateSize( $params )
{
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
JHtml::_('behavior.framework', true);
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName(array('a.imgfilename', 'b.catpath')))
->from($db->quoteName('#__imagestable', 'a'))
->where(($db->quoteName('height').'=') && ($db->quoteName('width').'='))
->join('INNER', $db->quoteName('#__imagestable_cat', 'b') . ' ON (' . $db->quoteName('a.catid') . ' = ' . $db->quoteName('b.cid') . ')')
->order('RAND() LIMIT 5');
$db->setQuery($query);
$size = $db->loadObjectList();
return $size;
foreach($size as $imageSize){
$imgpath = $path.$imageSize->catpath.'/'.$imageSize->imgfilename;
$imgfilename = $imageSize->imgfilename;
list($width, $height) = getimagesize($imgpath);
$validImgs[] = $imageSize;
foreach ( $validImgs as $row ) {
$query = $db->getQuery(true)
->update($db->quoteName('#__imagestable'))
->where ($db->quoteName('imgfilename').'='.$imgfilename)
->set(array($db->quoteName('height') . '=' . $height, $db->quoteName('width') . '=' .$width));
$db->setQuery($query);
$imgSize = $db->execute();
}
}
}
Is it possible to attach this function to backend XML - maybe to button "Update image sizes"?
You are using return $size; before the foreach.

Categories