Pages

Friday, 27 July 2012

Simple Search Engine With PHP


ကြ်န္ေတာ္တို ့ PHP ကိုအသံုးျပဳျပီးေတာ့ ကိုယ္ပိုင္၀က္ဆိုက္ သို ့မဟုတ္  ပေရာဂ်က္တစ္ခုခု အတြက္ search engine ေလးတစ္ခု ဖန္တီးၾကည့္ရေအာင္….အရင္ဦးဆံုး ကြ်န္ေတာ္တို ့ HTMLနဲ ့အေျခခံ Search Form ေလးတစ္ခု ဖန္တီးပါမရ္…


<html>
<head>
<title>Simple Search Engine by OpenEyes</title>
</head>
<body>
<center>
Enter the first, last, or middle name of the person you are looking for:
<form action=”search.php” method=”post”>
<input type=”text” name=”search_query” maxlength=”25″ size=”15″><br>
<input type=”reset” name=”reset” value=”Reset”>
<input type=”text” name=”submit” value=”Submit”>
</form>
</center>
</body>
</html>

Database နဲ ့ခ်ိတ္ဆက္ဖို ့အတြက္  PHP ထပ္ျဖည့္ပါမရ္….


<?php
mysql_pconnect(“host”, ”username”, ”password”) or die(“Can’t connect!”);
mysql_select_db(“Names”) or die(“Can’t select database!”);
if (!eregi(“[[:alpha:]]”, $search_query))
{
echo ”Error: you have entered an invalid query, you can only use characters!<br>”;
exit;
}

အဲဒီေနာက္  Search Quary အတြက္  My SQl ကိုအသံုးျပဳပါမရ္….

$query= mysql_query(“SELECT * FROM some_table WHERE First_Name= ’$search_query’
OR Middle_Name= ’$search_query’ OR Last_Name= ’$search_query’ ORDER BY Last_Name”);

ဒီေနရာမွာ ကြ်န္ေတာ္ အနည္းငယ္ရွင္းျပလိုပါတရ္….အခုလို  My SQL နဲ ့ရွာလိုက္တဲ့အခါ  Database ရဲ ့  Row တိုင္းမွာရွိတဲ့ First_Name, Middle_Name, and Last_Name   ေတြက်လာပါတရ္….ဒါေပမရ္ ့တိက်မႈမရွိတာကိုေတြ ့ရပါလိမ့္မရ္…အဲဒါေၾကာင့္  ကြ်န္ေတာ္တို ့  mysql_fetch_array( ) ကိုအသံုးျပဳျပီး ထပ္ရွာပါမရ္….

$result= mysql_num_rows($query);
if ($result == 0)
{
echo ”Sorry, I couldn’t find any user that matches your query ($search_query)”;
exit;
}
else if ($result == 1)
{
echo ”I’ve found <b>1</b> match!<br>”;
}
else {
echo ”I’ve found <b>$result</b> matches! <br>”;
}
while ($row= mysql_fetch_array($query))
{
$first_name= $row["First_Name"];
$middle_name = $row["Middle_Name"];
$last_name = $row["Last_Name"];
echo ”The first name of the user is: $first_name.<br>”;
echo ”The middle name of the user is: $middle_name.<br>”;
echo ”The last name of the user is: $last_name.<br>”;
}
?>

အဲဒီေနာက္ ကြ်န္ေတာ္  Statement တစ္ေၾကာင္းထပ္ထည့္ပါမရ္…အဲဒါကေတာ့ ကြ်န္ေတာ္  ရွာေဖြလိုက္တဲ့အခါရရွိတဲ့ Result အေရအတြက္ အတိအက်ကိုပါ…

{
echo ”I’ve found <b>1</b> match!<br>”;
}
else {
echo ”I’ve found <b>$result</b> matches!<br>”;
}

ကဲ နိဂံုးခ်ဳပ္ကို ၾကည့္ၾကည့္ရေအာင္….

<html>
<head>
<title>Simple Search Engine version by OpenEyes</title>
</head>
<body>
<?php
mysql_pconnect(“host”, “username”, “password”) or die(“Can’t connect!”);
mysql_select_db(“Names”) or die(“Can’t select database!”);
if (!eregi(“[[:alpha:]]”, $search_query))
{
echo “Error: you have entered an invalid query, you can only use characters!<br>”;
exit; //No need to execute the rest of the script.
}
$query= mysql_query(“SELECT * FROM some_table WHERE First_Name=’$search_query’
OR Middle_Name= ‘$search_query’ OR Last_Name=’$search_query’ ORDER BY Last_Name”);
$result= mysql_numrows($query);
if ($result == 0)
{
echo “Sorry, I couldn’t find any user that matches your query ($search_query)”;
exit; //No results found, why bother executing the rest of the script?
}
else if ($result == 1)
{
echo “I’ve found <b>1</b> match!<br>”;
}
else {
echo “I’ve found <b>$result</b> matches!<br>”;
}
while ($row= mysql_fetch_array($query))
{
$first_name= $row["First_Name"];
$middle_name = $row["Middle_Name"];
$last_name = $row["Last_Name"];
echo “The first name of the user is: $first_name.<br>”;
echo “The middle name of the user is: $middle_name.<br>”;
echo “The last name of the user is: $last_name. <br>”;
}
?>
</body>
</html>

အာလံုးပဲ အဆင္ေျပႏိုင္ၾကပါေစ….

Share-က်ေနာ္ စုေဆာင္သိမ္းဆည္ထာတဲ့ နည္းပညာမွတ္စု မွျပန္ျပီးမွ်ေ၀ေပးလိုက္ပါတယ္..

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

အေထြးေထြးနည္းပညာမ်ား