MySQL相关性排序

时间:2015-11-04 15:13:52

标签: php mysql

我正在尝试找出订购搜索结果的最佳方式,它们需要按照以下顺序排列,但它似乎无法正常工作。

这可能是因为正在选择列评级但是用户没有填写名为评级的输入吗?

if(isset($_POST['search'])){
   $lname = trim($_POST['lname']);
   $address = trim($_POST['address']);
   $street = trim($_POST['street']);
   $city = trim($_POST['city']);
   $county = trim($_POST['county']);

   if($lname==""){
      $error[] = "Provide Last Name."; 
   }elseif($address=""){
       $error[] = "Provide House Number/Name.";
   }elseif($street==""){
      $error[] = "Provide Street.";
   }elseif($city==""){
       $error[] = "Provide City.";
   }elseif($county==""){
       $error[] = "Provide County.";
   }else{
       try{
           $stmt = $conn->prepare("SELECT *
           FROM clients
           WHERE  lname  like :lname
           AND   address like :address
           AND   street  like :street
           AND   city    like :city
           AND   county  like :county
           ORDER BY rating 
           WHEN rating like 'badabusive' THEN 0
           WHEN rating like 'badnopay' THEN 1
           WHEN rating like 'cautionlate' THEN 2
           WHEN rating like 'cautiondiscount' THEN 3
           WHEN rating like 'cautionextra' THEN 4
           WHEN rating like 'good' THEN 5
           END, rating");

           $stmt->execute(array("lname"  => empty($lname) ? '%' : '%'. $lname .'%',
           "address" => empty($address) ? '%' : '%'. $address .'%',
           "street"  => empty($street) ? '%' : '%'. $street .'%',
           "city"    => empty($city) ? '%' : '%'. $city .'%',
           "county"  => empty($county) ? '%' : '%'. $county .'%'));

    $userRow = $stmt->fetch(PDO::FETCH_ASSOC);

                if(!$userRow){                  
                     if($user->reportsearch($id,$lname,$address,$street,$city,$county)){
                          $user->redirect('./results.php?new='.$lname.$street);
                      }
                }else{
                    if($userRow['rating'] == 'badnopay'){
                        $user->redirect('./results.php?badnopay='.$lname.$street);
                    }
                    if($userRow['rating'] == 'cautionextra'){
                        $user->redirect('./results.php?cautionextra='.$lname.$street);
                    }
                    if($userRow['rating'] == 'cautiondiscount'){
                        $user->redirect('./results.php?cautiondiscount='.$lname.$street);
                    }
                    if($userRow['rating'] == 'cautionlate'){
                        $user->redirect('./results.php?cautionlate='.$lname.$street);
                    }
                    if($userRow['rating'] == 'good'){
                        $user->redirect('./results.php?good='.$lname.$street);
                    }
                    if($userRow['rating'] == 'badabusive'){
                        $user->redirect('./results.php?badabusive='.$lname.$street);
                    }
                }
            }
        catch(PDOException $e){
             echo $e->getMessage();
        }
    } 
}

我希望结果按此顺序显示,因此,如果我有多个具有不同评分的结果,那么我希望它们按顺序显示。

它是在提交时重新加载页面,而不是正常情况下显示结果页面。

1 个答案:

答案 0 :(得分:1)

您必须更改脚本。

  1. 查询,订单后的时间不正确
  2. 您必须打印出您想要的网址,可以重定向,但它会将您重定向到第一行(或第一个结果)
  3. 您的数据中有badshort评级,这将导致null,因为您没有“badshort”规则成为值。
  4. 无邮件测试的完整脚本将成为:

    <?php
    $lname = 'something';
    $address = 'something1';
    $street = 'something2';
    $city = 'something3';
    $county = 'something4';
    
    try{
    $stmt = $conn->prepare("SELECT *, case 
    WHEN rating like 'badabusive' THEN 0 
    WHEN rating like 'badnopay' THEN 1 
    WHEN rating like 'cautionlate' THEN 2 
    WHEN rating like 'cautiondiscount' THEN 3 
    WHEN rating like 'cautionextra' THEN 4 
    WHEN rating like 'good' THEN 5 
    END as ratingval FROM clients 
    WHERE  
    lname  like :lname and
    address  like :address and
    street  like :street and
    city  like :city and
    county  like :county
    order by ratingval");
    
    $stmt->bindParam(':lname', $lname);
    $stmt->bindParam(':address', $address);
    $stmt->bindParam(':street', $street);
    $stmt->bindParam(':city', $city);
    $stmt->bindParam(':county', $county);
    
    $stmt->execute();
    
    $userRow = $stmt->fetchAll();
    if(!$userRow){
    print "nothing found";
    }
    else
    {
    foreach ($userRow as $result) {
    echo $result[lname] . " " . $result[ratingval]. "<br>";
    }
    }
    }
    catch(PDOException $e){
    echo $e->getMessage();
    }
    ?>