Postingan lainnya
Kelas Premium!
Belajar bikin website dari nol sekarang
Gunakan kupon "lebihcepat" untuk diskon 25%!
Menambahkan For-Each php
Halo, Saya ingin menggunakan perulangan foreach agar saat user menginputkan data, data langsung ditampilkan di bawahnya secara berurutan dan tidak berantakan.. nah pertanyaannya, dibagian manakah saya harus menambahkan perulangan for each? terima kasih..
kode timeline.php
<?php
require_once("auth.php");
//include "koneksi.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="icon" type="image/png" href="Assets/icon.png">
<link rel="stylesheet" type="text/css" href="Assets/timeline.css">
<title>Timeline HeadBook</title>
<script type="text/javascript" src="Assets/jquery.js" ></script>
<link rel="stylesheet" href="css/bootstrap.min.css" />
</head>
<body class="bg-light">
<div class="container mt-5" style="height:100%;">
<div class="row">
<div class="col-md-4">
<div class="card">
<div class="card-body text-center">
<img class="img img-responsive rounded-circle mb-3" width="160" src="Assets/<?php echo $_SESSION['user'][7] ?>" />
<h3><?php echo $_SESSION['user'][1]." ".$_SESSION['user'][2] ?></h3>
<p><?php echo $_SESSION['user'][3] ?></p>
<p><a href="logout.php">Logout</a></p>
</div>
</div>
</div>
<div class="col-md-8">
<form action="" method="post" >
<div class="form-group">
<textarea type="text" name="Nama" style="display:none;"><?php echo $_SESSION['user'][3] ?> </textarea>
<textarea class="form-control" placeholder="Apa yang kamu pikirkan?" name="status"></textarea>
<br>
<button type="submit" class="btn btn-danger" name="tombol">Bagikan</button>
</div>
<?php
require_once("config.php");
if(isset($_POST['tombol'])){
$email = $_REQUEST['Nama'];
$status = $_REQUEST['status'];
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die ("Koneksi gagal");
$mysqli = "INSERT INTO timeline (Email, Status) VALUES ('$email', '$status')";
$result = mysqli_query($conn, $mysqli);
//$rows = $result->fetch(PDO::FETCH_NUM);
//$_SESSION['user1'] = $rows;
//echo "<script>history.back()</script>";
} ?>
<div>
<?php
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die ("Koneksi gagal");
$select = mysqli_query($conn, "SELECT * FROM timeline ORDER BY id DESC");
while($stat = mysqli_fetch_array($select)){
echo "
<div class='card mb-3' style=''>
<div class='card-body'>
<div class='row' style='background-color:#9F3'>
<div class='media'>
<img src='Assets/default.svg' class='media-object img-rounded' width='50' height='50' alt='Sample Image'>
<div class='media-body'>
<h4 class='media-heading'><small>".$stat['Email'].' | '.$stat['Waktu'].
"<i></i></small></h4>
<br><hr>
<p>".$stat['Status']."</p>
</div>
</div>
<div class='row'>
</div>
</div>
<input type='submit' value='Edit'>
<input type='submit' value='Hapus' name='hapus'>
</div>
</div>
</div>
<br>
"
;
}
mysqli_close($conn);
?>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
1 Jawaban:
Jawaban Terpilih
Tergantung kamu mau tampilkan dimana :D cek kelas ini sebagai contoh <a href='https://sekolahkoding.com/kelas/membuat-blog-dengan-php/video/koneksi-dan-menampilkan-data '>Menampilkan data di PHP</a>
Tanggapan
Sudah bang, saya tambahkan <div> untuk membungkus echo nya dan berhasil :D
cakep! semangat belajarnya!