【问题标题】:How to change results ordering for MySQL query?如何更改 MySQL 查询的结果排序?
【发布时间】:2023-03-23 15:03:01
【问题描述】:

我正在尝试获取页面的上一个和下一个节点链接/缩略图,并根据其标题或文件 URI 或文件名对结果进行排序...

代码是查询数据库并根据节点ID(nid,n.nid)输出上一个和下一个节点链接。我想根据节点标题(title,n.title),文件名(filename,f.filename)甚至文件URI(uri,f.uri)对结果进行排序。

但是,当我更改此行时:

->orderBy('n.nid', $order)

到:

->orderBy('n.title', $order)

这行不通。唯一的区别是,如果您从一个图片库的最后一页移动到另一个,它会稍微改变顺序,但在画廊内部,一切都是一样的。问题是如果您有一个画廊并决定在一段时间后插入新图像。节点 ID 现在与其他节点 ID 完全不同,此代码无法识别它。

我还尝试更改出现nid 的其他部分,但它不起作用。 我想这对于更了解 MySQL 和 PHP 的人来说是微不足道的,但我坚持了几个小时,希望能得到任何帮助。

这是整个代码(来自 Vlad Stratulat,最初发现 here):

模板.php

function dad_prev_next($nid = NULL, $op = 'p', $start = 0) {
if ($op == 'p') {
    $sql_op = '>';
    $order = 'ASC';
}
elseif ($op == 'n') {
    $sql_op = '<';
    $order = 'DESC';
}
else {
    return NULL;
}

$output = '';

// your node must have an image type field
// let's say it's name is IMAGEFIELD
// select from node table
$query = db_select('node', 'n');
// join node table with image field table
$query->leftJoin('field_data_field_IMAGEFIELD', 'i', 'i.entity_id = n.nid');
// join file managed table where all data about managed files stored
$query->leftJoin('file_managed', 'f', 'f.fid = i.field_IMAGEFIELD_fid');
$query
    // select nid and title from node
    ->fields('n', array('nid', 'title'))
    // select uri from file_managed (image path)
    ->fields('f', array('uri'))
    // select image alt and title
    ->fields('i', array('field_IMAGEFIELD_alt', 'field_IMAGEFIELD_title'))
    // where nid "greater than"/"lower than" our current node nid
    ->condition('n.nid', $nid, $sql_op)
    // where node type in array('your content types')
    ->condition('n.type', array('PHOTOS'), 'IN')
    // where node is published
    ->condition('n.status', 1)
    // where requested node has image to display (if you want thumbnail)
    ->condition('f.uri', '', '!=')
    // order by nid
    ->orderBy('n.nid', $order)
    // limit result to 1
    ->range($start, 1);     

// make query
$result = $query->execute()->fetchAll();

foreach ($result as $node) {
    // theme your thumbnail image
    $variables = array(
        // default image style name `thumbnail`
        // you can use your own by following
        // admin/config/media/image-styles on your site
        'style_name' => 'thumbnail',
        'path' => $node->uri,
        'alt' => $node->field_IMAGEFIELD_alt,
        'title' => $node->field_IMAGEFIELD_title
    );
    $image = theme('image_style', $variables);

    $options = array(
        'html' => TRUE,
        'attributes' => array(
            'title' => $node->title
        )
    );
    $output = l($image, "node/{$node->nid}", $options);
}

return $output;
}

Node.tpl.php

<?php print dad_prev_next($node->nid, 'p', 0); ?>
<?php print dad_prev_next($node->nid, 'n', 0); ?>

编辑 2:

尝试使用strcmp函数:

