代码之家  ›  专栏  ›  技术社区  ›  Radu

对drupal视图使用定制的动态查询

  •  1
  • Radu  · 技术社区  · 16 年前

    动态的意思是我希望能够根据用户输入更改sql查询。

    假设这是我的自定义查询,我该如何更改它以便在ORDER BY..之间切换。。用户单击列时降序和升序?当您重写视图生成的查询时,这是否可能?

    <?php
    function views_views_pre_execute(&$view) {
       if($view->name=="hud_downloads") {
         $view->build_info['query']="SELECT node.nid AS nid, 
             node.title AS node_title, 
             SUM(pubdlcnt.count) AS pubdlcnt_count 
             FROM node node 
             LEFT JOIN pubdlcnt pubdlcnt ON node.nid = pubdlcnt.nid  
             WHERE (node.type in ('huds')) AND (node.status <> 0) 
             GROUP BY node.nid ORDER BY pubdlcnt_count DESC";
         }
    }
    ?>
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   Jason Smith    16 年前

    小心,这里躺着龙。 我就是这样做的。首先,创建显示感兴趣内容的视图。对所有感兴趣的字段(计数、价格、标题等)添加排序不要担心它们不能很好地协同工作,您将在代码中动态地删除/更改它们。

    return _mymodule_handle_sortables($view);
    

    这将允许您快速/轻松地编辑函数的内容,而无需在每次迭代时编辑视图/保存视图/预览视图。请注意,我传递$\u GET变量。这不是绝对必要的,因为它应该在函数中可用。我这样做只是为了更容易阅读。

    第一步,使用devel模块获取可排序字段的名称

    function _mymodule_handle_sortables(&$view) {
        dpm($view->sort);
    }
    

    function _mymodule_handle_sortables(&$view) {
        switch ($_GET['sort']) {
            case 'sell_price_asc':
                unset($view->sort['title']);
                $view->sort['sell_price']->options['order'] = 'ASC';
                break;
            case 'sell_price_desc':
                unset($view->sort['title']);
                $view->sort['sell_price']->options['order'] = 'DESC';
                break;
            case 'alpha_asc':
                unset($view->sort['sell_price']);
                $view->sort['title']->options['order'] = 'ASC';
                break;
            case 'alpha_desc':
                unset($view->sort['sell_price']);
                $view->sort['title']->options['order'] = 'DESC';
                break;
        }
        return true;
    }
    

    在视图中添加一个PHP头并添加以下内容

    <?php echo _mymodule_sortables($_GET); ?>
    

    function _emunications_sortables($g) {
        // Collect all the relevant GET parameters
        $gopts = array();
        foreach ($g as $k=>$v) {
            if ($k == 'q') continue;
            $gopts[$k] = $v;
        }
    
        $opts = http_build_query($gopts);
    
        // Preserve the sort choice for selection
        $s1 = $s2 = $s3 = $s4 = '';
    
        switch ($gopts['sort']) {
          case 'alpha_asc'    : $s1 = 'selected="selected"';break;
          case 'alpha_desc'   : $s2 = 'selected="selected"';break;
          case 'sell_price_asc' : $s3 = 'selected="selected"';break;
          case 'sell_price_desc'  : $s4 = 'selected="selected"';break;
        }
    
        // Unset the sort option so that it can be set in the url manually below
        unset($gopts['sort']);
        $opts_sort = http_build_query($gopts);
    
        $output = "
        <div class='product_index_header'>
          <div class='view-selection'>
            <span class='descript'>View: </span>
            <a class='list' href='/products?$opts'>&nbsp;</a>
            <span class='bar'>|</span>
            <a class='grid' href='/products/grid/list?$opts'>&nbsp;</a>
          </div>
          <div class='sortable'>
            <select name='droppy' class='droppy kitteh' onchange=\"window.location.href=$('select.droppy').val()\">
              <option value='#'>Sort</option>
              <option $s1 value='?sort=alpha_asc&amp;$opts_sort'>a-z</option>
              <option $s2 value='?sort=alpha_desc&amp;$opts_sort'>z-a</option>
              <option $s3 value='?sort=sell_price_asc&amp;$opts_sort'>$ - $$</option>
              <option $s4 value='?sort=sell_price_desc&amp;$opts_sort'>$$ - $</option>
            </select>
          </div>
        </div>
        ";
    
        return $output;
    }
    
    推荐文章