help with php search..

Discussion in 'PHP' started by sstoney200, Feb 28, 2011.

  1. #1
    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?
     
    sstoney200, Feb 28, 2011 IP
  2. firemax

    firemax Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    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.
     
    firemax, Feb 28, 2011 IP
  3. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    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
     
    sstoney200, Feb 28, 2011 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    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:
     
    tvoodoo, Feb 28, 2011 IP
  5. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    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!
     
    sstoney200, Feb 28, 2011 IP
  6. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #6
    If you store the info that will be shown on fr1001.html in the xml then you can just match the REF.
     
    tvoodoo, Feb 28, 2011 IP
  7. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    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
     
    sstoney200, Feb 28, 2011 IP
  8. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #8
    
    <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.
     
    tvoodoo, Feb 28, 2011 IP
  9. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    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!
     
    sstoney200, Feb 28, 2011 IP
  10. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #10
    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! :)
     
    Last edited: Mar 1, 2011
    sstoney200, Mar 1, 2011 IP
  11. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #11
    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:
     
    tvoodoo, Mar 1, 2011 IP
  12. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #12

    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...'
     
    sstoney200, Mar 1, 2011 IP
  13. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #13
    replace header with : echo '<script>window.location = "http://www.for-rent-nerja.com/index_with_search.html";</script>';
     
    tvoodoo, Mar 1, 2011 IP
  14. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #14
    Thanks for your continued support tvoodoo! ;) That certainly appears to be doing something, but it's not taking me to the page.
     
    sstoney200, Mar 1, 2011 IP
  15. sstoney200

    sstoney200 Greenhorn

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #15
    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):
    ?
     
    sstoney200, Mar 1, 2011 IP