根据下拉列表中的选定值显示记录

时间:2015-03-10 19:14:37

标签: php

用户搜索名为search.php的页面,搜索结果显示在另一个名为results的页面上。

这很有效。

以下是相关代码:

  $fields = array(
      'projectTitle' => array('field' => 'b.BidTitle', 'searchType' => 'like'),
      'BidType' => array('field' => 'b.BidType', 'searchType' => 'equal'),
      'BidStatus' => array('field' => 'b.BidStatus', 'searchType' => 'equal'),
      'department' => array('field' => 'b.Department', 'searchType' => 'equal'),
      'bidId' => array('field' => 'b.BidID', 'searchType' => 'equal'),
      'bidDate' => array('field' => 'b.BidDate', 'searchType' => 'equal'),
      'dueDate' => array('field' => 'b.DueDate', 'searchType' => 'equal')

  );

  $where = array();
  foreach($fields as $fieldPost => $field) {
      if(isset($_POST[$fieldPost]) && strlen($_POST[$fieldPost]) > 0) {
          if($field['searchType'] == 'like') {
              $where[] = "".$field['field']." LIKE '%" . ms_escape_string($_POST[$fieldPost]) . "%'";
          } else {
              $where[] = "".$field['field']." = '" . ms_escape_string($_POST[$fieldPost]) . "'";
          }
      }
  }
     $sql = "Select b.ID,convert(char(10),b.BidDate,101) BidDate,convert(char(10),
            b.DueDate,101)DueDate,b.BidTitle,b.DueTime,b.BidID,BidIDFile,
            d.Department,b.BidType,CASE WHEN b.AwardDate ='01/01/1900' Then NULL ELSe convert(char(10),b.AwardDate,101)END AS AwardDate,
            convert(char(10),b.LastUpdate,101) LastUpdate,s.Status
            FROM bids b inner join dept d on b.Department=d.DeptID inner join Status s on b.BidStatus=s.StatusId " . ( count($where) > 0 ? " WHERE " . implode(' AND ', $where) : "" );
  //echo $sql;
    $params = array();
    $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
    $query = sqlsrv_query( $conn, $sql , $params, $options );

    $num_rows = sqlsrv_num_rows($query);

现在,我有从View - >生成的以下下拉值:源。

<select name="SType">
  <option value=""></option>
  <option value="Bid" name="Bid">Bid[2]</option>
  <option value="Lib CM" name="Library CM">Lib CM[23]</option>
  <option value="Quote" name="Quote">Quote[20]</option>
  <option value="RFP" name="RFP">RFP[2]</option>
  <option value="Removed" name="Removed">Removed[29]</option>
  <option value="Archived" name="Archived">Archived[4501]</option>
  <option value="Awarded" name="Awarded">Awarded[40]</option>
  <option value="Open" name="Open">Open[47]</option>
  <option value="Closed" name="Closed">Closed[11]</option>
  <option value="Under Review" name="Under Review">Under Review[126]</option>
  <option value="Cancelled" name="Cancelled">Cancelled[64]</option>
</select>

此下拉列表还驻留在search.php页面上,并使用php动态填充来自sql server db的记录。

与上面的代码一样,当用户从下拉列表中选择一个值时,我们希望显示与从下拉列表中选择的值相关联的记录的结果。

我试图将下面的代码与上面的代码集成,但它没有给我我预期的结果。预期结果仅与下拉值相关联。

例如,Bid有两条记录。当用户从下拉列表中选择出价时,我们预计只会显示两条记录。

相反,也会显示与出价无关的sevaral记录。

任何想法如何解决这个问题?

//If value matches dropdown value, then display records associated with dropdown value
if(isset($_REQUEST['Solicitations'])){
    if($_GET['Solicitations'] == "Bid"){
    $result = "SELECT * FROM bids where BidStatus = 1 and BidType = 'Bid' ORDER BY title";
    $query = sqlsrv_query( $conn, $sql , $params, $options );
    }
  } 

非常感谢我在这里获得的所有帮助。

0 个答案:

没有答案