doumeng3345 2016-12-20 18:56
浏览 68

变量不适用于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')

?>

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);
  • 写回答

2条回答 默认 最新

  • duannaozhao4626 2016-12-21 05:07
    关注

    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;
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?