function dad_prev_next($title = NULL, $op = 'p', $start = 0) {
if ($op == 'p') {
    $strcmp = '1';
    $order = 'ASC';
}
elseif ($op == 'n') {
    $strcmp = '2';
    $order = 'DESC';
}
else {
    return NULL;
}

$output = '';

// your node must have an image type field
// let's say it's name is IMAGEFIELD
// select from node table
$query = db_select('node', 'n');
// join node table with image field table
$query->leftJoin('field_data_field_IMAGEFIELD', 'i', 'i.entity_id = n.nid');
// join file managed table where all data about managed files stored
$query->leftJoin('file_managed', 'f', 'f.fid = i.field_IMAGEFIELD_fid');
$query
    // select nid and title from node
    ->fields('n', array('nid', 'title'))
    // select uri from file_managed (image path)
    ->fields('f', array('uri'))
    // select image alt and title
    ->fields('i', array('field_IMAGEFIELD_alt', 'field_IMAGEFIELD_title'))
    // where node type in array('your content types')
    ->condition('n.type', array('PHOTOS'), 'IN')
    // where node is published
    ->condition('n.status', 1)
    // where requested node has image to display (if you want thumbnail)
    ->condition('f.uri', '', '!=')
    // order by nid
    ->orderBy('n.title', $order)
    // limit result to 1
    ->range($start, 1);     

// make query
$result = $query->execute()->fetchAll();

foreach ($result as $node) {
    // theme your thumbnail image
    $variables = array(
        // default image style name `thumbnail`
        // you can use your own by following
        // admin/config/media/image-styles on your site
        'style_name' => 'thumbnail',
        'path' => $node->uri,
        'alt' => $node->field_IMAGEFIELD_alt,
        'title' => $node->field_IMAGEFIELD_title
    );
    $image = theme('image_style', $variables);

    $options = array(
        'html' => TRUE,
        'attributes' => array(
            'title' => $node->title
        )
    );
    $output = l($image, "node/{$node->nid}", $options);
}

return $output;
}

现在没有记录错误,但始终显示相同的照片 - 按字母顺序排列的第一张照片中的 2 张和最后一张中的 2 张。

【问题讨论】:

  • 您想用-&gt;condition('n.nid', $nid, $strcmp) 实现什么目标?它在任何情况下都不起作用,因为-&gt;condition() 的第三个参数可能是&gt;&lt;=&lt;&gt;&gt;=&lt;=IN、'NOT IN' 等。
  • @VladStratulat 好的,感谢您提供的信息。正如我所说,我是一个编程新手。但有一件事我在这里没有得到 - 如果它是一个只有一些数字的文本字符串(例如“标题号 2”),我为什么要将它与大于/小于字符的字符进行比较?不应该是字母表的东西吗?如果我可以使用&gt; &lt;,那么除了-&gt;condition('n.nid', $nid, $sql_op)-&gt;condition('n.title', $title, $sql_op) 并使用-&gt;orderBy('n.title', $order) 之外,我为什么还要更改任何内容?不幸的是,这并没有奏效......
  • 在你的情况下-&gt;condition('n.nid', $nid, $strcmp) 是没用的。如果您只想按标题订购,只需从查询中删除此条件,-&gt;orderBy('n.title', $order) 就可以完成它的工作。
  • @VladStratulat 那太好了。我已经删除了那条线(你可以检查我的新编辑),但现在它总是输出相同的照片 - 字母表中的第一张照片和最后一张照片中的 2 张。但是,现在没有错误,所以我想它已经接近解决方案,但我缺少一些简单的东西。我也尝试使用您的原始代码并删除该行(添加标题而不是 nid),结果是相同的。

标签: php mysql drupal drupal-7


【解决方案1】:

您按 n.title 排序,但您的第一个条件是查找大于/小于 $nid 的 n.nid。这没有任何意义,并导致半随机选择。我建议在按标题排序时将条件更改为 n.title $node->title,它会起作用。

【讨论】:

  • 你是对的。我确实将-&gt;condition('n.nid', $nid, $sql_op) 更改为-&gt;condition('n.title', $title, $sql_op),但它没有用。我的猜测是标题不能使用$sql_op,因为它寻找“大于/小于”。但是,对于您按字母顺序排列的东西,正确的表达方式是什么?
  • 我认为 STRCMP 功能可以满足您的需求:dev.mysql.com/doc/refman/5.0/en/…
  • 谢谢,这绝对是我需要的!我已经用更改后的代码编辑了我的问题。记录了一个错误,但我不确定我需要另外更改什么。它只是列出了 template.php 中的所有指定参数
  • 您将 $strcmp 设置为 1 或 2,然后将其放入查询中。您实际上从未在查询中使用 STRCMP() 函数。在查询错误中查找“n.title 2 :db_condition_placeholder_0”。 2 来自您的 $strcmp 变量。
  • 感谢您的建议。看来我应该重写整个“dad_prev_next”函数,这肯定看起来太难了,因为我从来没有在 php 中写过任何严肃的事情。您能否尝试更详细地解释一下,以便我接受您的回答?
猜你喜欢
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多