Last active
December 15, 2015 18:59
-
-
Save atomize/5307459 to your computer and use it in GitHub Desktop.
Build MySQL query from checkboxes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include("MYSQL2JSON.class.php"); | |
//MySQL connect | |
$c = mysql_connect("useawsfordbduuuh.c345tgxpm1uqz.us-east-1.rds.amazonaws.com", "login", "password!"); | |
mysql_select_db("database"); | |
$json = new MYSQL2JSON(); //Create an object | |
$form = '<FORM | |
<form action="index.php" method="post"> | |
<label><input type="checkbox" name="col[]" value="Asphalt">Asphalt</label><br> | |
<label><input type="checkbox" name="col[]" value="Banana">Banana</label><br> | |
<label><input type="checkbox" name="col[]" value="Black">Black</label><br> | |
<label><input type="checkbox" name="col[]" value="Canary">Canary</label><br> | |
<label><input type="checkbox" name="col[]" value="Light Blue">Light Blue</label><br> | |
<label><input type="checkbox" name="col[]" value="Light Yellow">Light Yellow</label><br> | |
<label><input type="checkbox" name="col[]" value="Natural">Natural</label><br> | |
<label><input type="checkbox" name="col[]" value="Navy">Navy</label><br> | |
<label><input type="checkbox" name="col[]" value="Orange">Orange</label><br> | |
<label><input type="checkbox" name="col[]" value="Pink">Pink</label><br> | |
<label><input type="checkbox" name="col[]" value="Pool">Pool</label><br> | |
<label><input type="checkbox" name="col[]" value="Red">Red</label><br> | |
<label><input type="checkbox" name="col[]" value="Royal Blue">Royal Blue</label><br> | |
<label><input type="checkbox" name="col[]" value="Soft Blue">Soft Blue</label><br> | |
<label><input type="checkbox" name="col[]" value="Soft Yellow">Soft Yellow</label><br> | |
<label><input type="checkbox" name="col[]" value="Stone">Stone</label><br> | |
<label><input type="checkbox" name="col[]" value="Sun">Sun</label><br> | |
<label><input type="checkbox" name="col[]" value="White">White</label><br> | |
<label><input type="checkbox" name="col[]" value="Yellow">Yellow</label><br> | |
<input type="submit" name="submit" value="Submit"> | |
</form> | |
<FORM'; | |
if (is_array($_POST['col'])) { | |
echo $form; | |
$sql = "SELECT * FROM SomeTable WHERE Colors="; | |
foreach($_POST['col'] as $value) { | |
$sql .= "'{$value}' OR "; | |
} | |
$sql = substr($sql, 0, -3); | |
$sql .= " ORDER BY DesignId DESC"; | |
echo "sql= " . $sql . "<br /><br />\n"; | |
$jsonResults = $json->query($sql); | |
print $jsonResults; | |
// foreach ($jsonResults['results'] as $result) { | |
// echo $result['SomeColumnOtherThanColors'] . "<br>"; | |
//} | |
} else { | |
echo "No column was selected<br /><br />\n"; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MYSQL2JSON{ | |
var $num_fields; | |
var $root_string; | |
function MYSQL2JSON(){ | |
$this->root_string = 'result'; | |
} | |
function query($sql){ | |
$result_resource = mysql_query(trim($sql)); | |
if(!$result_resource) die('MYSQL2JSON - Query Error: '.mysql_error()); | |
$arTableFields = $this->findAllTableFields($result_resource); | |
$arAllRows = array(); | |
while($row = mysql_fetch_array($result_resource)){ | |
$arOneRow = array(); | |
for($j = 0; $j < $this->num_fields; $j++){ | |
$data = $this->encode_msg($row[$j]); | |
$arOneRow[] = '"'.$arTableFields[$j].'"'.':'.'"'.$data.'"'; | |
} | |
$arAllRows[] = '{'.implode(',', $arOneRow).'}'; | |
} | |
if(count($arAllRows) == 1){ | |
return '{"'.$this->root_string.'": ' . $arAllRows[0] . '}'; | |
}else{ | |
return '{"'.$this->root_string.'": ' . '['.implode(',', $arAllRows).']' . '}'; | |
} | |
} | |
function findAllTableFields($result_resource){ | |
$fields = array(); | |
$this->num_fields = mysql_num_fields($result_resource); | |
for($i = 0; $i < $this->num_fields; $i++){ | |
$field_meta = mysql_fetch_field($result_resource); | |
$fields[] = $field_meta->name; | |
} | |
return $fields; | |
} | |
function encode_msg($msg){ | |
return utf8_decode(utf8_encode($msg)); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment