Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

nicholas maddren
nicholas maddren
12,793 Points

How has this search been developed?

Hey guys I'm trying to work out how this list search has been developed if you take a look here:

http://screencast.com/t/1WWAmOUdaSbt

As you can see when the search is filtered the listings automatically update, I have never seen any tutorials on how to do this in PHP so I'm wondering if a bit of JS might have been implemented?

Here is the link to the site: http://www.motors.co.uk/search/car/reduced

Any idea how this might have been done? Every search I have made has always needed an update button to trigger the search.

Thanks, Nick.

4 Answers

Looks like javascript / AJAX. You might translate your php arrays into either JSON or an XML file to create this kind of functionality.

nicholas maddren
nicholas maddren
12,793 Points

I thought it didn't look like PHP, so I wonder how they are getting the data delivered to the JavaScript :s.

If they used XML or CSV you'd expect it to be really unificient however it operates quite fast.

So just so I understand do you think they are exporting their database table regularly into a XML and displaying the data that way?

I'm a little confused haha

That's one possibility, or they're using a php array, which has been converted to JSON format. At it's most basic, this is creating an array that javascript can read from.

Either create an XML file on CSV upload or convert a php array to JSON for javascript to play with. Have you seen the treehouse ajax courses?

nicholas maddren
nicholas maddren
12,793 Points

No I haven't I'll take a look now, thanks Tom now that I've got a general understanding I'll do further research :)

No worries

Tom is 100% correct. Did the same for search on one of my websites. PHP + Javascript + AJAX

Did you go with JSON or an XML file out of interest? Or do you employ both?

nicholas maddren
nicholas maddren
12,793 Points

Just been doing a bit more research on this. Can I just ask how you kept all JSON data up to date? Do you just cron the script regularly? Thanks

I think you create it for every database return

I went the JSON route. Had to filter out nasty characters from the input and search the database for matching students and store them in json format so my js script could retrieve the student names, profiles pictures and inject them into the dom.

Nice!

There was no need to keep it up to date. It was a simple search function. 1.The user types in a query 2.Javascript Ajax script sends query to php script 3.php filters special characters like "*, /, ' " 4. PHP checks if data exists in table and returns data in json format 5.Javascript ajax script injects data to DOM. Simple no need to keep up to date because the response depends on the user query. It wont be the same everytime.