Metode Searching PHP

Posted: March 18, 2013 in php
Tags:

1. Form & Function

Selanjutnya, setelah pembuatan database selesai, sekarang kita akan membuat Form sederhana untuk fungsi pencarian. kita namakan file ini sebagai “index.php”

<?php
include “config.php”;
$mode = $_REQUEST[‘mode’] ;
$id = $_REQUEST[‘id’];
$search = trim($_POST[‘search’]);
$search_array = explode(” “,$search);
$banyak_kata = (integer)count($search_array);
$searchquery = “select * from article where ” ;
for ( $x = 0; $x<= $banyak_kata; $x++) {
$searchquery .= “isi_artikel like ‘%$search_array[$x]%’”;
if ( $x <  $banyak_kata ) {
$searchquery .= ” AND “;
}
}
$runsearchquery = mysql_query($searchquery);
$viewquery = “select * from article where id = ‘$id’”;
$runviewquery = mysql_query($viewquery);
if ($mode == “search” ) {
$numrows = mysql_num_rows($runsearchquery);
print “Kata yang Anda cari adalah : “;
for ( $x = 0; $x<= $banyak_kata; $x++) {
$search_replace[$x] = “<b><FONT style=’BACKGROUND-COLOR:yellow’>$search_array[$x]</b></FONT>”;
print “$search_replace[$x] “;
}
print “<br>Kata yang Anda cari tedapat dalam <b>$numrows</b> buah Database<hr>” ;
while ( $result = mysql_fetch_array($runsearchquery)) {
$id = $result[‘id’];
$judul = $result[‘judul_artikel’] ;
$isi = $result[‘isi_artikel’];
$isi_baru = str_replace($search_array, $search_replace, $isi);
$isi_display = substr($isi_baru, 0, 800);
$isi_display = substr($isi_baru, 0, strrpos($isi_display, ” “));
print “<a href=?mode=view&id=$id>$judul</a><br>$isi_display … <br><br>”;
}
print “<hr><a href=index.php>Kembali</a>”;
}
else if($mode == “view”) {
while ( $result = mysql_fetch_array($runviewquery))
{
$id = $result[‘id’];
$judul = $result[‘judul_artikel’] ;
$penulis = $result[‘penulis_artikel’];
$isi = $result[‘isi_artikel’];
print “<b><u>$judul</u></b><br>Penulis : $penulis<br>$isi”;
}
print “<hr><a href=index.php>Kembali</a>”;
}
else {
?>
<html>
<head>
<title>Search Database … </title></head>
<body>
<br>
<table border=1 align=center bgcolor=lightgreen>
<form method=POST action=?mode=search>
<tr><td><input type=text name=search> <input type=submit name=submit value=Search></td></tr>
</form>
</body>
</html>
<?php
}
?>

2. Config

Untuk menghubungkan file PHP dengan database MySQL kita akan membuat file “config.php”

<?php
$host = “localhost”;
$username = “root”;
$password = “”;
$databasename = “search_db”;
$connect = mysql_connect($host, $username, $password) or die (“Nggak bisa konek !”);
mysql_select_db($databasename, $connect) or die(“Nggak bisa konek !”);
?>

Ini adalah tulisan tentang metode searching. Semoga tulisan ini bermanfaat..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s