按 Field1 Field2 Field3 ASC 和 DESC 对查询结果进行排序
你好朋友,我需要我能得到的帮助,我的网站上有一个过滤器,让我知道如何对泄露到 mysql 数据库的结果进行排序,因此 Field1 Field2 Field3 ASC 和 DESC
并希望在表单上创建选择选项的值,以便用户决定是 ASC 还是 DESC 排序过滤器和每个字段选择的选项值
<?php
$o = '';
// Pon la información correspondiente:
$data = array( 'localhost', 'user', 'password' );
$con = mysql_connect( $data[0], $data[1], $data[2] );
if( ! $con ) {
$o = 'Error: no se pudo conectar con el servidor. ' . mysql_error();
echo $o;
exit;
}
// Cambia el nombre de la base de datos por la tuya
$db_name = 'database';
if( ! mysql_select_db( $db_name, $con ) ) {
$o = 'Error: no se pudo seleccionar la base de datos "' . $db_name . '". ' . mysql_error();
echo $o;
exit;
}
$table = 'users'; // Cambia este SÓLO si sabes lo que hace.
$query = "SELECT * FROM $table";
$where = " WHERE";
$and = 0;
if( isset( $_GET['Nombre'] ) && ! empty( $_GET['Nombre'] ) ) {
$where .= " Nombre LIKE '%$_GET[Nombre]%'";
$and = 1;
}
if( isset( $_GET['Tarifa'] ) ) {
$e = explode( ' - ', $_GET['Tarifa'] );
if( is_numeric( $e[0] ) && is_numeric( $e[1] ) ) {
if( $and === 1 )
$where .= " AND";
$where .= " Tarifa BETWEEN $e[0] AND $e[1]";
$and = 1;
}
}
if( isset( $_GET['Edad'] ) ) {
$e = explode( ' - ', $_GET['Edad'] );
if( is_numeric( $e[0] ) && is_numeric( $e[1] ) ) {
if( $and === 1 )
$where .= " AND";
$where .= " Edad BETWEEN $e[0] AND $e[1]";
$and = 1;
}
}
if( isset( $_GET['Estatura'] ) ) {
$e = explode( ' - ', $_GET['Estatura'] );
if( is_numeric( $e[0] ) && is_numeric( $e[1] ) ) {
if( $and === 1 )
$where .= " AND";
$where .= " Estatura BETWEEN $e[0] AND $e[1]";
$and = 1;
}
}
if( isset( $_GET['Peso'] ) ) {
$e = explode( ' - ', $_GET['Peso'] );
if( is_numeric( $e[0] ) && is_numeric( $e[1] ) ) {
if( $and === 1 )
$where .= " AND";
$where .= " Peso BETWEEN $e[0] AND $e[1]";
$and = 1;
}
}
if( isset( $_GET['Ciudad'] ) && !empty( $_GET['Ciudad'] ) ) {
if( $and === 1 )
$where .= " AND";
$where .= " Ciudad = '$_GET[Ciudad]'";
$and = 1;
}
if( isset( $_GET['Ojos'] ) && !empty( $_GET['Ojos'] ) ) {
if( $and === 1 )
$where .= " AND";
$where .= " Ojos = '$_GET[Ojos]'";
$and = 1;
}
if( isset( $_GET['Cabello'] ) && !empty( $_GET['Cabello'] ) ) {
if( $and === 1 )
$where .= " AND";
$where .= " Cabello = '$_GET[Cabello]'";
$and = 1;
}
if( strlen( $where ) > 6 )
$query .= $where;
$result = mysql_query( $query, $con);
if( $result ) {
$nrows = mysql_num_rows( $result );
if( $nrows > 0 ) {
$o = '';
while( $row = mysql_fetch_assoc( $result ) ) {
$o .= "$row[Imagen]";
}
$o .= "";
} else {
$o = 'No hubieron resultados';
}
} else {
$o = 'Error: no se ejecutó la consulta. ' . mysql_error( $con );
}
mysql_free_result( $result );
mysql_close( $con );
echo $o . "";
exit;
?>