如何更改结果的排序为MySQL查询?(How to change results ordering

2019-10-17 08:54发布

我试图让我的网页上一个和下一个节点的链接/缩略图,并根据它们的标题或文件的URI或文件名排序的结果...

该代码是根据节点ID(NID,n.nid)查询数据库,并输出前一个和下一个节点的链接。 我想订购的结果根据任一节点标题(标题,n.title),文件名(文件名,f.filename),或甚至文件的URI(URI,f.uri)。

然而,当我改变这一行:

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

至:

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

它不会工作。 唯一的区别是,它会稍微改变顺序,如果你从最后一页的图片库移动到另一个,但在其他画廊的一切内部是一样的。 问题是,如果你有一个画廊,并决定了一会儿后插入一个新的形象。 节点ID是现在和别人完全不同的,这个代码不把它捡起来。

我也曾尝试与变化的其他部分,其中nid显示出来,但它不工作。 我想,它的一些小事的人谁了解MySQL和PHP更好,但我坚持了好几个小时,并希望得到任何帮助。

这里是整个代码(从弗拉德Stratulat,最初发现这里 ):

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

有没有现在记录的错误,但也有始终显示相同的照片 - 首当其冲的2和字母列表中的最后的2。

Answer 1:

你被n.title排序,但那么你的第一条件是在寻找一个n.nid高于/低于NID $更大。 这是没有意义的,并导致半随机选择。 我建议按标题排序当你改变条件的东西n.title $节点 - >标题,它会工作。



文章来源: How to change results ordering for MySQL query?