如何更改MySQL查询的结果排序?

时间:2012-10-29 11:02:24

标签: php mysql drupal drupal-7

我正在尝试为我的网页获取上一个和下一个节点链接/缩略图,并根据其标题或文件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):

的template.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;
}

现在没有记录任何错误,但总会显示相同的照片 - 第一个中的两个和按字母顺序列表中最后一个中的两个。

1 个答案:

答案 0 :(得分:0)

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