
Untitled
By: a guest on
Apr 28th, 2012 | syntax:
None | size: 1.63 KB | hits: 22 | expires: Never
PHP code to correctly handle an HTML query
SELECT * FROM $tableName WHERE category RLIKE ('option-choice') and
genre RLIKE ('option-choice') and Sub_genre RLIKE ('option-choice')
and lyrical_theme RLIKE ('option-choice') and lead RLIKE
('option-choice') and tempo RLIKE ('option-choice'); "
<select id="category" name="category"><option value="" class="header">Category</option>
$query1= "SELECT * FROM $tableName
WHERE category= '$category' AND genre='$genre' AND sub_genre= '$sub_genre'
LIMIT $start, $limit";
$query1 = "SELECT * FROM $tableName WHERE [some condition thats always required]";
if (!empty ($category))
$query1 .= "AND category = '$category' ";
if (!empty ($genre))
$query1 .= "AND genre= '$genre' ";
[etc...]
$query1 .= "LIMIT $start, $limit;"
SELECT * FROM $tableName
WHERE category RLIKE 'Indie'
and genre RLIKE 'Blues'
and Sub_genre RLIKE ''
and lyrical_theme RLIKE ''
and lead RLIKE ''
and tempo RLIKE '';
<?php
$arrConditions = Array();
if (isset($_POST['category']) && !empty($_POST['category'])) {
$arrConditions[] = 'category RLIKE "' . $_POST['category'] . '"';
}
if (isset($_POST['genre']) && !empty($_POST['genre'])) {
$arrConditions[] = 'genre RLIKE "' . $_POST['genre'] . '"';
}
if (isset($_POST['sub_genre']) && !empty($_POST['sub_genre'])) {
$arrConditions[] = 'Sub_genre RLIKE "' . $_POST['sub_genre'] . '"';
}
/* ADD SIMILAR IF STATEMENTS FOR ALL OTHER FORM FIELDS */
$qryWhere = '';
if (!empty($arrConditions)) { // assuming that there is at least one condition
$qryWhere = ' WHERE ' . implode(' AND ', $arrConditions);
}
$query = 'SELECT * FROM ' . $tableName . ' ' . $qryWhere;
?>