select filter,id
from `table1`
where id = 475
or id = ( select max(id) from `table1` where id < 475)
or id = ( select min(id) from `table1` where id > 475)$Q = $modx->newQuery('table1');
$Q->select(array('id','resource_id','alias'));
$Q->where(array(
));
$Q->prepare();
$Q->stmt->execute();
$out=$Q->stmt->fetchAll(PDO::FETCH_ASSOC);sounds great))$Q->where(array('field'=>'value'));
where id = 475 or id = ( select max(id) from `table1` where id < 475) or id = ( select min(id) from `table1` where id > 475)
<?php
$classname = 'table1';
$id = 475;
$out = array();
//prev object
$c= $modx->newQuery($classname);
$c->where(array('id:<'=>$id));
$c->select(array('id','resource_id','alias'));
$c->sortby('id',DESC);
$c->limit('1');
if ($object = $modx->getObject($classname,$c)){
$out[] = $object->toArray('',false,true);
}
//current object
$c= $modx->newQuery($classname);
$c->where(array('id'=>$id));
$c->select(array('id','resource_id','alias'));
if ($object = $modx->getObject($classname,$c)){
$out[] = $object->toArray('',false,true);
}
//next object
$c= $modx->newQuery($classname);
$c->where(array('id:>'=>$id));
$c->select(array('id','resource_id','alias'));
$c->sortby('id',ASC);
$c->limit('1');
if ($object = $modx->getObject($classname,$c)){
$out[] = $object->toArray('',false,true);
}
echo '<pre>' . print_r($out,1) . '</pre>';
to get the same result, you can try something like that:
<!--?php $classname = 'table1'; $id = 475; $out = array(); //prev object $c= $modx--->newQuery($classname); $c->where(array('id:<'=>$id)); $c->select(array('id','resource_id','alias')); $c->sortby('id',DESC); $c->limit('1'); if ($object = $modx->getObject($classname,$c)){ $out[] = $object->toArray('',false,true); } //current object $c= $modx->newQuery($classname); $c->where(array('id'=>$id)); $c->select(array('id','resource_id','alias')); if ($object = $modx->getObject($classname,$c)){ $out[] = $object->toArray('',false,true); } //next object $c= $modx->newQuery($classname); $c->where(array('id:>'=>$id)); $c->select(array('id','resource_id','alias')); $c->sortby('id',ASC); $c->limit('1'); if ($object = $modx->getObject($classname,$c)){ $out[] = $object->toArray('',false,true); } echo '<pre>' . print_r($out,1) . '</pre>';
<?php
/*[[!navigation?&className=`ShopContent`&packageName=`shop`&prevClass=`nextLeft`&nextClass=`nextRight`&delimiter=`|`&urlSuffix=`.html`]]
* prevClass /nextClass - css class for next/prev links
* delimiter - if need to put something between next/prev links
* urlSuffix - if need to generate url output with special suffix.html *
*/
$config = array(
'urlSuffix'=>'.html',
'packageName' => 'shop',
'className' => 'ShopContent',
'delimiter' => '|',
'prevClass' => 'nextLeft',
'nextClass' => 'nextRight'
);
$config=array_merge($config,$scriptProperties);
$config ['parent_field'] = empty($config['className']) || $config['className']=='modResource' ? "parent" : "resource_id";
$out='';
$resource_id = $modx->resource->get($config['parent_field']);
$id = $modx->resource->get('id');
if($config['className'] != 'modResource'){
$modelpath = $modx->getOption('core_path') . 'components/' . $config['packageName'] . '/model/';
$added = $modx->addPackage($config['packageName'], $modelpath);
}
$query = $modx->newQuery($config['className']);
$query->select(array('id',$config['parent_field'],'alias','pagetitle'));
$query->where(array('resource_id' =>$resource_id));
$query->where(array(
array(
'id= ( SELECT MAX(id) FROM '.$modx->getTableName($config['className']).' WHERE id < '.$id.')',
'id= ( SELECT MIN(id) FROM '.$modx->getTableName($config['className']).' WHERE id > '.$id.')',
'id= ( SELECT MAX(id) FROM '.$modx->getTableName($config['className']).' WHERE '.$config['parent_field'].' = '.$resource_id.')',
'id= ( SELECT MIN(id) FROM '.$modx->getTableName($config['className']).' WHERE '.$config['parent_field'].' = '.$resource_id.')'
)
),xPDOQuery::SQL_OR);
$query->prepare();
//$modx->log(modX::LOG_LEVEL_ERROR, $query->toSql());
$query->stmt->execute();
$dbout=$query->stmt->fetchAll(PDO::FETCH_ASSOC);
if($dbout){
$prevId=0;
$nextId=0;
$maxId=0;
$minId=0;
$links=array();
foreach($dbout as $r){
if($r['id']> $id){
if($nextId==0 || $nextId>$r['id']){$nextId =$r['id'];}
if($maxId==0 || $maxId<$r['id']){$maxId =$r['id'];}
}
elseif ($r['id']< $id){
if($prevId==0 || $prevId<$r['id']) {$prevId=$r['id'];}
if($minId==0 || $minId>$r['id']) {$minId=$r['id'];}
}
if (!isset($resurces[$r['id']])) $resurces[$r['id']]=array();
$resurces[$r['id']]['resource_id'] = $r['resource_id'];
$resurces[$r['id']]['alias'] = $r['alias'];
$resurces[$r['id']]['pagetitle'] = $r['pagetitle'];
}
if($prevId == 0 && $maxId!=$nextId) $prevId=$maxId;
if($nextId == 0 && $minId!=$prevId) $nextId=$minId;
if($prevId!=0) {
$links[0]='<a href="'.$modx->makeUrl($resurces[$prevId]['resource_id']).$resurces[$prevId]['alias'].$config['urlSuffix'].'"
class="'.$config['prevClass'].'" title="'.$resurces[$prevId]['pagetitle'].'">назад</a>';
}
if($nextId!=0) {
$links[1]='<a href="'.$modx->makeUrl($resurces[$nextId]['resource_id']).$resurces[$nextId]['alias'].$config['urlSuffix'].'"
class="'.$config['nextClass'].'" title="'.$resurces[$nextId]['pagetitle'].'">вперед</a>';
}
$out=implode($config['delimiter'],$links);
}
return $out;