簡體   English   中英

如何將選擇選項過濾為全部

[英]How to filter Select option to ALL

使用Select All選選項,我已經看到了很多演示或類似的問題。 但是我想要的只是一個下拉菜單,該菜單允許我選擇All選項,而不顯示整個Select框。

這是我的示例HTML:

<select name="status" id="status" style="width: 224px;">
    <option value="" selected="selected">Please select...</option>
    <option value="All">All</option>
    <option value="Option1">Option1</option>
    <option value="Option2">Option2</option>
    <option value="Option3">Option3</option>
    <option value="Option4">Option4</option>
</select>

從上面的代碼中,我只想使用All選項過濾結果。

所以這是我過濾並在表格中顯示結果的頁面,

<script>
$(document).ready(function(){
    $("#results").show();
});
</script>

<script type="text/javascript">
$(document).ready(function(){
    $("#RetrieveList").on('click',function() {
        var status = $('#status').val();
        var date = $('#Date').val();
        var date1 = $('#Date1').val();
        $.post('retrieve_status.php',{status:status, date:date, date1:date1}, function(data){
            $("#results").html(data);
        });
        return false;
    });
});
</script>

<form id="form2" name="form2" method="post" action="">
    <table width="941" border="0" align="center">
        <tr>
            <th width="935" colspan="9" scope="col">Status:
                <select name="status" id="status" style="width: 224px;">
                    <option value="" selected="selected">Please select...</option>
                    <option value="All">All</option>
                    <option value="Option1">Option1</option>
                    <option value="Option2">Option2</option>
                    <option value="Option3">Option3</option>
                    <option value="Option4">Option4</option>
                </select>
                Start Date:<input type="text" name="Date" id="Date" size="8"/>
                End Date:<input type="text" name="Date1" id="Date1" size="8"/>
                <input name="action" type="submit" id="RetrieveList" value="Retrieve List" />
            </th>
        </tr>
    </table>
</form>

<div id="results">
</div>

這就是我如何獲取數據,並進行更新以顯示Adam Silenko的代碼

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

// check data before use it and convert from string to expected type, use try, not like here:
$status = $_POST['status'];
$date = $_POST['date'];
$date1 = $_POST['date1'];
// use valid data to select rows
try {
    //1. connect to MySQL database
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

    //2. set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    //3. create query string (here is answer on your question)
    $sql = 'SELECT column1, column2, status FROM tracker WHERE 1=1';
    if ($status != 'ALL') $sql .= ' AND status = :st'; 
    $sql .= ' AND scheduled_start_date BETWEEN :d1 AND :d2';

    //4. prepare statement from query string
    $stmt = $conn->prepare($sql);

    //5. bind optional parameters
    if ($status != 'ALL') $stmt->bindParam(':st', $status);

    //6. bind parameters
    $stmt->bindParam(':d1', $date);
    $stmt->bindParam(':d2', $date1);

    //7. execute statement
    $stmt->execute();

    //8. returns an array containing all of the result set rows 
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

    //get count of rows
    $numrow = count($result);

    //print array - there is many solution to print array,
    //to debug you can do: 
    print_r($result);

} catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;

if($numrow == 0) 
  echo "No results found.";
else 
  echo "Count: $numrow</br>";
{

echo "<table width='auto' cellpadding='1px' cellspacing='0px' border=1 align='center'> 
<tr> 
<th align='center'><strong>Column1</strong></th> 
<th align='center'><strong>Column2</strong></th> 
<th align='center'><strong>Status</strong></th> 
</tr>";  

foreach ($result as $row => $info) {
echo "<form action='retrieve_status_test1.php' method='post'>";
echo"<tr>"; echo  "<td align='center'>" . $info['column1'] . "<input type=hidden name=column1 value=" . $info['column1'] . " </td>"; 
echo  "<td align='center'>" . $info['column2'] . "<input type=hidden name=column2 value=" . $info['column2'] . " </td>";  
echo  "<td align='center'>" . $info['status'] . "<input type=hidden name=status value=" . $info['status'] . " </td>"; 
echo "</tr>";  
echo "</form>"; 
} 
} 
echo "</table>";  
?>

僅當選項值不等於“ ALL”時,才將條件狀態添加到sql字符串中的where子句中,您應該使用[參數化查詢]

例:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

// check data before use it and convert from string to expected type, use try, not like here:
$status = $_POST['status'];
$date = $_POST['date'];
$date1 = $_POST['date1'];
// use valid data to select rows
try {
    //1. connect to MySQL database
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

    //2. set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    //3. create query string (here is answer on your question)
    $sql = 'SELECT column1, column2, status FROM tracker WHERE 1=1';
    if ($status != 'ALL') $sql .= ' AND status = :st'; 
    $sql .= ' AND scheduled_start_date BETWEEN :d1 AND :d2';

    //4. prepare statement from query string
    $stmt = $conn->prepare($sql);

    //5. bind optional parameters
    if ($status != 'ALL') $stmt->bindParam(':st', $status);

    //6. bind parameters
    $stmt->bindParam(':d1', $date);
    $stmt->bindParam(':d2', $date1);

    //7. execute statement
    $stmt->execute();

    //8. returns an array containing all of the result set rows 
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

    //get count of rows
    $numrow = count($result);

    //print array - there is many solution to print array,
    //to debug you can do: 
    //print_r($result);

} catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;

if($numrow == 0) 
  echo "No results found.";
else 
  echo "CRQ Count: $numrow</br>";

foreach ($result as $row => $info) {
  echo  $info['column1'] . ', ' . $info['column1'] . ', ' . $info['status'] . '; </br>';
}

將此php數組讀取為HTML表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM