Hi berikut akan saya berikan tips sederhana membuat pencarian di setiap kata kunci yang dimasukkan dalam search form, tentunya perlu anda lengkapi sendiri
Ambillah contoh saya punya table sederhana tbpegawai dalam database dbcoba dengan kolom kolom id, nama, kota dan saya fokus ingin menggunakan kolom nama sebagai target pencarian
maka berikut php simplenya
<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "dbcoba";
$koneksi = mysqli_connect($host,$user,$password);
$dbkonek = mysqli_select_db($koneksi,$dbname) or die("error euy ");
if(isset($_GET['txtnama'])) {
$search = str_replace(" ","|",$_GET['txtnama']);
$sql= " SELECT * FROM tbpegawai WHERE nama REGEXP '$search' ";
$run = mysqli_query($koneksi,$sql) or die(mysqli_error($koneksi));
$row = mysqli_fetch_assoc($run);
$total = mysqli_num_rows($run);
}
?>
<body>
<form action="cari.php" method="GET">
<input type="text" name="txtnama" id="txtnama" placeholder="Cari" value="<?php echo $_GET['txtnama']; ?>">
<button type="submit" value="Cari" > Cari</button>
</form>
<?php if($total>0) { ?>
<table border="1" width="500">
<thead>
<tr>
<th> <strong>Hasil Cari <?php echo $total ; ?></strong> </th>
</tr>
</thead>
<tbody>
<?php do{ ?>
<tr>
<td><?php echo $row['nama']; ?> </td>
</tr>
<?php } while($row=mysqli_fetch_assoc($run)) ;?>
</tbody>
</table>
<?php } ?>
</body>
<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "dbcoba";
$koneksi = mysqli_connect($host,$user,$password);
$dbkonek = mysqli_select_db($koneksi,$dbname) or die("error euy ");
if(isset($_GET['txtnama'])) {
$search = str_replace(" ","|",$_GET['txtnama']);
$sql= " SELECT * FROM tbpegawai WHERE nama REGEXP '$search' ";
$run = mysqli_query($koneksi,$sql) or die(mysqli_error($koneksi));
$row = mysqli_fetch_assoc($run);
$total = mysqli_num_rows($run);
}
?>
<body>
<form action="cari.php" method="GET">
<input type="text" name="txtnama" id="txtnama" placeholder="Cari" value="<?php echo $_GET['txtnama']; ?>">
<button type="submit" value="Cari" > Cari</button>
</form>
<?php if($total>0) { ?>
<table border="1" width="500">
<thead>
<tr>
<th> <strong>Hasil Cari <?php echo $total ; ?></strong> </th>
</tr>
</thead>
<tbody>
<?php do{ ?>
<tr>
<td><?php echo $row['nama']; ?> </td>
</tr>
<?php } while($row=mysqli_fetch_assoc($run)) ;?>
</tbody>
</table>
<?php } ?>
</body>
Silahkan dikembangkan dan dirapihkan sendiri
Regards,
HERY PURNAMA
Certified IT Trainer
Call/ SMS/WA : 081.223344.506 , BBM : 7DC633AA
Blog :
Social Net :
Hometown : Bandung - Jawa Barat
Please consider your environmental responsibility. Before printing this e-mail message, ask yourself whether you really need a hard copy.
0 comments:
Post a Comment
Silahkan isikan comment box untuk komentar Anda..