943,671 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1391
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 5th, 2009
1

Re: searching and comparing from several tables

Try the following code:
php Syntax (Toggle Plain Text)
  1. <?
  2. mysql_connect('localhost','root','');
  3. mysql_select_db('mydatabase');
  4. if (isset($_POST) && !empty($_POST)) {
  5. function generate($sql) {
  6. $dat='';
  7. $var=mysql_query($sql);
  8. while ($data=mysql_fetch_assoc($var)) {
  9. $dat.='<tr><td>'.$data['lessonID'].'</td><td>'.$data['subject'].'</td><td>'.$data['learningArea'].'</td><td>'.$data['ability'].'</td><td>'.$data['form'].'</td><td>'.$data['skills'].'</td><td>'.$data['time'].'</td></tr>';
  10. }
  11. return $dat;
  12. }
  13. $table=0;
  14. $string='';
  15. if ($_POST['weight']['subject']>0 && !empty($_POST['subject'])) {
  16. $sql='SELECT * FROM `lessonplan` WHERE `subject` LIKE "%'.mysql_real_escape_string($_POST['subject']).'%"';
  17. $string.='`subject` NOT LIKE "%'.mysql_real_escape_string($_POST['subject']).'%" AND ';
  18. $sqlarray[$_POST['weight']['subject']][]=$sql;
  19. }
  20. if ($_POST['weight']['learningarea']>0 && !empty($_POST['learningarea'])) {
  21. $sql='SELECT * FROM `lessonplan` WHERE '.$string.'`learningarea` LIKE "%'.mysql_real_escape_string($_POST['learningarea']).'%"';
  22. $string.='`learningarea` LIKE "%'.mysql_real_escape_string($_POST['learningarea']).'%" AND ';
  23. $sqlarray[$_POST['weight']['learningarea']][]=$sql;
  24. }
  25. if ($_POST['weight']['ability']>0 && !empty($_POST['ability'])) {
  26. $sql='SELECT * FROM `lessonplan` WHERE '.$string.'`ability`="'.mysql_real_escape_string($_POST['ability']).'"';
  27. $string.='`ability`!="'.mysql_real_escape_string($_POST['ability']).'" AND ';
  28. $sqlarray[$_POST['weight']['ability']][]=$sql;
  29. }
  30. if ($_POST['weight']['skills']>0 && !empty($_POST['skills'])) {
  31. $sql='SELECT * FROM `lessonplan` WHERE '.$string.'`skills` LIKE "%'.mysql_real_escape_string($_POST['skills']).'%"';
  32. $string.='`skills` NOT LIKE "%'.mysql_real_escape_string($_POST['skills']).'%" AND ';
  33. $sqlarray[$_POST['weight']['skills']][]=$sql;
  34. }
  35. if ($_POST['weight']['time']>0 && !empty($_POST['time'])) {
  36. $sql='SELECT * FROM `lessonplan` WHERE '.$string.'`time` LIKE "%'.mysql_real_escape_string($_POST['time']).'%"';
  37. $sqlarray[$_POST['weight']['time']][]=$sql;
  38. }
  39. if ($table==1) {
  40. echo '</table>';
  41. }
  42. if (!empty($sqlarray) && isset($sqlarray)) {
  43. echo '<table border=1 cellpadding=4 cellspacing=0><tr bgcolor="#CCCCCC"><td>ID</td><td>Subject</td><td>Learning Area</td><td>Ability</td><td>Form</td><td>Skills</td><td>Time</td></tr>';
  44. krsort($sqlarray);
  45. foreach ($sqlarray AS $array) {
  46. foreach ($array AS $sql) {
  47. echo generate($sql);
  48. }
  49. }
  50. echo '</table><p>';
  51. }
  52. }
  53. ?>
  54. <style type="text/css">
  55. .class {
  56. border:0px;
  57. }
  58. </style>
  59. <form method="post">
  60. <table border=1 cellpadding=0 cellspacing=0>
  61. <tr>
  62. <td colspan=4 bgcolor="#CCCCCC">Enter Keywords here:</td>
  63. </tr><tr>
  64. <td>Subject:</td>
  65. <td><input class="class" type="text" name="subject" value="<? echo htmlentities($_POST['subject'], ENT_QUOTES); ?>"></td>
  66. <td><select name="weight[subject]" style="width:100%">
  67. <option value="9">9
  68. <option value="8">8
  69. <option value="7">7
  70. <option value="6">6
  71. <option value="5">5
  72. <option value="4">4
  73. <option value="3">3
  74. <option value="2">2
  75. <option value="1">1
  76. <option value="0">0
  77. </select></td>
  78. </tr><tr>
  79. <td>Learning Area:</td>
  80. <td><input class="class" type="text" name="learningarea" value="<? echo htmlentities($_POST['learningarea'], ENT_QUOTES); ?>"></td>
  81. <td><select name="weight[learningarea]" style="width:100%">
  82. <option value="9">9
  83. <option value="8">8
  84. <option value="7">7
  85. <option value="6">6
  86. <option value="5">5
  87. <option value="4">4
  88. <option value="3">3
  89. <option value="2">2
  90. <option value="1">1
  91. <option value="0">0
  92. </select></td>
  93. </tr><tr>
  94. <td>Ability:</td>
  95. <td><select name="ability" style="width:100%">
  96. <option value="excellent">Excellent
  97. <option value="good">Good
  98. <option value="poor">Poor
  99. </select></td>
  100. <td><select name="weight[ability]" style="width:100%">
  101. <option value="9">9
  102. <option value="8">8
  103. <option value="7">7
  104. <option value="6">6
  105. <option value="5">5
  106. <option value="4">4
  107. <option value="3">3
  108. <option value="2">2
  109. <option value="1">1
  110. <option value="0">0
  111. </select></td>
  112. </tr><tr>
  113. <td>Skills:</td>
  114. <td><input class="class" type="text" name="skills" value="<? echo htmlentities($_POST['skills'], ENT_QUOTES); ?>"></td>
  115. <td><select name="weight[skills]" style="width:100%">
  116. <option value="9">9
  117. <option value="8">8
  118. <option value="7">7
  119. <option value="6">6
  120. <option value="5">5
  121. <option value="4">4
  122. <option value="3">3
  123. <option value="2">2
  124. <option value="1">1
  125. <option value="0">0
  126. </select></td>
  127. </tr><tr>
  128. <td>Time (minutes):</td>
  129. <td><input class="class" type="text" name="time" value="<? echo htmlentities($_POST['time'], ENT_QUOTES); ?>"></td>
  130. <td><select name="weight[time]" style="width:100%">
  131. <option value="9">9
  132. <option value="8">8
  133. <option value="7">7
  134. <option value="6">6
  135. <option value="5">5
  136. <option value="4">4
  137. <option value="3">3
  138. <option value="2">2
  139. <option value="1">1
  140. <option value="0">0
  141. </select></td>
  142. </tr><tr bgcolor="#CCFFCC">
  143. <td colspan=2>Search Now:</td>
  144. <td bgcolor="#FFFFFF"><input type="submit" value="Search"></td>
  145. </tr></table></form>
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 5th, 2009
0

