如何在PHP中获取empty_value的值

时间:2017-06-30 13:45:29

标签: php sql

我有multiple choices的列表。当用户不选择任何选择时,应显示ALL文章的相关信息。 我在以下ALL中添加了默认值form

   <?php

    $user = $app['security']->getToken()->getUser();
            $default = 'ALL';
            $form = $app['form.factory']->createBuilder('form',['lru' => [$default]])->setMethod('GET')
 ->add('lru', 'choice', array(
                    'choices' => array(
                     $default => $default,

                    '\'ATSU\'' => 'ATSU', .... )

                    'required' => FALSE,
                    'empty_value' => 'ALL',
                    'empty_data' => NULL,
                    'multiple' => TRUE
                      ))

当我运行时显示错误,我应该修改我的请求,默认情况下empty_value ALL

我在课堂上宣布:

 private $_sqlSelect = 'Select * ';
  private $_sqlFrom = 'From `piecearticles`';
  private $_sqlWhere = 'Where ';

这跟随我的功能:

if ($this->_count == 0) {
        $this->_sqlWhere.="`piecearticles`.`ID_Article`=`article`.`ID_Article` AND `piecearticles`.`Designiation`=`article`.`Designiation` AND `article`.`ID_LRU`=`lru`.`ID_LRU`";

        $this->_count++;
    } else {
        $this->_sqlWhere.="AND `piecearticles`.`ID_Article`=`article`.`ID_Article` AND `piecearticles`.`Designiation`=`article`.`Designiation` AND `article`.`ID_LRU`=`lru`.`ID_LRU`";
    }

    if (!empty($this->_lru)) {
        if(is_array($this->_lru) or ($this->_lru instanceof Traversable)) {
            $this->_sqlWhere.=" AND `lru`.`LRU` IN (" . implode(",", $this->_lru) . ")"; //='" . $this->_lru . "'";
        } else {
            $this->_sqlWhere.=" AND `lru`.`LRU`='" . $this->_lru . "'";
        }
    }
    //if (strpos($this->_sqlFrom,'article') == false) {
    $this->_sqlFrom.=', `article` ';
    //}
    if (strpos($this->_sqlFrom, 'lru') == false) {
        $this->_sqlFrom.=',`lru`';
    }
    //echo count($this->_lru);
} 

我做了echo的{​​{1}},它返回querylru IN(全部) 如何更改我的请求以获取默认值“ALL”的值。 谢谢。

1 个答案:

答案 0 :(得分:1)

我将冒昧地提供一个部分示例,我希望在一个专栏中解决您的问题。我假设用户只检查'ALL'框,或选择其他几个框。

if (!empty($this->_lru)) {
        if((is_array($this->_lru) or ($this->_lru instanceof Traversable)) && ! in_array('ALL', $this->_lru)) {
            $this->_sqlWhere.=" AND `lru`.`LRU` IN (" . implode(",", $this->_lru) . ")"; //='" . $this->_lru . "'";
        } else if ($this->_lru != 'ALL' && ! in_array('ALL', $this->_lru)) {
            $this->_sqlWhere.=" AND `lru`.`LRU`='" . $this->_lru . "'";
        }
    }

此代码段应达到以下条件:如果根本没有选中任何框,请将其视为对所有内容的查询。如果选择“ALL”的任何框,则省略where子句。否则,如果_lru是一个数组,则将数组内插到where子句中。如果由于我不理解的原因,_lru不是数组,请在where子句中使用它的值。