创建下拉分类过滤器


Create dropdown category filter

有一个.txt文件保存来自输入的每条记录。我读取文件并按特定的字符展开它。这是第3列所有值的和。我想实现一个类别筛选选项。

我的代码:

<?php
                if(file_exists('store.txt')){ 
                    $result=file('store.txt');
                    foreach($result as $value){ 
                         $columns =  explode('!', $value);  
                         $price = array($columns[2]); 
                         $sum += $price[0]; 
                         if(isset($_POST['filter'])){ //There's the part with the filter
                             $filter = $_POST['category'];

                         }
                         echo 
                              '<tr><td>'.$columns[0].'</td>
                              <td>'.$columns[1].'</td>
                              <td>'.$columns[2].'</td>
                              <td>'.$columns[3].'</td></tr>';
                        }
                }
                             ?>

提前感谢!

将服务器上的文本文件中的值打印到HTML表中,并使用PHP限制结果,求和价格。

getstore.php:

if(file_exists('store.txt')) {
    $result=file('store.txt');
    $filter = 'no';
    if ( isset($_POST['filter']) ) {
        $filter = $_POST['filter'];
        echo 'Filter = "' . $filter . '"';
    }
    else { echo 'No filter selected.'; }
    if ( 0 < count($result) ) {
        echo '<table border="1"><tr><th>column 1</th><th>column 2</th><th>price</th><th>filter</th></tr>';
        $sum = 0;
        foreach($result as $value) {
            $columns =  explode('!', $value);
            if ( 'no' == $filter || trim($columns[3]) == $filter ) {
                $sum += $columns[2]; // add price to total
                echo '<tr><td>'.$columns[0].'</td><td>'.$columns[1].'</td><td>'.$columns[2].'</td><td>'.$columns[3].'</td></tr>';
            }
        }
        echo "<tr><td></td><td align='"right'"><strong>SUM:</strong></td><td>$sum</td><td></td></tr></table>";
    }
    else { 'Empty file.'; }
}
else { echo 'File not found.'; }
?>
<form action="getstore.php" method="post">
    <select name="filter">
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>
    <input type="submit">
</form>

当我选择过滤器2时,得到:

Filter = "2"
<table border="1">
    <tr>
        <th>column 1</th>
        <th>column 2</th>
        <th>price</th>
        <th>filter</th>
    </tr>
    <tr>
        <td>row 4, column 1, index 0</td>
        <td>column 2, index 1</td>
        <td>25</td>
        <td>2</td>
    </tr>
    <tr>
        <td>row 5, column 1, index 0</td>
        <td>column 2, index 1</td>
        <td>30</td>
        <td>2</td>
    </tr>
    <tr>
        <td>row 6, column 1, index 0</td>
        <td>column 2, index 1</td>
        <td>35</td>
        <td>2</td>
    </tr>
    <tr>
        <td></td>
        <td align="right"><strong>SUM:</strong></td>
        <td>90</td>
        <td></td>
    </tr>
</table>