Re: searching and comparing from several tables

thanks a lot.
I am trying to run it.
Reputation Points: 10
Solved Threads: 0
Junior Poster
tulipputih is offline Offline
107 posts
since May 2009
Sep 5th, 2009
0

Re: searching and comparing from several tables

Hi,
I need to refer to the lookup table for each keyword inserted by user
to compare query and record in database and find the similarity.
this similarity is then times by the weight chose by user.
How can we refer to this lookup table ?
(separate lookup table for each field)

thank you.
Reputation Points: 10
Solved Threads: 0
Junior Poster
tulipputih is offline Offline
107 posts
since May 2009
Sep 5th, 2009
0

Re: searching and comparing from several tables

Could you please explain in more detail what each column does. What I so far get is that there is one table per keyword box and the simularity field is times by the keyword strength and the id field is the linker between the lookup table and the lessonplan table. What I dont get is what are the fields 'query' and 'case' used for?
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 6th, 2009
0

Re: searching and comparing from several tables

ok..thanks..
Attached herein is the interface of the search page.
the similarity should be reffered to other tables
(similarity table)
Attached Files
File Type: doc search.doc (81.5 KB, 12 views)
Reputation Points: 10
Solved Threads: 0
Junior Poster
tulipputih is offline Offline
107 posts
since May 2009
Sep 6th, 2009
0

Re: searching and comparing from several tables

Quote ...
What I dont get is what are the fields 'query' and 'case' used for?
query is the keyword that user key-in
cases are records in database..
query and cases are compared..the similarity is taken from the llokup table..
& overall similarity is calculated.

many thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster
tulipputih is offline Offline
107 posts
since May 2009
Sep 6th, 2009
0

Re: searching and comparing from several tables

I just check the picture you sent and it has 12 searchable fields where as the mysql database has only 10 fields. How is that suppose to work? Also what might make this a lot easier since how you have a picture is if you write ontop of each box in the picture what mysql column the input box is meant to refer to because this is becomming a mind warp.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 6th, 2009
0

Re: searching and comparing from several tables

Quote ...
it has 12 searchable fields where as the mysql database has only 10 fields. How is that suppose to work
yup..that is just a sample interface..it will be editted later according to the databse structure

The main problem is I do not know how to refer to several lookup tables. How can I do this ?
many thanks.
Reputation Points: 10
Solved Threads: 0
Junior Poster
tulipputih is offline Offline
107 posts
since May 2009
Sep 6th, 2009
0

Re: searching and comparing from several tables

Result of the search will be displayed in rank.
The most similar record to the query will be at the top of the result list..followed by the 2nd most similar and so on.
this similarity is calculated by referring to the weight and similar match of the keyword (query) and records in database.

Your code is good, but I need a suggestion on how to integrate the code to the lookup tables (which contain similarity)
thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster
tulipputih is offline Offline
107 posts
since May 2009
Sep 7th, 2009
0

Re: searching and comparing from several tables

Click to Expand / Collapse  Quote originally posted by tulipputih ...
Your code is good, but I need a suggestion on how to integrate the code to the lookup tables (which contain similarity)
thanks
Well I'm not sure if I can do much more on this topic but all the hard code I have posted in post #11. And as for how to do it. It should be just as simple as adding a bunch of lines of code into my script (post #11). But what those lines should be I'm not sure because you haven't go an easy database structure. But I noticed that I seem to be the only one replying to this topic. I wonder why? Perhaps somebody else could continue on with this topic as I am finding it difficult to understand the database structure. Good luck though.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Fix the options in Cakephp Form Field of type radio button
Next Thread in PHP Forum Timeline: How to Update database when the Browser is closed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC