Postingan lainnya
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.jpg
1 Jawaban:
Jawaban Terpilih
<div>Itu di script data_buku, kamu running query dan loopingnya 2x </div><pre><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)) { &lt;-- LOOPING KE-2
?&gt;
&lt;a href="download.php?file=&lt;?php echo $rows['file'] ?&gt;"&gt;&lt;i class="fa fa-download" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;
&lt;a href="detail.php?id_perpustakaan=&lt;?php echo $rows['id_perpustakaan']?&gt;"&gt;&lt;i class="fa fa-eye"&gt;&lt;/i&gt;&lt;/a&gt;
&lt;?php
};
?&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;?php
}; ?> </tbody> </table></pre><div><br>Coba scriptnya ubah jadi kaya gini :</div><pre><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></pre><div><br><br>Semoga berhasil.</div><div><br></div>
Tanggapan
baik bg sudah bisaaa. trimksi banyakk bg :)