dougao9864 2017-06-23 01:54
浏览 16
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • donglin6659 2017-06-23 08:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)