1

我正在尝试为我的网页开发搜索功能。数据库中的某些列存储为表示值的整数。我有一个函数可以在返回结果时显示这些:

public function listsystypestatus($sysobj){

if ($this->sysobj ==='1') return "Core";
if ($this->sysobj ==='2') return "Aggregation";
if ($this->sysobj ==='3') return "Core/Aggr";
if ($this->sysobj ==='4') return "Customer";
if ($this->sysobj ==='5') return "TX Site";
return "--Please Select--";

我希望能够在此列上进行搜索并返回结果。例如,在“Cus”上进行通配符搜索,并返回在该列的数据库中存储为“4”的所有记录。我不知道如何解决这个问题。

我的搜索查询适用于其他(字符串)字段:

if($action=='search' && $search!=NULL){ 


 $wildsearch = "%%$search%%";
    $query = "SELECT *, INET_NTOA(ip_add) AS ip_add
    FROM `equipment` 
    LEFT JOIN itam_asset on equipment.itamname=itam_asset.ASSETID 
    WHERE site_code LIKE :search 
    OR site_id LIKE :search 
    OR system_name LIKE :search 
    OR INET_NTOA(ip_add) LIKE :search 
    OR sys_loc LIKE :search 
    OR systype LIKE :search 
    OR itamname LIKE :search 
    OR dimetis LIKE :search 
    OR DNS LIKE :search 
    ";

//Get equipment data
//select and prepare
$database->query($query);
//Bind
$database->bind(':search',$wildsearch);
  }else{
   ....
   ....
   Display results

我用来显示返回结果的代码是:

<?php
foreach($rows as $row){

//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
...
...
echo reults
...
...

谢谢大家。

编辑:新查询 - 现在有效:

$query = "SELECT *, INET_NTOA(ip_add) AS ip_add, list_systype.systype_label 
        FROM `equipment` 
        LEFT JOIN itam_asset on equipment.itamname=itam_asset.ASSETID 
        LEFT JOIN list_systype on equipment.systype=list_systype.systype_value
        WHERE site_code LIKE :search 
        OR site_id LIKE :search 
        OR system_name LIKE :search 
        OR INET_NTOA(ip_add) LIKE :search 
        OR sys_loc LIKE :search 
        OR list_systype.systype_label LIKE :search 
        OR itamname LIKE :search 
        OR dimetis LIKE :search 
        OR DNS LIKE :search 
        ";
4

1 回答 1

0

如果我很好理解你,如果你搜索“Cus”,你想搜索“4”,因为“4”在你的listsystypestatus函数中是“客户”。为此,您需要:

  1. 查找匹配的状态,并找出与其关联的号码
  2. 根据该整数在数据库中查找相关条目

在主题 2 上,您可以只限制查询以搜索 sysobj 字段。

我会稍微改写你的listsystypestatus逻辑,这样你就可以将它重用于搜索。

class Test {

  // Your type
  private $systypes = array('1' => 'Core', '2' => 'Aggregation', '3' => 'Core/aggr', '4' => 'Customer', '5' => 'TX Site');

  public function listsystypestatus($sysobj) {
    var_dump(array_key_exists($sysobj, $this->systypes));
    if(array_key_exists((int)$sysobj, $this->systypes)) {
      return $this->systypes[$sysobj];
    }
    return "--Please Select--";
  }

  // Retrieve the type
  public function gettype($search) {
    $matches = array_filter($this->systypes, function($text) use ($search) {
        return ( strpos($text, $search) !== false );
    });

    return array_keys($matches);
  }
}

$test = new Test();

// Retrieving records for 'Cus' (only one, 4)
var_dump($test->gettype('Cus'));

// Few more hits now
$result = $test->gettype('C');
var_dump(implode(',', $result));

使用生成的数组,您可以决定要在 SQL 查询中执行的操作。例如,对数组中的所有值执行 IN(1,2,3) 或类似操作。

于 2013-08-23T16:27:40.660 回答