Daftar dan Belajar Sekarang!

Membuat 3 inputan pencarian dengan kata kunci 1 atribut per inputan pencarian di php

foto Obby14
107 Kontribusi 7 Coto
3 bulan yang lalu
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.. https://i.postimg.cc/kX3TWxFY/sscffff.png
<!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>

Tag: php ajax

✔ Forum ini sudah terjawab


Jawaban
foto verified logo Nandar
608 Kontribusi 179 Coto
3 bulan yang lalu
Coba edit lagi
<!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>

				<input type="submit" name="simpan" value="Search" placeholder="fakultas">
				</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['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)){
					?>
					<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>
answered icon
foto verified logo Nandar
608 Kontribusi 179 Coto
3 bulan yang lalu
Coba edit lagi
<!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>

				<input type="submit" name="simpan" value="Search" placeholder="fakultas">
				</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['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)){
					?>
					<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>
  • kurang paham bang hehehe - Obby14
  • dibagian mana yg kurang paham? - Nandar
  • sudah bisa gan terimakasih sudah paham - Obby14

Login untuk tulis komentar



Login untuk diksusi di forum sekolah koding