问题
I'm trying to get the previous and next node link/thumbnail for my pages and order the results according to their titles or file URIs or filenames...
The code is querying the database and outputting previous and next node links according to node IDs (nid, n.nid). I want to order the results according to either node title (title, n.title), filename (filename, f.filename), or even file URI (uri, f.uri).
However, when I change this line:
->orderBy('n.nid', $order)
to:
->orderBy('n.title', $order)
it won't work. The only difference is that it slightly changes the order if you move from the last page in one image gallery to another, but otherwise inside of the galleries everything is the same. The problem is if you have one gallery and decide to insert a new image after a while. Node ID is now totally different from the others and this code doesn't pick it up.
I have also tried with changing other parts where nid
shows up, but it's not working.
I suppose that it's something trivial for someone who understands MySQL and PHP better, but I'm stuck with it for hours and would appreciate any help.
Here is the whole code (from Vlad Stratulat, originally found 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); ?>
EDIT 2:
Trying to use strcmp function:
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;
}
There are no errors logged now, but there are always same photos displayed - 2 of the first ones and 2 of the last ones in the alphabetical list.
回答1:
You're sorting by n.title, but then your first condition is looking for an n.nid greater than/lower than $nid. This makes no sense, and results in a semi-random selection. I suggest when sorting by title you change the condition to something n.title $node->title, and it would work.
来源:https://stackoverflow.com/questions/13120363/how-to-change-results-ordering-for-mysql-query