Algoritma Boyer More Dengan PHP MYSQL

Sekarang Mungkin Post Postan nya sedikit beda. hehe. karna ane lagi ngalamin kasus tersebut skrng..

dan sekalian ane share share siapa tau ada yang berminat hehehe.. oke lansung saja..

> database :

database

> Code

form.php


<h3>Pasal/Perkara</h3>
Ini adalah pasal/perkara.<br> <hr>
<form method='post' action='cari_pasalperkara.php'>
<table border='0' cellpadding='4' cellspacing='0'>
<tr><td colspan=2><b>Cari Pasal / Perkara<b></td><tr>
<tr><td>Pasal</td> <td> <input type='search' name='cari' placeholder='Masukkan Pasal/Perkara' size=35> </td></tr>
<tr><td></td><td><input type='submit' value='Cari Pasal'></td></tr>
</table>
</form>

cari_pasalperkara.php


<?php
include"admin/koneksi.php";
echo"
<h3>Pasal/Perkara:</h3><br>
";

$pattern = $_POST['cari'];
$sqlQuery=mysql_query("select * from pasal where isi LIKE '%$pattern%'");
$ketemu = false;
$text[] = array();
$arrayOutput[] = array();
$x = 0;
$y = 0;
while($data=mysql_fetch_array($sqlQuery)){
$text[$x] = $data;
$x++;
}

$jumlah = count($text);
echo "Jumlah = ",$jumlah;
for($p=0; $p<$jumlah; $p++){
$hasil = BoyerMoore($text[$p]['isi'], $pattern);
if($hasil != -1){
$ketemu = true;
$arrayOutput[$y] = $text[$p];
$y++;
}
}

if($ketemu == true){
echo "<br><br>KETEMU";
$jumlahKetemu = count($arrayOutput);
echo "<br>Jumlah Ketemu = ",$jumlahKetemu;
echo "<br><br>HASIL:";
for($q=0; $q<$jumlahKetemu;$q++){
echo "<br>Nama Pasal = ",$arrayOutput[$q]['nama_pasal'];
echo "<br>Ayat = ",$arrayOutput[$q]['ayat'];
echo "<br>Isi = ",$arrayOutput[$q]['isi'];
}
}
else {
echo "<br><br>Tidak Ditemukan";
}
/**
* Returns the index of the first occurrence of the
* specified substring. If it's not found return -1.
*
* @param text The string to be scanned
* @param pattern The target string to search
* @return The start index of the substring
*/
function BoyerMoore($text, $pattern) {
$patlen = strlen($pattern);
$textlen = strlen($text);
$table = makeCharTable($pattern);
for ($i=$patlen-1; $i < $textlen;) {
$t = $i;
for ($j=$patlen-1; $pattern[$j]==$text[$i]; $j--, $i--) {
if($j == 0) return $i;
}
$i = $t;
if(array_key_exists($text[$i], $table))
$i = $i + max($table[$text[$i]], 1);
else
$i += $patlen;
}
return -1;
}
function makeCharTable($string) {
$len = strlen($string);
$table = array();
for ($k=0; $k < $len; $k++) {
$table[$string[$k]] = $len - $k - 1;
}
return $table;
}


echo"
<p><a href='javascript:history.go(-1)'> <input type='button' value='Kembali'> </a>
";
?>

>Hasilnya

hasil

Leave a comment

3 Comments

  1. wih keren da, dah maen algoritma segala… ada refrensi teori yang lu pake kagak da?

    Reply

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

%d bloggers like this: