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>

avatar Obbytokun
@Obbytokun

186 Kontribusi 16 Poin

Diperbarui 5 tahun yang lalu

1 Jawaban:

Jawaban Terpilih

Coba edit lagi

<pre> &lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;title&gt;Searching&lt;/title&gt; &lt;meta charset="utf-8"&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt; &lt;link rel="stylesheet" href="bootstrap/css/bootstrap.css"&gt; &lt;script src="js/jquery.js"&gt;&lt;/script&gt; &lt;script src="bootstrap/js/bootstrap.min.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt;

&amp;lt;?php
include &#039;config.php&#039;;
?&amp;gt;
&amp;lt;div class=&quot;container&quot; style=&quot;padding-top: 20px; padding-bottom: 20px;&quot;&amp;gt;
	&amp;lt;h3&amp;gt;Form Pencarian Dengan PHP&amp;lt;/h3&amp;gt;
	&amp;lt;hr&amp;gt;


			&amp;lt;form role=&quot;form&quot; action=&quot;index.php&quot; method=&quot;get&quot;&amp;gt;
				&amp;lt;div class=&quot;form-group&quot;&amp;gt;
					&amp;lt;label&amp;gt;Cari :&amp;lt;/label&amp;gt;
					&amp;lt;input type=&quot;text&quot; name=&quot;cari&quot; placeholder=&quot;id&quot;&amp;gt;
					&amp;lt;input type=&quot;text&quot; name=&quot;cari2&quot; placeholder=&quot;nama&quot;&amp;gt;
					&amp;lt;input type=&quot;text&quot; name=&quot;cari3&quot; placeholder=&quot;fakultas&quot;&amp;gt;
				&amp;lt;/div&amp;gt;

			&amp;lt;input type=&quot;submit&quot; name=&quot;simpan&quot; value=&quot;Search&quot; placeholder=&quot;fakultas&quot;&amp;gt;
			&amp;lt;/form&amp;gt;


			&amp;lt;?php
			if(isset($_GET[&#039;cari&#039;])){
				$cari = $_GET[&#039;cari&#039;];
				echo &quot;&amp;lt;b&amp;gt;Hasil pencarian : &quot;.$cari.&quot;&amp;lt;/b&amp;gt;&quot;;
			}
			?&amp;gt;

			&amp;lt;table class=&quot;table table-striped&quot;&amp;gt;
				&amp;lt;tr&amp;gt;
					&amp;lt;th&amp;gt;ID&amp;lt;/th&amp;gt;
					&amp;lt;th&amp;gt;Nama&amp;lt;/th&amp;gt;
					&amp;lt;th&amp;gt;Fakultas&amp;lt;/th&amp;gt;
				&amp;lt;/tr&amp;gt;
				&amp;lt;?php
				if(isset($_GET[&#039;simpan&#039;])){
					$cari = $_GET[&#039;cari&#039;];
        $cari2 = $_GET[&#039;cari2&#039;];
        $cari3 = $_GET[&#039;cari3&#039;];
        $gabung = &quot;&quot;;

        if ($cari != &quot;&quot;) {
          $gabung .= &quot;AND id like &#039;%&quot;.$cari.&quot;%&#039;&quot;;
        }
        if ($cari2 != &quot;&quot;) {
          $gabung .= &quot;AND nama like &#039;%&quot;.$cari2.&quot;%&#039;&quot;;
        }
        if ($cari3 != &quot;&quot;) {
          $gabung .= &quot;AND fakultas like &#039;%&quot;.$cari3.&quot;%&#039;&quot;;
        }
        $gabung = &quot;WHERE &quot; .ltrim($gabung, &quot;AND &quot;);
					$data = mysql_query(&quot;select * from mhs $gabung&quot;);


				while($d = mysql_fetch_array($data)){
				?&amp;gt;
				&amp;lt;tr&amp;gt;
					&amp;lt;td&amp;gt;&amp;lt;?php echo $d[&#039;id&#039;]; ?&amp;gt;&amp;lt;/td&amp;gt;
					&amp;lt;td&amp;gt;&amp;lt;?php echo $d[&#039;nama&#039;]; ?&amp;gt;&amp;lt;/td&amp;gt;
					&amp;lt;td&amp;gt;&amp;lt;?php echo $d[&#039;fakultas&#039;]; ?&amp;gt;&amp;lt;/td&amp;gt;
				&amp;lt;/tr&amp;gt;
				&amp;lt;?php }
				} ?&amp;gt;
			&amp;lt;/table&amp;gt;

		&amp;lt;/div&amp;gt;
	&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&lt;/body&gt; &lt;/html&gt; </pre>

avatar Nandar
@Nandar

648 Kontribusi 204 Poin

Dipost 5 tahun yang lalu

Tanggapan

kurang paham bang hehehe

dibagian mana yg kurang paham?

sudah bisa gan terimakasih sudah paham

Login untuk ikut Jawaban