hi there,

I am working on getting a search page to cache it's results to speed up searches that have already been done. Originally, I was running into the problem of not being able to serialize/unserialize the simpleXMLElements...so I took the approach of converting the original results from the simpleXML to stdClass. Now the problem I have is that when I try to display the results from cache (they are stdClass objects), they won't display with echo/print ... but all the data is correct if I use print_r. The simpleXMLElement objects that are used the first time a search is done all display correctly.

For example:

foreach ($resultset->results as $result) {
    echo $result->title;
}

displays absolutely nothing if I am trying to use the stdClass object ($resultset is a SearchResult object, results is an array of stdClass objects), and displays the title correctly if I am using the simpleXMLElement.

Please clarify this for me...I don't see why they should act any differently...?

Oh, and print_r($result) in the above loop displays:

SearchResultYahoo Object ( [title] => stdClass Object ( [0] => title of result) [excerpt] => stdClass Object ( [0] => info about the result ) [modified] => stdClass Object ( [0] => date last modified ) [url] => stdClass Object ( [0] => the url to a site ) [size] => stdClass Object ( [0] => 167080 ) [attributes] => Array ( ) [mime_type] => stdClass Object ( [0] => application/pdf ) )

So I know that the title, url, etc are all there...

Thanks for any help!

    I suspect that stdClass does not have a toString() "magic method", whereas simpleXMLElement does. Perhaps you need to create your own class, including a toString() method in it? Or perhaps more to the point would be determining why you cannot use the serialize/unserialize the simplexml stuff in the first place?

      thanks for the response. i can't use the serialize/unserialize on the simplexml stuff because it's a php built-in object, and "It is not possible to serialize PHP built-in objects."
      Thats the reason I found anyway...whenever I tried to unserialize the simplexml after serializing it, i got a Warning that said the node no longer exists.

      It seems like a lot of work for me to write my own class just to have a __toString() method...does anyone know of anything else I could try first?

      Also, I found an example using the google search API:
      http://www.chrisdevbox.com/lab/jsonphp/google.php

      doesn't that mean they would use that to display the results?

        when I do

        var_dump($resultset->results[0]->title);

        I get:

        object(stdClass)#24 (1) { [0]=>  string(length) "some title" } 

        How should I be trying to access this string since

        echo ($resultset->results[0]->title);

        won't do it? Should there be more at the end of that command?

          What happens if you use [man]get_object_vars/man and var_dump() that? Does it give you something you can navigate successfully?

            Write a Reply...