Postingan lainnya
Membuat 3 inputan pencarian dengan kata kunci 1 atribut per inputan pencarian di php
mau nanya lagi nih hehe gimana yah caranya membuat fungsi pencarian dengan 3 inputan pencarian masing masing2 inputan pencarian hanya bisa cari kata kunci dengan 1 atribut di DB..
<!DOCTYPE html>
<html lang="en">
<head>
<title>Searching</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
<script src="js/jquery.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php
include 'config.php';
?>
<div class="container" style="padding-top: 20px; padding-bottom: 20px;">
<h3>Form Pencarian Dengan PHP</h3>
<hr>
<form role="form" action="index.php" method="get">
<div class="form-group">
<label>Cari :</label>
<input type="text" name="cari" placeholder="id">
<input type="text" name="cari2" placeholder="nama">
<input type="text" name="cari3" placeholder="fakultas">
</div>
<button type="submit" >Search</button>
</form>
<?php
if(isset($_GET['cari'])){
$cari = $_GET['cari'];
echo "<b>Hasil pencarian : ".$cari."</b>";
}
?>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Nama</th>
<th>Fakultas</th>
</tr>
<?php
if(isset($_GET['cari'])){
$cari = $_GET['cari'];
$data = mysql_query("select * from mhs where id like '%".$cari."%'");
while($d = mysql_fetch_array($data)){
?>
<tr>
<td><?php echo $d['id']; ?></td>
<td><?php echo $d['nama']; ?></td>
<td><?php echo $d['fakultas']; ?></td>
</tr>
<?php }
} ?>
</table>
</div>
</div>
</div>
</body>
</html>
1 Jawaban:
Jawaban Terpilih
Coba edit lagi
<pre> <!DOCTYPE html> <html lang="en"> <head> <title>Searching</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="bootstrap/css/bootstrap.css"> <script src="js/jquery.js"></script> <script src="bootstrap/js/bootstrap.min.js"></script> </head> <body>
&lt;?php
include 'config.php';
?&gt;
&lt;div class="container" style="padding-top: 20px; padding-bottom: 20px;"&gt;
&lt;h3&gt;Form Pencarian Dengan PHP&lt;/h3&gt;
&lt;hr&gt;
&lt;form role="form" action="index.php" method="get"&gt;
&lt;div class="form-group"&gt;
&lt;label&gt;Cari :&lt;/label&gt;
&lt;input type="text" name="cari" placeholder="id"&gt;
&lt;input type="text" name="cari2" placeholder="nama"&gt;
&lt;input type="text" name="cari3" placeholder="fakultas"&gt;
&lt;/div&gt;
&lt;input type="submit" name="simpan" value="Search" placeholder="fakultas"&gt;
&lt;/form&gt;
&lt;?php
if(isset($_GET['cari'])){
$cari = $_GET['cari'];
echo "&lt;b&gt;Hasil pencarian : ".$cari."&lt;/b&gt;";
}
?&gt;
&lt;table class="table table-striped"&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;Nama&lt;/th&gt;
&lt;th&gt;Fakultas&lt;/th&gt;
&lt;/tr&gt;
&lt;?php
if(isset($_GET['simpan'])){
$cari = $_GET['cari'];
$cari2 = $_GET['cari2'];
$cari3 = $_GET['cari3'];
$gabung = "";
if ($cari != "") {
$gabung .= "AND id like '%".$cari."%'";
}
if ($cari2 != "") {
$gabung .= "AND nama like '%".$cari2."%'";
}
if ($cari3 != "") {
$gabung .= "AND fakultas like '%".$cari3."%'";
}
$gabung = "WHERE " .ltrim($gabung, "AND ");
$data = mysql_query("select * from mhs $gabung");
while($d = mysql_fetch_array($data)){
?&gt;
&lt;tr&gt;
&lt;td&gt;&lt;?php echo $d['id']; ?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?php echo $d['nama']; ?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?php echo $d['fakultas']; ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php }
} ?&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</body> </html> </pre>
Tanggapan
kurang paham bang hehehe
dibagian mana yg kurang paham?
sudah bisa gan terimakasih sudah paham