php - Pass a value and call previous, current and next values in order -
i'm using pdo. want able fetch 3 values 3 different rows using single query. i've got table below.
itemid | item_name ____________________ 125 | apple 297 | lychee 851 | mango 005 | orange 1009 | strawberry
i want able send item_name
, sort table item_name
, able call next item_name
, previous item_name
. how can this?
e.g.:
if
$passeditem
orange
want outputmango
,orange
,strawberry
.if
$passeditem
mango
want outputlychee
,mango
,orange
using on()
operator did not work because 1 item_name passed on query.
$sql = "select item_name itemstable item_name = :passeditem" order asc; $stmt = $con_db->prepare($sql); $stmt->execute(array(':passeditem'=>"orange")); $rslt = $stmt->fetchall(pdo::fetch_assoc);
this solution based on mysql: conditionally selecting next , previous rows.
$sql = "(select item_name itemstable item_name < ? order item_name desc limit 1) union (select item_name itemstable itemname = ?) union (select item_name itemstable itemname > ? order item_name asc limit 1)"; $stmt = $con_db->prepare($sql); $stmt->execute(array("orange", "orange", "orange")); $rslt = $stmt->fetchall(pdo::fetch_assoc);
you need pass orange
3 times fill in placeholders.
you can output with:
foreach($rslt $row) { echo $row['item_name'] . '<br>'; }
Comments
Post a Comment