PHP demonstration - sorting outside the database

The first report that this application outputs is a seriese of records returned by an SQL query. In the second report, instead of outputting the records they are saved into an associative array which is then sorted, and the records are output in sorted order. They key to the associative array is the value on which the items are going to be sorted - and we have added an extra (incrementing) value onto the end of that key to ensure that it's unique. If we didn't do that, and if we had two houses at the same price, we would loose one from our sorted report.

Here are some properties for sale

Semington - 225000 - Double fronted two story apartment
Melksham - 195000 - Attractive detached older style family home
Atworth - 237500 - Attractive four bedroom village home
Westbury - 152000 - Three bedroomed semi
Trowbridge - 205000 - Four bedroom detached in cul de sac
Melksham - 229950 - Four bedroomed semi in Aloeric school catchment
Trowbridge - 335000 - Detached family home with no onward chain
Trowbridge - 96950 - Well presented one bedroom flat
Hilperton - 279950 - Modern detached home
Melksham - 149950 - For all those non-gardeners
Trowbridge - 220000 - semidetached family home
Melksham - 127500 - Within a presigious retirement complex
Melksham - 465000 - Semidetached Spa house on town outskirts
Semington - 222000 - A pleasing detached modern residence
Hilperton - 465000 - An individual detached quality home
Westbury - 140000 - Three bedroom semidetached house
Trowbridge - 116000 - Larger than average first floor flat
Melksham - 275000 - Grade II Georgian townhouse requiring modernisation
Semington - 250000 - One bedroomed smarter home

Here they are in price order

Hilperton - 465000 - An individual detached quality home
Melksham - 465000 - Semidetached Spa house on town outskirts
Trowbridge - 335000 - Detached family home with no onward chain
Hilperton - 279950 - Modern detached home
Melksham - 275000 - Grade II Georgian townhouse requiring modernisation
Semington - 250000 - One bedroomed smarter home
Atworth - 237500 - Attractive four bedroom village home
Melksham - 229950 - Four bedroomed semi in Aloeric school catchment
Semington - 225000 - Double fronted two story apartment
Semington - 222000 - A pleasing detached modern residence
Trowbridge - 220000 - semidetached family home
Trowbridge - 205000 - Four bedroom detached in cul de sac
Melksham - 195000 - Attractive detached older style family home
Westbury - 152000 - Three bedroomed semi
Melksham - 149950 - For all those non-gardeners
Westbury - 140000 - Three bedroom semidetached house
Melksham - 127500 - Within a presigious retirement complex
Trowbridge - 116000 - Larger than average first floor flat
Trowbridge - 96950 - Well presented one bedroom flat

A couple of words of caution here: (a) You would be best advised to have the MySQL do the sorting if you can - more efficient and (b) For a clean application, I would much rather you had all the PHP at the top of the file, stored the output into string variables, and output them within a template - this is very much a sorting demo, and not meant to show best overall practise!

Training sample © 2024, WELL HOUSE CONSULTANTS LTD
This is http://www.wellho.net/demo/sort_sql.php
See source code here • More examples - same topic here
Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
Phone: +44 (0) 1225 708 225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net