المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : أخطر درس مفصل لكيفية برمجة محرك بحث متصل بقواعد البيانات يصلح لاى اسكربت



Chakira
11-01-2013, بتوقيت غرينيتش 04:31 PM
http://www.dzbatna.com/images/icons/iconrote.gif ط£ط®ط·ط± ط¯ط±ط³ ظ„ظƒظٹظپظٹط© ط¨ط±ظ…ط¬ط© ظ…ط*ط±ظƒ ط¨ط*ط« ظ…طھطµظ„ ط¨ظ‚ظˆط§ط¹ط¯ ط§ظ„ط¨ظٹط§ظ†ط§طھ ظٹطµظ„ط* ظ„ط§ظ‰ ط§ط³ظƒط±ط¨طھ (http://www.dzbatna.com/t1507597/)



http://www.advphp.com/tuts/7r/engine/1.jpg


http://i43.tinypic.com/16aeik4.jpg


يعتبر محرك البحث Search Engine من أهم المميزات تميز اى اسكربت ذو برمجة راقية , فمحرك البحث Search Engine يعتبر الأداة المثالية للوصول السريع لما يبحث عنه زائر موقعك. ولعل أهم شىء فى برمجة اى اسكربت هو سهولة الاستخدام من قبل الزائر.


فى هذا الدرس مفصل سوف نتعرف على كيفية برمجة محرك بحث داخلى يقوم بالبحث فى قواعد البيانات عن اى كلمة او جملة joomla تكتبها فى محرك البحث Search Engine الداخلى بالموقع ,ويمكنك من خلال هذا الدرس مفصل ان تقوم بعمل خاصية البحث فى اى اسكربت تقوم ببرمجته مهما كانت عدد جداوله ومهما كانت عدد حقوله , افهم فكرة الدرس مفصل واطلق العنان لطموحاتك http://www.dzbatna.com/images/smilies/icon30.gif



خطوات تطبيق الدرس مفصل:

1-انشاء قاعدة بيانات كمثال حتى يتم البحث فيها

2-انشاء ملف form.htm الخاص بعمل مستطيل البحث

3-انشاء ملف style.css الخاص بتجميل شريط البحث

4-انشاء ملف mysql.php الخاص بالاتصال ومحاكاة قاعدة البيانات وتنفيذ عملية البحث فى الجداول والحقول

5-انشاء ملف processform.php الذى سوف يقوم تنفيذ الكلمة او الجملة joomla التى تم كتابتها فى محرك البحث Search Engine ليتم البحث عنها


اولا: انشاء قاعدة البيانات

سوف نقوم بانشاء قاعدة بيانات وراح نسميها users, وبداخلها سوف ننشأ جدول اسمه users وعدد حقوله 5 حقول

-id

-firstname

-lastname

-email

-comments


http://www.advphp.com/tuts/7r/engine/1.png

تمام الحين راح ندخل بعض البيانات فى هذا الجدول حتى نستخدمها كمثال للبحث عنها

انا قمت بادخال بعض الاسماء وبياناتهم

شوف الصورة

http://www.advphp.com/tuts/7r/engine/2.png



تمام انتهينا من انشاء قاعدة البيانات .........


يوجد خطوة مهمة جدا وهى اننا نريد ان نقوم بعمل Index من النوع fulltext
للجدول اللى راح نبحث فيه
ولعمل ذلك فقط قم بعمل هذا الامر من خلال اختيار زر sql لتنفيذ أمر sql


رمز Code:
ALTER TABLE users ADD FULLTEXT(firstname, lastname, comments);

بحيث راح يكون الناتج كالصورة الاتية:

http://www.advphp.com/tuts/7r/engine/4.png


ثانيا: انشاء ملف form.htm

وهو عبارة عن صفحة الفورم الخاصة بمستطيل البحث الذى سوف سيكون محرك البحث Search Engine

وانا استخدمت بعض الجافا اسكربت لتعطى لمسة جمالية للفورم وجعلنا action هذا الفورم

هو تنفيذ ملف processform.php الذى سوف يشرح طريقة فيما بعد


كود code ملف form.htm






رمز Code:
<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;> <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;> <head> <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /> <title>محرك بحث</title> <link href=&quot;/style.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /> <script language=&quot;javascript&quot; type=&quot;text/javascript&quot;> window.onload=function(){ if(document.getElementById&&document. getElementsByTagName&&document.createElement){ var sfield=document.getElementsByTagName('form')[0].elements[0]; if(!sfield){return}; sfield.onfocus=function(){this.value=''}; sfield.onblur=function(){ if(!this.value){this.value='ادخل الكلمة المراد البحث عنها'}; } } } </script> </head> <body> <h1>محرك البحث Search Engine فى قاعدة البيانات</h1> <div> <form method=&quot;get&quot; action=&quot;processform.php&quot;> <input type=&quot;text&quot; name=&quot;searchterm&quot; title=&quot;ادخل الكلمة المراد البحث عنها&quot; value=&quot;ادخل الكلمة المراد البحث عنها&quot; class=&quot;searchbox&quot; /> <input type=&quot;submit&quot; name=&quot;search&quot; title=&quot;Search Now!&quot; value=&quot;بحـث&quot; /> </form> </div> </body> </html>


ثالثا: انشاء ملف style.php

سوف نقوم بتجميل الفورم الخاص بنا فى الصفحة السابقة

وهذا هو كود code الملف



