变量在where子句php joomla中不起作用

问题描述:

我有功能,但我的功能很好,只有我不明白这一点:

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')

?>

这是完整的代码:

$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;

}

此功能运行良好,只需要获取类别数据即可. 这是joomla模块,该功能位于default.php文件的helper.php文件中.我从该功能中获取了一些数据.

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.

我找到了解决方法:

添加mod_suggested_deals.php

Add in mod_suggested_deals.php

$category = (int) $params->get('fieldvalue'); 

然后:

$results = modsuggested_dealsHelper::getSuggested($category);

您所做的错误是您在函数外部定义了变量值,但在函数内部调用了变量.如果您的类不是抽象类,则可以使用$this->category;如果您的类是抽象类,则可以在函数内部使用$ 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;

}