4444$ searchResult = (isset ($ _POST ['data ' ]) ? $ _POST ['data ' ] : null );
4545$ searchResult = stripslashesDeep ($ searchResult );
4646
47+ $ searchKey = array ('name ' , 'description ' );
4748$ idArray = array ();
49+ $ typeMatches = array ();
50+ $ otherMatches = array ();
51+ $ typeResults = array ();
52+ $ otherResults = array ();
53+ $ results = array ();
4854$ currentID = '' ;
4955
5056
6975 $ type = 'landmarks ' ;
7076 $ collection = $ db -> $ type ;
7177
72- // sanitize text box input
73- // $sanTerm = 'nerd';
7478 $ sanTerm = substr ($ _POST ['searchTerm ' ], 0 , 160 );
7579 $ sanTerm = strip_tags ($ sanTerm );
80+ $ sanTerm = str_replace (": " , " " , $ sanTerm );
81+ $ sanTerm = preg_replace ('/\s+/ ' , ' ' , $ sanTerm ); // remove extra spaces
7682 $ sanTermLowercase = strtolower ($ sanTerm );
7783
84+ $ termsArray = explode (' ' , $ sanTerm );
85+
86+ // REMEMBER TO ADD MULTIPLE TERM STUFF BACK IN (E.G., FROM TESTME5.PHP
7887
7988 // group search types together and prepare to query database
8089 $ query = array (
8493 )
8594 );
8695
87- // add search by landmark type, if applicable
88- if (array_search ($ sanTermLowercase , $ landmarkTypes ) !== FALSE ) {
96+ // prepend search by landmark type, if applicable
97+ $ landmarkSearch = in_array ($ sanTermLowercase , $ landmarkTypes );
98+
99+ if ($ landmarkSearch ) {
89100 array_unshift ($ query ['$or ' ], array ('type ' => $ sanTermLowercase ));
101+ array_unshift ($ searchKey , 'type ' );
90102 }
91103
92-
93- /* echo '<pre>';
94- print_r($query);
95- echo '</pre>';
96- */
97-
98104 $ cursor = $ collection -> find ($ query );
99105 $ cursor = iterator_to_array ($ cursor );
100106
101- foreach ($ cursor as $ v ) {
102- $ currentID = $ v ['_id ' ];
103- $ currentID = (string ) $ currentID ;
104- array_push ($ idArray , $ currentID );
105-
106- $ results [0 ]["$ currentID " ] = $ cursor ["$ currentID " ];
107- }
108-
109- unset ($ v ); // remove lingering foreach() values from memory
110-
111-
112- // ------ NEW SORTING ROUTINE WILL GO HERE -------- //
107+ // if applicable, separate landmark matches from others
108+ if ($ landmarkSearch ) {
109+ foreach ($ cursor as $ v ) {
110+ $ currentID = (string ) $ v ['_id ' ];
111+ // $results[0]["$currentID"] = $cursor["$currentID"];
112+
113+ if ($ v ['type ' ] == $ sanTermLowercase ) {
114+ $ countTypeMatches = countMatches ($ v , $ sanTermLowercase );
115+ $ typeResults [0 ]["$ currentID " ] = $ cursor ["$ currentID " ];
116+ $ typeMatches [] = $ countTypeMatches ;
117+ } else {
118+ $ otherResults [0 ]["$ currentID " ] = $ cursor ["$ currentID " ];
119+ $ otherMatches [] = countMatches ($ v , $ sanTermLowercase );
120+ }
121+ }
113122
123+ unset ($ v , $ val ); // remove lingering foreach() values from memory
114124
115- $ countDupes = array_count_values ($ idArray );
125+ if ($ typeMatches ) {
126+ array_multisort ($ typeMatches , SORT_NUMERIC , SORT_DESC , $ typeResults [0 ]);
127+ }
128+ if ($ otherMatches ) {
129+ array_multisort ($ otherMatches , SORT_NUMERIC , SORT_DESC , $ otherResults [0 ]);
130+ }
116131
117- /*
118- echo 'idArray: <pre>';
119- print_r($idArray);
120- echo '</pre>';
132+ $ results = array_merge ($ typeResults , $ otherResults );
121133
122- echo 'countDupes: <pre>';
123- print_r($countDupes);
124- echo '</pre>' ;
125- */
134+ } else {
135+ foreach ( $ cursor as $ v ) {
136+ $ currentID = ( string ) $ v [ ' _id ' ] ;
137+ $ results [ 0 ][ " $ currentID " ] = $ cursor [ " $ currentID " ];
126138
127- // sort results according to number of matches
128- // array_multisort($countDupes, SORT_NUMERIC, SORT_DESC, $results[0]);
139+ $ otherMatches [ 0 ][ " $ currentID " ] = countMatches ( $ v , $ sanTermLowercase );
140+ }
129141
142+ unset ($ v , $ val ); // remove lingering foreach() values from memory
143+ }
130144
131145
132146
217231 echo 'Type: ' . $v['type'] . '<br />';
218232 echo '<br />';
219233 }
220-
221- unset ($v); // remove lingering foreach() values from memory
222234*/
223235
224236// ---------------------------------------------------------------------- //
225237
238+
239+ // ADD ROUTINE HERE TO TRUNCATE AFTER MAX RESULTS = 50
240+ //
241+
226242 $ results = json_encode ($ results );
227243 print_r ($ results );
228- // var_dump($results);
229244
230245 // disconnect from server
231246 $ m -> close ();
@@ -245,3 +260,16 @@ function stripslashesDeep($value)
245260 return $ value ;
246261}
247262
263+ function countMatches ($ thisLandmark , $ term )
264+ {
265+ $ count = 0 ;
266+
267+ if (stripos ($ thisLandmark ['name ' ], $ term ) !== FALSE ) {
268+ $ count ++;
269+ }
270+ if (stripos ($ thisLandmark ['description ' ], $ term ) !== FALSE ) {
271+ $ count ++;
272+ }
273+
274+ return $ count ;
275+ }
0 commit comments