Postingan lainnya
Foreach if PHP
Dear Master dan Gan,
Mau tanya kenapa saya pakai foreach seperti ini data nya tidak muncul ya. Apakah ada yg salah?
//Melakukan query $sql = "SELECT * FROM barang"; $hasil = $koneksi->query($sql); $no = 1; if ($hasil->num_rows > 0) { foreach ($hasil as $row) { ?>
Data yg muncul hanya kosong saja. Mohon masukkan nya.
Terima kasih master dan gan.
0
3 Jawaban:
gk bisa langsung pake forech bang, harus fetch dulu
<?php while ( $row = mysqli_fetch_assoc($hasil)): ?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $row['nama_barang']; ?></td>
<td><?php echo $row['desc_barang']; ?></td>
</tr>
<?php endwhile; ?>
atau oop modelnya
<?php while ( $row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $row['nama_barang']; ?></td>
<td><?php echo $row['desc_barang']; ?></td>
</tr>
<?php endwhile; ?>
semoga membantu mohon dikoreksi kalo ada salah :)
0
Berikut Syntax saya secara keseluruhan. Kenapa masih blank ya hasil nya?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Edit data di dalam form Modal Bootstrap</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<table class="table table-bordered">
<tr>
<th>No</th>
<th>data yang akan ditampilkan</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "test";
$dbname = "test";
// Membuat Koneksi
$koneksi = new mysqli($servername, $username, $password, $dbname);
// Melakukan Cek Koneksi
if ($koneksi->connect_error) {
die("Koneksi Gagal : " . $koneksi->connect_error);
}
//Melakukan query
$sql = "SELECT * FROM barang";
$hasil = $koneksi->query($sql);
$no = 1;
if ($hasil->num_rows > 0) {
foreach ($hasil as $row) { ?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $row['nama_barang']; ?></td>
<?php echo "<td><a href='#myModal' class='btn btn-default btn-small' id='custId' data-toggle='modal' data-id=".$row['id'].">Detail</a></td>"; ?>
</tr>
<?php
$no++;
}
} else {
echo "0 results";
} $koneksi->close();
?>
</table>
</div>
</div>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Detail Barang</h4>
</div>
<div class="modal-body">
<div class="fetched-data"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Keluar</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
var rowid = $(e.relatedTarget).data('id');
//menggunakan fungsi ajax untuk pengambilan data
$.ajax({
type : 'post',
url : 'detail.php',
data : 'rowid='+ rowid,
success : function(data){
$('.fetched-data').html(data);//menampilkan data ke dalam modal
}
});
});
});
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
0
coba foreachnya diganti bang, kyk yg saya kasih itu
if ($hasil->num_rows > 0) {
while ( $row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $row['nama_barang']; ?></td>
<td><?php echo $row['desc_barang']; ?></td>
</tr>
<?php endwhile;
} else {
echo "0 results";
} $koneksi->close();
?>
0