1

按 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;
       ?>
4

3 回答 3

0

我不太明白这个问题,如果您不知道如何为用户提供排序选项,您可以使用具有多项选择的列表框,然后在每个项目中查找“TRUE”。这在这里解释: http ://www.abbeyworkshop.com/howto/lamp/php-listbox/index.html

有很多方法可以实现,这是我认为可行的。另一个是有组合,所以你首先选择要过滤的内容,然后选择 asc 或 desc 的组合,然后是另一个组合,依此类推......你发布它并在这些组合中查找文本以制作查询字符串.

于 2013-02-24T03:22:01.220 回答
0

要实现排序,您可以添加一个order by子句,您可以在其中指定要对选择进行排序的字段。

如果您想让用户决定如何排序,您可以添加一个指定升序或降序的请求参数。此外,您可以添加一个请求参数“字段”,您可以使用该参数让用户决定他希望对选择进行排序的字段。

于 2013-02-23T11:57:36.970 回答
0

你可以这样做

SELECT * FROM table ORDER BY  Field1 ASC,Field2 DESC,Field3 ASC
于 2013-02-23T12:01:22.473 回答