|
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
Some other Articles
From Sunday to Monday, from Melksham to the BalkansWhat makes our courses special?Santa on the train to MelkshamMore PHP sample and demonstration programsEasy selection of multiple SQL conditions from PHPUpgrade Mac OSX to Leopard, Web Server Apache httpd config lostQuestion on division (Java) - Also Perl, PHP, Python ...Refa Tandoori Indian Restaurant, MelkshamAn answer to a student asking 'Help'Looking after you Christmas Customer Crowds
|
2259 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, 38, 39, 40, 41, 42, 43, 44, 45, 46 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).
|
|