变量不适用于where子句php joomla

时间:2016-12-20 18:56:25

标签: php mysql variables joomla where

我有功能,我的功能很好但只是我不明白:

<?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文件中我从这个函数中得到一些数据。

我找到了解决方案:

添加mod_suggested_deals.php

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

然后:

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

2 个答案:

答案 0 :(得分:0)

您正在做的错误是您在函数外部定义变量值,但调用其中的变量。如果你的类不是抽象类,你可以使用$this->category,或者如果你的类是一个抽象类,那么在函数内使用$ category变量,如下所示

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;

}

答案 1 :(得分:0)

我找到了这个问题的解决方案:

在我的函数中我添加了param $ category

public static function getSuggested($category) {...}
mod_suggested_deals.php中的

我添加代码:

$category = $params->get('title');

然后是代码(它已经在这里)。

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