php news records previous next
SELECT
i.images_id
,COALESCE(
(SELECT images.images_id FROM images WHERE images.images_id < i.images_id ORDER BY images.images_id DESC LIMIT 1),
(SELECT images.images_id FROM images ORDER BY images.images_id DESC LIMIT 1)
) AS previous_images_id
,COALESCE(
(SELECT images.images_id FROM images WHERE images.images_id > i.images_id ORDER BY images.images_id ASC LIMIT 1),
(SELECT images.images_id FROM images ORDER BY images.images_id ASC LIMIT 1)
) AS next_images_id
FROM
images i
WHERE
i.images_id = 1
\==========
for record, not for page
1、 get all new\_id in array (PDO fetchAll(PDO::FETCH\_ASSOC) )
> $all\_news\_ids = $crud->sql("select news\_id from news");
2、get $now\_news\_id key for array
> $now\_news\_id\_key\_id = array\_search(array('news\_id'=>$now\_news\_id), $ar\_all\_news\_ids);
3、check $now\_news\_id\_key\_id first, if empty
> $prive\_news\_id = empty($now\_news\_id\_key\_id)?"":get\_record\_column('news\_id', $all\_news\_ids, $now\_news\_id\_key\_id+1);
> $next\_news\_id = empty($now\_news\_id\_key\_id)?"":get\_record\_column('news\_id', $all\_news\_ids, $now\_news\_id\_key\_id-1);
> function get\_array\_column($val, $ar){
> return array\_key\_exists($val, $ar)?$ar\[$val\]:"";
> }
> function get\_record\_column($val, $ar, $rec\_arrow=0){
> return (isset($ar\[$rec\_arrow\]))?get\_array\_column($val, $ar\[$rec\_arrow\]) : "";
> }