我发现了一个php类,它似乎可以帮助我对数据库进行高级搜索。
这是:
<?php
class search {
var $table;
var $field1;
var $field2;
function queryRow($query){
//define database settings
define("host", "xxxxxxxx");
define("login", "xxxxxx");
define("senha", "xxxxxxx");
//define database name
define("data", "xxxxx");
//conection routine
try{
$host = host;
$data = data;
$connection = new PDO("mysql:host=$host;dbname=$data", login, senha);
//$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $connection->prepare($query);
$result->execute();
return $result;
$this->connection = $connection;
}catch(PDOException $e){
echo $e->getMessage();
}
}
function close($connection){
$connection = null;
}
function query($query){
$host = host;
$result = $this->queryRow($query);
$row = $result->fetch(PDO::FETCH_ASSOC);
$this->close($this->connection);
$this->query = $query;
return $row;
}
//finish connection
//method to list the fields
function fieldSelect(){
$query = $this->queryRow('SELECT Gender,Orphan,County,District FROM '.$this->table);
$retorno = "<select name=\"fieldselect\">\n";
foreach ($query as $collums){
if ($_POST['fieldselect'] == $collums['Field']){
$selected = " selected=\"selected\" ";
}else{
$selected = "";
}
$retorno .= "<option value=\"$collums[Field]\"$selected>$collums[Field]</option>\n";
}
$retorno .= "</select>\n";
return $retorno;
}
//method to select the functions to condictions
function whereSelect(){
$wheres = array();
$wheres[] = 'equal';
$wheres[] = 'diferent';
$wheres[] = 'minor';
$wheres[] = 'more';
$wheres[] = 'minororequal';
$wheres[] = 'moreorequal';
$wheres[] = 'content';
$wheres[] = 'notcontent';
$wheres[] = 'between';
$wheres[] = 'notbetween';
$label[] = 'Equal';
$label[] = 'Diferent';
$label[] = 'Minor';
$label[] = 'More';
$label[] = 'Minor or Equal';
$label[] = 'More or Equal';
$label[] = 'Content';
$label[] = 'Not Content';
$label[] = 'Between';
$label[] = 'Not Between';
$retorno = "<select name=\"select\">\n";
$i=0;
do{
if ($_POST['select'] == $wheres[$i]){
$selected = " selected=\"selected\" ";
}else{
$selected = "";
}
$retorno .= "<option value=\"$wheres[$i]\"$selected>$label[$i]</option>\n";
$i++;
}while($i < count($wheres));
$retorno .= "</select>\n";
return $retorno;
}
function fieldText($size, $max){
$retorno .= "<input type=\"text\" name=\"fieldtext\" size=\"$size\" maxlength=\"$max\" value=\"$_POST[fieldtext]\" />\n";
return $retorno;
}
//method to implement condictions and your variables
function wheres($value){
$retorno = "";
//parei aqui
$this->field2 = explode(' OR ',$this->field2);
//var_dump($this->field2);
$i = 0;
switch($value){
case 'equal':
foreach ($this->field2 as $field2){
$retorno .= "$this->field1 = '$field2' ";
$i = ++$i;
if ($i != 0 && $i != count($this->field2)){
$retorno .= " OR ";
}
}
break;
case 'diferent':
foreach ($this->field2 as $field2){
$retorno .= "$this->field1 != '$field2'";
$i = ++$i;
if ($i != 0 && $i != count($this->field2)){
$retorno .= " OR ";
}
}
break;
case 'minor':
foreach ($this->field2 as $field2){
$retorno .= "$this->field1 < '$field2'";
$i = ++$i;
if ($i != 0 && $i != count($this->field2)){
$retorno .= " OR ";
}
}
break;
case 'more':
foreach ($this->field2 as $field2){
$retorno .= "$this->field1 > '$field2'";
$i = ++$i;
if ($i != 0 && $i != count($this->field2)){
$retorno .= " OR ";
}
}
break;
case 'minororequal':
foreach ($this->field2 as $field2){
$retorno .= "$this->field1 <= '$field2'";
$i = ++$i;
if ($i != 0 && $i != count($this->field2)){
$retorno .= " OR ";
}
}
break;
case 'moreorequal':
foreach ($this->field2 as $field2){
$retorno .= "$this->field1 >= '$field2'";
$i = ++$i;
if ($i != 0 && $i != count($this->field2)){
$retorno .= " OR ";
}
}
break;
case 'content':
foreach ($this->field2 as $field2){
$retorno .= "$this->field1 LIKE '%$field2%'";
$i = ++$i;
if ($i != 0 && $i != count($this->field2)){
$retorno .= " OR ";
}
}
break;
case 'notcontent':
foreach ($this->field2 as $field2){
$retorno .= "$this->field1 NOT LIKE '%$field2%'";
$i = ++$i;
if ($i != 0 && $i != count($this->field2)){
$retorno .= " OR ";
}
}
break;
case 'between':
foreach ($this->field2 as $field2){
$retorno .= "$this->field1 BETWEEN $field2";
$i = ++$i;
if ($i != 0 && $i != count($this->field2)){
$retorno .= " OR ";
}
}
break;
case 'notbetween':
foreach ($this->field2 as $field2){
$retorno .= "$this->field1 NOT BETWEEN $field2";
$i = ++$i;
if ($i != 0 && $i != count($this->field2)){
$retorno .= " OR ";
}
}
break;
}
return $retorno;
}
//method to list results of sql consult
function result($fields){
if (isset($_POST['submit'])){
$this->field1 = $_POST['fieldselect'];
$this->field2 = $_POST['fieldtext'];
$resultfields = "";
if(is_array($fields)){
$i = 0;
foreach($fields as $collums){
if($i< count($fields)-1){
$resultfields .= $collums.', ';
}else{
$resultfields .= $collums;
}
$i = ++$i;
}
}else{
$resultfields = $fields;
}
$query = $this->queryRow("SELECT $resultfields FROM $this->table WHERE ".$this->wheres($_POST['select']));
$retorno = "<table>\n";
foreach($query as $querycollum){
$retorno .= "<tr>";
if(is_array($fields)){
foreach($fields as $collumstable){
$retorno .= "<td>$querycollum[$collumstable]</td>";
}
$retorno .= "</tr>\n";
}
}
$retorno .= "</table>\n";
return $retorno;
}
}
}
?>
到目前为止工作正常。见http://onlinestudentsadmission.com/schooldemo/dataform.php
但是,我希望做一些改变: 1.仅显示fieldselect中的某些列,而不是表中的所有列。似乎在search.class.php中提取列的代码是:
//method to list the fields
function fieldSelect(){
$query = $this->queryRow('SHOW FULL COLUMNS FROM '.$this->table);
我尝试过不同的sql select变种无济于事。
也就是说,首先应选择年份,然后选择性别,县,区和孤儿状态等其他条件。
看来我应该能够使用这个php类实现这一点,我只是不知道在哪里更改代码。
非常感谢任何帮助。