Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

nicholas maddren
nicholas maddren
12,793 Points

What would be the best method to list items in database?

Hey guys, I'm just wondering what would be the best method for my search and listing tool, before I get started I want to know I am using the right method.

The search tool needs to be able to list items and be able to filter results, there are many filters such as 'Price' and 'Make'. There are about 120 vehicles that I will be listing.

First method:

Directly display results using PHP just like Randy's project.

Second method:

Use PHP to display MySQL data in JSON and then use JavaScript to filter results and search listings.

What would you say is the best method? What are the positives and the negatives?

Thanks, Nick.

3 Answers

Robert Walker
Robert Walker
17,146 Points

I am relatively new to JavaScript and newish to databases but I would think using the database would be better as you can simply request the data you want and then display it how ever you see fit.

Displaying it in JavaScript through JSON seems to just add extra steps to get the same result as making direct search query to the database, as I say though I am new to JavaScript and not that knowledgeable with databases.

nicholas maddren
nicholas maddren
12,793 Points

I've just been checking and all the big listing sites need JavaScript to use their search functions which makes me think that everyone else must be doing this method even Ebay's uses JavaScript so I'm a little confused on what method to take.

Kevin Korte
Kevin Korte
28,148 Points

I think with that many vehicles to list, you're going to be much better off having the data in a database.

You don't have to use Javascript. You could have your seach and filter items cause a refresh of the page and the return the new data from the database and parse it with PHP into some form of template.

The javascript would create a better user experience if done correctly, as you could make AJAX calls to return new data without making the whole page refresh.

nicholas maddren
nicholas maddren
12,793 Points

Thanks for giving me a bit of insight Kevin, if you take a look at this site: http://www.motors.co.uk/search/car/

I'm pretty sure they are using Ajax for their search, which is why I am so confused as that company lists thousands of cars never mind hundreds.

Any idea how they might have done this? It runs so efficiently however I wonder how the data is being displayed, if it was JSON you'd expect it to run slowly :s

Each search request via AJAX or PHP should query the database and only return the requested information and store this information in a a php variable or javascript object. It would be best to use these variables only to temporarily store the output from the database, not as a database solution. Sorting and limiting by array on a massive scale is likely to have negative performance impacts, as you mentioned.

These websites will use AJAX to request information asynchronously (key) of the page load but will also have extensive php back-ups for situations where javascript is disabled by the user. Inclusion of JavaScript should be progressive i.e. without it, the site should retain all functionality.

If you have already written a php solution, creating an AJAX / Javascript layer over the top should be relatively simple.