如何在php中从我的数据库中添加我的表的过滤器选项

时间:2018-04-13 09:05:53

标签: php html mysql mysqli

我想通过例如'product_id'过滤我的数据库输出。因此,只显示具有相同ID的产品。我在HTML中添加了一个下拉列表,但我不知道如何将它与我的mysql请求连接起来。

Here you can see what the headers of my table look like, and how my dropdown looks, it already takes the data of my database, so it already shows the right products

 <div class="form-group{P1}">
<label class="col-sm-2 control-label" for="selectp1" value="{P1}">P</label>
<div class="col-sm-10">
    <select id="selectp1" name="selectp1" required autofocus class="form-control selectpicker unloadmsg" onchange="showSelects();">
        {POPTIONS}
    </select>
</div>

     private function getDatensatzQuery() {
        $sql = "SELECT [....]


                . 'LEFT JOIN [....];

        if ($filterdata['filterfor'] == 'date') {
            $sql .= "AND DATE(a.datum) = '$filterdata[datum]' ";
        } else if ($filterdata['filterfor'] == 'daterange') {
            $sql .= "AND a.datum BETWEEN '" . date('Y-m-d H:i:s', 
  $filterdata['filterrangefrom']) . 
                    "' AND '" . date('Y-m-d H:i:s', 
  $filterdata['filterrangeto']) . "' ";
        }
        if($filterdata['filterfor']=='produkt'){
            $sql .= "AND p_id= '$filterdata[p]'";
        }

        $sql .= "ORDER BY $orderby " . (($orderdesc) ? 'DESC' : 'ASC');
        $sql .= ($paginate) ? " LIMIT $start, $schritt" : '';          
        return $sql;
}

随意提问

2 个答案:

答案 0 :(得分:1)

编辑:删除了我的上一条评论,这里是我对您的问题的理解所编写的内容,但我无法使用SQL过滤器/排序,因为我从未使用过它

<table style="text-align: center;">
    <tr>
        <th>
            ID
        </th>
        <th>
            NAME
        </th> 
        <th>
            IP
        </th>
    </tr>
<?php
    $servername = "";
    $susername = "";
    $spassword = "";
    $dbname = "";
    $conn = mysqli_connect($servername, $susername, $spassword, $dbname); //Connect
    if(!empty($_POST['option_filter'])){
        $choosen_filter = $_POST['option_filter'];
        //You can now create the SQL request to filter the results to display to the user depending on one of the options
        if($choosen_filter == 'filtrer_ids'){
            //SQL filter per IDS
        }else if($choosen_filter == 'filtrer_names'){
            //SQL filter per NAMES
        }else if($choosen_filter == 'filtrer_ips'){
            //SQL filter per IPS
        }
        // ----------------------------------------------------------------------------------
        // This is just an example to get every row of the database with all 3 variables
        // You can print each row with all results just after sorting/filtering them
        $sql = "SELECT id,username,ip FROM users_database";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $MYSQL_username = $row['username'];
                $MYSQL_ip = $row['ip'];
                $MYSQL_id = $row['id'];
                $display=1; //Just to remember that you got results
                //Then print the entire row with each values
                print('<tr><th>'.$MYSQL_username.'/th><th>'.$MYSQL_ip.'</th><th>'.$MYSQL_id.'</th></tr>');
               }
        }
        // ----------------------------------------------------------------------------------
    }
?>
</table>
<form action="PAGE.php" method="POST" id="filter">
    <select name="option_filter">
        <option value="filtrer_ids">Filter IDS</option>
        <option value="filtrer_names">Filter NAMES</option>
        <option value="filtrer_ips">Filter IPS</option>
    </select>
    <input type="submit" name="submit" value="filter">
</from>

答案 1 :(得分:1)

这是我很久以前的尝试^^为我工作,但我只使用了1个过滤器。 请注意我使用了JQuery。

在下拉列表旁边添加了一个按钮:

<button id="suchen" onclick="Suchen()" value="Suche starten" class="btn btn-secondary">Suche starten</button>

它调用我的名为“Suchen()”的搜索功能(JS函数)。

function Suchen() {
     var eingrenzung1 = $("#filter :selected").text();
     var eingrenzung2 = $("#filter2 :selected").text();
     var eingrenzung3 = $("#filter3 :selected").text();
     var eingrenzung4 = $("#filter4 :selected").text();
     //Just an idea, otherwise the if-Check would be false
     if(eingrenzung2 == ""){
           var eingrenzung2 = "%"
     }
     if(eingrenzung3 == ""){
           var eingrenzung3 = "%"
     }
     if(eingrenzung4 == ""){
           var eingrenzung4 = "%"
     }
     Ajax('#tabelle', 'http://localhost/.../ajax/Tabelle.php?eingrenzung1='+eingrenzung1+'&eingrenzung2='+eingrenzung2+'&eingrenzung3='+eingrenzung3+'&eingrenzung4='+eingrenzung4;

}

它将数据发送到表格(我已将表格放在额外的文档中)并激活该文档中的if-check

if(isset($_GET['eingrenzung1']) AND isset($_GET['eingrenzung2']) AND isset($_GET['eingrenzung3']) AND isset($_GET['eingrenzung4'])) {
    $kopf1->Sucher($_GET['eingrenzung1'], $_GET['eingrenzung2'], $_GET['eingrenzung3'], $_GET['eingrenzung4']);
    $result2 = $kopf1->alleDaten;
    $anzahl = $kopf1->anzahl;
}

$ kopf1是一个对象,不知道你是否在使用OOP? 和Sucher类功能:

public function Sucher($eingrenzung1,$eingrenzung2,$eingrenzung3,$eingrenzung4){
    $query = "SELECT * FROM table WHERE Column1 LIKE '$eingrenzung1%' AND Column2 LIKE '$eingrenzung2%' AND Column3 LIKE '$eingrenzung3%' AND Column4 LIKE '$eingrenzung4%'";
    $this->Ausfuehren($query);
    $this->alleDaten = $this->query->fetchAll(PDO::FETCH_BOTH);
    $this->anzahl = $this->query->rowCount();
    return $this->alleDaten;
}

然后我回显表中的结果(在Table.php中)

<?php
for($i=0;$i < $anzahl;$i++){    ?>
    <tr id="<?php echo $result2[$i][0]; ?>" name="j">
        <td class="t1"><input type='checkbox' name='multiDelete' class="bd"></td>
        <td class="t1" value="<?php echo $result2[$i][0]; ?>"><?php echo $result2[$i][0]; ?></td>
        <td class="t1"><?php echo $result2[$i][1]; ?></td>
        <td class="t1"><?php echo $result2[$i][2]; ?></td>
        <td class="t1"><?php echo $result2[$i][3]; ?></td>
        <td class="t1"><?php echo $result2[$i][4]; ?></td>
        <td class="t1"><?php echo $result2[$i][5]; ?></td>
        <td class="t1"><?php echo $result2[$i][6]; ?></td>
        <td class="t1"><?php echo $result2[$i][7]; ?></td>

    </tr>
<?php
}   

&GT;

(我知道它看起来不太好^^)

不知道这是否有帮助

编辑:也许它现在对你的问题更合适了