从上一页获取数组值并显示在表中

从上一页获取数组值并显示在表中

问题描述:

So im trying to figure out how to display the data in table from an array which i get from previous page. below is the code is used to retrieve the barang_id value, the code below works fine, i use it to display "barang_id" which i get from previous page;

<?php
if(isset($_POST['barang_id'])){
  if (is_array($_POST['barang_id'])) {
    foreach($_POST['barang_id'] as $value){
      echo $value . " ";
    }
  } else {
    $value = $_POST['barang_id'];
    echo $value;
  }
}

?>

below is the screenshot where there are two values from barang_id which is 15 and 16,

enter image description here

my question is how do is i perform select statement then display it in table with respect to the id that i retrieved, below is the code i tried, it doesn't work, so im stuck. i tried googling, but my understanding is too weak as im unable to help myself.

<table class="table table-striped table-bordered">
  <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Description</th>

  </tr>
  <?php
  $sql2 = "SELECT * FROM `tbl_barang` WHERE `barang_id` IN (' . implode(',', array_map('intval', $value)) . ')";
  $result = $mydb->query($sql2);

  while ($detailrow = $result->fetch_array()) {
  ?>
  <tr>
    <td><?php echo $detailrow['barang_id']; ?></td>
    <td><?php echo $detailrow['name']; ?></td>
    <td><?php echo $detailrow['description']; ?></td>

  </tr>
  <?php } ?>
  </table>

any help is appreciated. thank you in advance

Replace your query

$sql2 = "SELECT * FROM `tbl_barang` WHERE `barang_id` IN (' . implode(',',$value)) . ')";

with the below code

$sql2 = "SELECT barang_id, name, description FROM tbl_barang WHERE barang_id IN (" . implode(',', $_POST['barang_id']) . ")";

before that make sure $_POST['barang_id'] should not empty.