رمز Code:
body{ background: #ccc; margin: 0; padding: 0; } h1{ width: 375px; padding: 10px; margin-left: auto; margin-right: auto; background: #339; font: normal 18px Arial, Helvetica, sans-serif; color: #fff; border: 1px solid #000; text-align: center; } h2{ font: bold 18px Arial, Helvetica, sans-serif; color: #339; } p{ font: normal 10pt Arial, Helvetica, sans-serif; color: #000; } a:link,a:visited{ font: normal 10pt Arial, Helvetica, sans-serif; color: #00f; text-decoration: none; } a:hover{ color: #f00; text-decoration: underline; } .maincontainer{ width: 375px; padding: 10px; margin-left: auto; margin-right: auto; background: #f0f0f0; border: 1px solid #000; } .rowcontainer{ padding: 10px; margin-bottom: 10px; background: #ccf; } .searchbox{ width: 200px; font: normal 12px Arial, Helvetica, sans-serif; color: #000; } .searchbutton{ width: 80px; font: bold 12px Arial, Helvetica, sans-serif; color: #000; }


http://www.advphp.com/tuts/7r/engine/5.png


رابعا: انشاء ملف mysql.php

طبعا هذا هو أهم ملف سوف يكون فى درس مفصلنا, والذى سوف ينفذ عملية البحث فى قاعدة البيانات عن الكلمة او الجملة joomla التى تريد البحث عنها

الملف عبارة عن كلاس تقوم بتنفيذ عدة عمليات متكاملة منها

1-الاتصال بقاعدة البيانات

2-تنفيذ عملية البحث للكلمة او الجملة joomla المراد البحث عنه


كود code ملف mysql.php



رمز Code:
<?php // define 'MySQL' class class MySQL{ private $conId; private $host; private $user; private $password; private $database; private $result; const OPTIONS=4; public function __construct($options=array()){ if(count($options)!=self::OPTIONS){ throw new Exception('Invalid number of connection parameters'); } foreach($options as $parameter=>$value){ if(!$value){ throw new Exception('Invalid parameter '.$parameter); } $this->{$parameter}=$value; } $this->connectDB(); } // connect to MySQL private function connectDB(){ if(!$this->conId=mysql_connect($this->host,$this->user,$this->password)){ throw new Exception('Error connecting to the server'); } if(!mysql_select_db($this->database,$this->conId)){ throw new Exception('Error selecting database'); } } // run query public function query($query){ if(!$this->result=mysql_query($query,$this->conId)){ throw new Exception('Error performing query '.$query); } return new Result($this,$this->result); } public function escapeString($value){ return mysql_escape_string($value); } } // define 'Result' class class Result { private $mysql; private $result; public function __construct(&$mysql,$result){ $this->mysql=&$mysql; $this->result=$result; } // fetch row public function fetchRow(){ return mysql_fetch_assoc($this->result); } // count rows public function countRows(){ if(!$rows=mysql_num_rows($this->result)){ return false; } return $rows; } // count affected rows public function countAffectedRows(){ if(!$rows=mysql_affected_rows($this->mysql->conId)){ throw new Exception('Error counting affected rows'); } return $rows; } // get ID form last-inserted row public function getInsertID(){ if(!$id=mysql_insert_id($this->mysql->conId)){ throw new Exception('Error getting ID'); } return $id; } // seek row public function seekRow($row=0){ if(!is_int($row)||$row<0){ throw new Exception('Invalid result set offset'); } if(!mysql_data_seek($this->result,$row)){ throw new Exception('Error seeking data'); } } } ?>

خامسا : انشاء ملف processform.php

وهذا الملف هو اللى راح ينفذ عملية البحث

كود code ملف processform.php

رمز Code:
<?php // include MySQL-processing classes require_once 'mysql.php'; try{ // connect to MySQL $db=new MySQL(array ('host'=>'localhost','user'=>'root','password'=>'root', 'database'=>'users')); $searchterm=$db->escapeString($_GET['searchterm']); $result=$db->query(&quot;SELECT firstname, lastname,comments FROM users WHERE MATCH(firstname,lastname,comments) AGAINST ('$searchterm')&quot;); if(!$result->countRows()){ echo '<div><h2>لاتوجد اى نتيجة,ارجع للخلف وعاود البحث مرة أخرى</h2></div>'; } else{ // display search results echo '<div><h2>Your search criteria returned '.$result->countRows().' results.</h2>'; while($row=$result->fetchRow()){ echo '<div><p><strong>First Name: </strong>'.$row['firstname'].'<p><p><strong>Last Name: </strong>'.$row['lastname'].'</p><p><strong>Comments: </strong>'.$row['comments'].'</p></div>'; } } echo '</div>'; } catch(Exception $e){ echo $e->getMessage(); exit(); } ?>

شرح طريقة الاجزاء المطلوب تعديلها بما يناسب طلبك:


1- اولا تغيير بيانات الاتصال بقاعدة البيانات من خلال هذا السطر


رمز Code:
$db=new MySQL(array ('host'=>'localhost','user'=>'root','password'=>'root', 'database'=>'users'));
2- تغيير اسم الجدول والاعمدة المراد البحث فيها , وذلك من خلال هذا السطر


رمز Code:
$result=$db->query(&quot;SELECT firstname, lastname,comments FROM users WHERE MATCH(firstname,lastname,comments) AGAINST ('$searchterm')&quot;);

وبهيك عند تنفيذ البحث عن اى كلمة ولنفرض اننا راح نبحث عن كلمة call و mazda

راح يكون الناتج مثل الصورة الاتية:

http://www.advphp.com/tuts/7r/engine/3.png


لتحميل ملفات الدرس مفصل كاملة : اضغــــط هـــــنا (http://www.advphp.com/tuts/7r/engine/search.rar)



vista-design






ألعاب الأندرويد مجانا و حصريا (http://www.apotox.info/forum)




https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-ash4/482113_236967293114455_1193518507_n.png (http://www.dzbatna.com)
©المشاركات المنشورة تعبر عن وجهة نظر صاحبها فقط، ولا تُعبّر بأي شكل من الأشكال عن وجهة نظر إدارة المنتدى (http://www.dzbatna.com)©

استعمل مربع البحث في الاسفل لمزيد من المواضيع


سريع للبحث عن مواضيع في المنتدى