我试图获取我的页面的上一个和下一个节点链接/缩略图,并根据它们的标题或文件URI或文件名排序结果...如何更改MySQL查询的结果排序?
代码查询数据库并输出上一个和下一个节点链接根据节点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,最初发现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;
}
现在没有错误记录,但总是显示相同的照片 - 第一个和第二个按字母顺序排列的列表中的2个。
你想用' - > condition('n.nid',$ nid,$ strcmp)''实现什么?它不会在任何情况下工作,因为' - > condition()'的第三个参数可能是'>','<', '=', '<>','> =','<=','IN','NOT IN'等。 –
@ VladStratulat好的,谢谢你的信息。正如我所说,我是一个编程新手。但有一件事我不明白 - 如果它是一个只包含一些数字的文本字符串(E. G.“Title number 2”),为什么我会比较大/小于字符?它不应该是字母表的东西吗?如果我可以使用'><',那么为什么我必须改变任何东西,除了' - > condition('n.nid',$ nid,$ sql_op)''''condition('n.title', $ title,$ sql_op)'并使用' - > orderBy('n.title',$ order)'?而这不起作用... – take2
在你的情况' - >条件('n.nid',$ nid,$ strcmp)'是没用的。如果你只是想按标题排序,只需从查询中删除这个条件,' - > orderBy('n.title',$ order)'就可以完成这项工作。 –