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.:

  1. if $passeditem orange want output mango , orange , strawberry.

  2. if $passeditem mango want output lychee , 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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -