使用带有 drupal 视图的自定义动态查询

发布于 2024-09-08 12:21:44 字数 620 浏览 3 评论 0原文

我所说的动态是指我希望能够根据用户输入更改 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";
     }
}
?>

By dynamic I mean I want to be able to change the sql query based on user input.

Say if this is my custom query, how do I change it so that it toggles between ORDER BY .. Descending and Ascending when a user clicks the column? Is this even possible when you override the query that Views generates?

<?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";
     }
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-09-15 12:21:44

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

我创建一个模块来包含处理排序的函数。创建类型为“Global:Null”的参数。它可以出现在列表中的任何位置,但该 url 位置中必须存在参数,否则代码将不会执行。我通常添加标题或搜索类型。设置参数以显示所有值(如果不存在)并添加验证器。选择一个 PHP 验证器并添加以下内容。

return _mymodule_handle_sortables($view);

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

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

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'> </a>
        <span class='bar'>|</span>
        <a class='grid' href='/products/grid/list?$opts'> </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&$opts_sort'>a-z</option>
          <option $s2 value='?sort=alpha_desc&$opts_sort'>z-a</option>
          <option $s3 value='?sort=sell_price_asc&$opts_sort'>$ - $</option>
          <option $s4 value='?sort=sell_price_desc&$opts_sort'>$ - 
lt;/option>
        </select>
      </div>
    </div>
    ";

    return $output;
}

Careful, here lie dragons.
Here's how I've done exactly that. First, you you create the view that displays the content of interest. Add sorts on all of the fields you are interested in (count, price, title, etc) Don't worry about them all not working well together, you'll be removing/altering them dynamically in code.

I create a module to contain a function to handle the sorting. Create an argument of type "Global:Null". It can come at any point in you're list, but there must be an argument present in that url location or the code will not execute. I usually add a title or search type. Set the argument to display all values if not present and add a validator. Choose a PHP validator and add the following.

return _mymodule_handle_sortables($view);

That will allow you to edit the contents of the function quickly/easily without having to edit view / save view / preview view at each iteration. Note that I pass the $_GET variable. That's not strictly necessary, since it should be available in the function anyway. I just do it for easier readability.

First step, get the names of the sortable fields with the devel module

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

Preview the view and note the names of the fields. Once you've got them, you can do the following to alter the output of the view.

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

Add a PHP header to your view and add the following to it

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

Now you can dynamically display a sort header. Here's an admittedly overdone function to do that.

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'> </a>
        <span class='bar'>|</span>
        <a class='grid' href='/products/grid/list?$opts'> </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&$opts_sort'>a-z</option>
          <option $s2 value='?sort=alpha_desc&$opts_sort'>z-a</option>
          <option $s3 value='?sort=sell_price_asc&$opts_sort'>$ - $</option>
          <option $s4 value='?sort=sell_price_desc&$opts_sort'>$ - 
lt;/option>
        </select>
      </div>
    </div>
    ";

    return $output;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文