looping saat upload data

Hallo selamat pagi teman2.. saya mau tnya ni, saat saya upload data, action download dan detail nya di inputan kedua jadi double.. 

daftar_buku
<thead>
                    <tr>
                      <th><center>JUDUL</center></th>
                      <th><center>KETERANGAN</center></th>
                      <th><center>FILE</center></th>
                      <th><center>ACTION</center></th>
                    </tr>
              </thead>
              <tbody>
              <?php
                $ambildata  = (mysqli_query($con,"SELECT * FROM tb_perpustakaan"));
                while($data=mysqli_fetch_array($ambildata)){
                ?>
            <tr>
                <td><center><?=$data['nama_buku']?></center></td>
                <td><center><?=$data['keterangan']?></center></td>
                <td><center><?=$data['file']?></center></td>
                <td>
                    <?php
                      $query  = "SELECT * FROM tb_perpustakaan";
                      $run    = mysqli_query($con,$query);

                      while($rows = mysqli_fetch_assoc($run)) {
                    ?>
                  <a href="download.php?file=<?php echo $rows['file'] ?>"><i class="fa fa-download" aria-hidden="true"></i></a>
                  <a href="detail.php?id_perpustakaan=<?php echo $rows['id_perpustakaan']?>"><i class="fa fa-eye"></i></a>
                  <?php
                      };
                    ?>
                </td>
            </tr>
            <?php
        };
          ?>
        </tbody>
                  </table>

download
<?php
    if(!empty($_GET['file'])) {
        $file_name = basename($_GET['file']);
        $filePath  = "berkas/".$file_name;

        if(!empty($file_name) && file_exists($filePath)) {
            //define header
            header("Cache-Control: public");
            header("Content-Description: File Transfer");
            header("Content-Disposition: attachment; filename=$file_name");
            header("Content-Type: application/pdf");
            header("Content-Transfer-Encoding: binary");

            //read file
            readfile($filePath);
            exit;
        } 
            else {
                echo "file not exit";
            }
    }
?>

detail
//query
    $query  = "SELECT * FROM tb_perpustakaan WHERE id_perpustakaan='$id_perpustakaan'";
    //hasil
    $hasil  = $con->query($query);

    //uraikan tb_perpust
    $row = $hasil->fetch_assoc();
?>

                <!-- <div class="col-12"> -->
                  <div class="card-body" col="10">
                <table border = "1" id="daftarbuku" class="table table-bordered table-hover">
                  <thead>
                    <tr>
                      <th><center>JUDUL</center></th>
                      <th><center>KETERANGAN</center></th>
                      <th><center>FILE</center></th>
                    </tr>
              </thead>
              <tbody>
              <?php
                $ambildata  = (mysqli_query($con,"SELECT * FROM tb_perpustakaan"));
                while($data=mysqli_fetch_array($ambildata)){
                ?>
            <tr>
                <td><center><?=$data['nama_buku']?></center></td>
                <td><center><?=$data['keterangan']?></center></td>
                <td><center><?=$data['file']?></center></td>
            </tr>
            <?php
        };
          ?>

hasil
Screenshot-2022-09-20-102932.jpgScreenshot 2022-09-20 102932.jpg


avatar novaje

@novaje

22 Kontribusi 2 Poin


Jawaban Terpilih

Itu di script data_buku, kamu running query dan loopingnya 2x 
<thead>
            <tr>
              <th><center>JUDUL</center></th>
              <th><center>KETERANGAN</center></th>
              <th><center>FILE</center></th>
              <th><center>ACTION</center></th>
            </tr>
      </thead>
      <tbody>
      <?php
        $ambildata  = (mysqli_query($con,"SELECT * FROM tb_perpustakaan")); <-- RUNNING QUERY KE-1
        while($data=mysqli_fetch_array($ambildata)){                        <-- LOOPING KE -1
        ?>
    <tr>
        <td><center><?=$data['nama_buku']?></center></td>
        <td><center><?=$data['keterangan']?></center></td>
        <td><center><?=$data['file']?></center></td>
        <td>
            <?php
              $query  = "SELECT * FROM tb_perpustakaan";
              $run    = mysqli_query($con,$query); <-- RUNNING QUERY KE-2

              while($rows = mysqli_fetch_assoc($run)) { <-- LOOPING KE-2
            ?>
          <a href="download.php?file=<?php echo $rows['file'] ?>"><i class="fa fa-download" aria-hidden="true"></i></a>
          <a href="detail.php?id_perpustakaan=<?php echo $rows['id_perpustakaan']?>"><i class="fa fa-eye"></i></a>
          <?php
              };
            ?>
        </td>
    </tr>
    <?php
};
  ?>
