Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Easy selection of multiple SQL conditions from PHP

Are you automating an SQL query in a web page, wanting to select records to display only where they match a whole series of criteria from what could be a long list of options - for example looking for houses:

* Only in Melksham
* Less that 200k
* With a large garden

and other criteria that perhaps are required by other users might include:

* Near a good school
* Near public transport
* Ground floor
* Parking

If all that data is in your database - good, you can do it. But how do you build up the complex query? One useful trick is to have an array of possible queries, then push all the ones that you actually want into a further array and link them together with an implode putting an " and" between each element ..

$selectors = array(
  "cheap" => "asking < 200000",
  "local" => 'locate = "Melksham"',
  "bathphone" => 'phone like "01225%"');
$cwant = array();
foreach (array_keys($selectors) as $cdi) {
  if ($_GET[$cdi]) {
    array_push($cwant, $selectors[$cdi]);
  }
}
$restrict = implode (" and ",$cwant);


Demo here
Full source code here
(written 2007-11-30 08:41:36)

 
Associated topics are indexed under
S156 - Interfacing Applications to MySQL Databases

Back to
Upgrade Mac OSX to Leopard, Web Server Apache httpd config lost
Previous and next
or
Horse's mouth home
Forward to
More PHP sample and demonstration programs
Some other Articles
From Sunday to Monday, from Melksham to the Balkans
What makes our courses special?
Santa on the train to Melksham
More PHP sample and demonstration programs
Easy selection of multiple SQL conditions from PHP
Upgrade Mac OSX to Leopard, Web Server Apache httpd config lost
Question on division (Java) - Also Perl, PHP, Python ...
Refa Tandoori Indian Restaurant, Melksham
An answer to a student asking 'Help'
Looking after you Christmas Customer Crowds
1829 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho