How to access a php var outside a while array, mat

2019-08-19 07:13发布

问题:

I'm trying to understand a (probably) basic procedure but I think I'm missing something. So I used a prepared statement to get some data from a mysql database:

<select>
  <?php
    $select_item_name = "select * from item order by item_name ASC";
    $run_item = mysqli_query($con, $select_item_name);
      while($row=mysqli_fetch_array($run_item)){
      $item_id = $row['item_id'];
      $item_name = $row['item_name'];
      $item_price = $row['item_price'];
      $item_desc = $row['item_desc'];
      echo "<option value='$item_desc'>$item_name</option>";
      }
  ?> 
</select>

So when selecting a item name, I am able to access the selected item description, because I use the value assigned in option. But what if I want to access the selected item price or id? In the same page, in another php tag, outside the while, I want to access the selected Item id and price, how do you do that?

<?php 
  echo "$item_id . "has a price of " . $item_price";
?>

If I do as shown in the code above, I get the last value in the database, and it does not change dinamically when selecting another item.

@Ryan Vincent As you can see here:

echo "<option value='$item_desc'>$item_name</option>";

The while loop shows all db item names in the select dropdown. Let's say I select item2 from the list. I want to show in another point of the page, item2's id and item2's price. So when changing the option from the dropdown I want those values to dinamically change. The issue here is the fact that I want to access those specific values from outside the while loop so I don't know how to call them. Can you help me?