</tbody>
</table>

Coba scriptnya ubah jadi kaya gini :
<table>
  <thead>
    <tr>
      <th><center>JUDUL</center></th>
      <th><center>KETERANGAN</center></th>
      <th><center>FILE</center></th>
      <th><center>ACTION</center></th>
    </tr>
  </thead>
  <tbody>
  <?php
    $ambildata  = (mysqli_query($con,"SELECT * FROM tb_perpustakaan"));
    while($data=mysqli_fetch_array($ambildata)){
    ?>
  <tr>
    <td><center><?=$data['nama_buku']?></center></td>
    <td><center><?=$data['keterangan']?></center></td>
    <td><center><?=$data['file']?></center></td>
    <td>
      <a href="download.php?file=<?php echo $data['file'] ?>"><i class="fa fa-download" aria-hidden="true"></i></a>
      <a href="detail.php?id_perpustakaan=<?php echo $data['id_perpustakaan']?>"><i class="fa fa-eye"></i></a>
    </td>
  </tr>
  <?php
  };
  ?>
  </tbody>
</table>


Semoga berhasil.
avatar ahanafi

@ahanafi

803 Kontribusi 534 Poin

Jawaban

Itu di script data_buku, kamu running query dan loopingnya 2x 
<thead>
            <tr>
              <th><center>JUDUL</center></th>
              <th><center>KETERANGAN</center></th>
              <th><center>FILE</center></th>
              <th><center>ACTION</center></th>
            </tr>
      </thead>
      <tbody>
      <?php
        $ambildata  = (mysqli_query($con,"SELECT * FROM tb_perpustakaan")); <-- RUNNING QUERY KE-1
        while($data=mysqli_fetch_array($ambildata)){                        <-- LOOPING KE -1
        ?>
    <tr>
        <td><center><?=$data['nama_buku']?></center></td>
        <td><center><?=$data['keterangan']?></center></td>
        <td><center><?=$data['file']?></center></td>
        <td>
            <?php
              $query  = "SELECT * FROM tb_perpustakaan";
              $run    = mysqli_query($con,$query); <-- RUNNING QUERY KE-2

              while($rows = mysqli_fetch_assoc($run)) { <-- LOOPING KE-2
            ?>
          <a href="download.php?file=<?php echo $rows['file'] ?>"><i class="fa fa-download" aria-hidden="true"></i></a>
          <a href="detail.php?id_perpustakaan=<?php echo $rows['id_perpustakaan']?>"><i class="fa fa-eye"></i></a>
          <?php
              };
            ?>
        </td>
    </tr>
    <?php
};
  ?>
</tbody>
</table>

Coba scriptnya ubah jadi kaya gini :
<table>
  <thead>
    <tr>
      <th><center>JUDUL</center></th>
      <th><center>KETERANGAN</center></th>
      <th><center>FILE</center></th>
      <th><center>ACTION</center></th>
    </tr>
  </thead>
  <tbody>
  <?php
    $ambildata  = (mysqli_query($con,"SELECT * FROM tb_perpustakaan"));
    while($data=mysqli_fetch_array($ambildata)){
    ?>
  <tr>
    <td><center><?=$data['nama_buku']?></center></td>
    <td><center><?=$data['keterangan']?></center></td>
    <td><center><?=$data['file']?></center></td>
    <td>
      <a href="download.php?file=<?php echo $data['file'] ?>"><i class="fa fa-download" aria-hidden="true"></i></a>
      <a href="detail.php?id_perpustakaan=<?php echo $data['id_perpustakaan']?>"><i class="fa fa-eye"></i></a>
    </td>
  </tr>
  <?php
  };
  ?>
  </tbody>
</table>


Semoga berhasil.
avatar ahanafi

@ahanafi

803 Kontribusi 534 Poin

  • baik bg sudah bisaaa. trimksi banyakk bg :) - @novaje


Login untuk gabung berdiskusi