Hi guys, I've attempted to implement a search facility on a site using some php! It's my first crack at php and I've got so far (with some help from various sources) but it's still bamboozling me! My search is producing some results but the results are being displayed below errors like the following: 'Warning: Illegal offset type in /hermes/bosweb/web210/b2109/d5.sstoney200/public_html/Rentals/search_test/search.php on line 26', s and 'Warning: Illegal offset type in /hermes/bosweb/web210/b2109/d5.sstoney200/public_html/Rentals/search_test/search.php on line 26' my search test is located here: http://www.for-rent-nerja.com/search_test/index_with_search.html and below is my php running the search: <?php $xml = simplexml_load_file('property_catalog.xml'); $results = array(); foreach($xml->PROPERTY as $property) { foreach($property->children() as $child_val) { // Here, we loop through all the posted data. It could use some sanitation, really. foreach($_POST as $search_criteria) { // Here we check if the property has any attributes that seems to match the query if(strtolower($child_val) == strtolower($search_criteria)) { $results[] = $property; // Add matches to our results $relevancy_counter = array(); // This goes at the top, below $results = array(); if(strtolower($child_val) == strtolower($search_criteria)) { if(array_key_exists($property->REF, $results)) { $relevancy_counter[$property->REF]++; // If property already has been matched, increase relevancy } else { $results[$property->REF] = $property; // Add matched property to our results $relevancy_counter[$property->REF] = 1; // Set our first match } } } } } } echo '<h1>Results</h1>'; foreach($results as $property) { echo 'Our ', $property->BEDROOMS, $property->DURATION, $property->TYPE, $property->AREA, $property->REGION, ' seems to match your query. View Property<br>'; } ?> PHP: As I said I'm pretty useless with PHP, anybody able to see my error?
You cannot use arrays or objects as keys. Doing so will result in a warning: Illegal offset type. Try casting $property->REF to a string or integer first. ex: $test = (int) $property->REF; if(array_key_exists($test, $results)) { ........................... This (hack) should fix your illegal offset type.
Hey firemax, thanks for you reply! It's reduced the errors from 4 to 1 but still outputting 1 error: 'Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in /hermes/bosweb/web210/b2109/d5.sstoney200/public_html/Rentals/search_test/search.php on line 12 Warning: Invalid argument supplied for foreach() in /hermes/bosweb/web210/b2109/d5.sstoney200/public_html/Rentals/search_test/search.php on line 16 Results' It also does not appear to be listing any results now... hmm
This should do it : $xml = simplexml_load_file('property_catalog.xml'); $results = array(); $query = '/CATALOG/PROPERTY'; if(isset($_POST['REF']) && strlen($_POST['REF']) > 0) $query .= '[REF="'.$_POST['REF'].'"]'; if(isset($_POST['DURATION']) && strlen($_POST['DURATION']) > 0) $query .= '[DURATION="'.$_POST['DURATION'].'"]'; if(isset($_POST['TYPE']) && strlen($_POST['TYPE']) > 0) $query .= '[TYPE="'.$_POST['TYPE'].'"]'; if(isset($_POST['AREA']) && strlen($_POST['AREA']) > 0) $query .= '[AREA="'.$_POST['AREA'].'"]'; if(isset($_POST['BEDROOMS']) && strlen($_POST['BEDROOMS']) > 0) $query .= '[BEDROOMS="'.$_POST['BEDROOMS'].'"]'; $results = $xml->xpath($query); echo '<h1>Results ('.((is_array($results) && count($results) > 0) ? count($results) : 0).')</h1>'; if(is_array($results) && count($results) > 0) { foreach($results as $property) { echo 'Our '. $property->BEDROOMS .' '.$property->DURATION.' '.$property->TYPE.' '.$property->AREA.' '.$property->REGION.' seems to match your query. View Property<br />'; } } PHP:
MY GOSH! TVOODOO, YOU ARE AN ABSOLUTE LEGEND! That is working (almost) precisely as I wanted! WOOP! The only 2 things to note are 1) if I input a property ref is there anyway I can discard the fields below to return that property? For instance if someone inputs fr1001 as per my xml prop: <PROPERTY> <REF>fr1001</REF> <TYPE>Apartment</TYPE> <DURATION>Holiday</DURATION> <AREA>Nerja</AREA> <BEDROOMS>1 Bedroom</BEDROOMS> <REGION>(Torrecilla)</REGION> </PROPERTY> Code (markup): I must also match all the other fields within the xml to show that prop which is not ideal, since an external user may not know the exact data for that property. and the final part 2) on the results page where it says: Results (1) Our 1 Bedroom Holiday Apartment Nerja (Torrecilla) seems to match your query. View Property on the 'view property' I want to link to that property page so for fr1001 that would be: http://www.for-rent-nerja.com/holiday_rentals/fr1001.html What's the best way to go about that? Someone suggested a permalink in my xml? Would you agree? Thank you so so much for your help!
Sorry if I sound dumb but I've re-read this about 10 times but can't get the jist of what you mean! lol
<PROPERTY> <REF>fr1001</REF> <TYPE>Apartment</TYPE> <DURATION>Holiday</DURATION> <AREA>Nerja</AREA> <BEDROOMS>1 Bedroom</BEDROOMS> <REGION>(Torrecilla)</REGION> <INFO> <PRICE>125</PRICE> <IMAGE1>link...</IMAGE1> . . . </INFO> </PROPERTY> HTML: Assuming that in the page fr1001.html you will be showing the INFO about that property. Like further informations about it.
Oh i see what you mean! I literally don't need it to be any more complicated than what you provided earlier. If you go to http://www.for-rent-nerja.com/index_with_search.html on the left hand side you will see that form (albeit very messy as I haven't had time to line it up yet) If you click submit as it is, it's producing the results page now incorporated onto a page on my site, and all I need to finish it of is to link from the 'view property' text!
I've managed to get it working as I want now from http://www.for-rent-nerja.com/index_with_search.html If you click submit on the property search as is it's producing 5 results that then link to the main property pages! Happy days! The only thing that isn't working is if someone enters 'fr1001' into the 'property ref' field, I want to take them straight to that property page if possible? Is that do-able? Despite that, thus far, I'm a very happy chappie! Thanks for help guys!
Replace : if(isset($_POST['REF']) && strlen($_POST['REF']) > 0) $query .= '[REF="'.$_POST['REF'].'"]'; PHP: with : if(isset($_POST['REF']) && strlen($_POST['REF']) > 0) { header("Location:http://www.for-rent-nerja.com/index_with_search.html"); } PHP:
If I input fr1001 into the prop ref number and search it is now chucking out this error: 'Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web210/b2109/d5.sstoney200/public_html/Rentals/search.php:7) in /hermes/bosweb/web210/b2109/d5.sstoney200/public_html/Rentals/search.php on line 187 Results (5) Our 1 Bedroom...'
replace header with : echo '<script>window.location = "http://www.for-rent-nerja.com/index_with_search.html";</script>';
Thanks for your continued support tvoodoo! That certainly appears to be doing something, but it's not taking me to the page.
Is the problem anything to do with the way I am linking to the page in the xml: <REF><![CDATA[<a class="t_color1 big" href="http://www.for-rent-nerja.com/holiday_rentals/fr1001.html", target="_blank">FR1001</a>]]></REF> Code (markup): ?