简体   繁体   English

通过下拉列表过滤数据库表

[英]Filter database table through drop-down list

I need to filter the table depends on the selected values of the drop-down lists.我需要根据下拉列表的选定值过滤表。 So know I need to know how to return the filter result into the page.所以知道我需要知道如何将过滤结果返回到页面中。 I made the filter result in search.php but I can't return the filter result to the table again.我在 search.php 中制作了过滤结果,但我无法再次将过滤结果返回到表中。 The filter should filter by user ID or product ID for now.过滤器现在应该按用户 ID 或产品 ID 进行过滤。

<body>
<?php include('navbar.php'); ?>
    <!-- Page header -->
    <div class="container">
        <h1 class="page-header text-center">Order system</h1>
    </div>

    <!-- Search -->
    <div class="container">
        <h3 class="text-left">Search</h3>
        <div class="col-md-4">
        <form method="post" action="search.php">
            <div class="form-group">
                <select name="search_date" class="form-control">
                    <option value="">Date</option>
                    <option value="">All time</option>
                    <option value="">Last 7 days</option>
                    <option value="">Today</option>
                </select>
            </div>
            <div class="form-group">
                <select name="user_id" class="form-control">
                    <option value="">User</option>
                    <option value="1">John Smith</option>
                    <option value="2">Laura Stone</option>
                    <option value="3">Jon Olseen</option>
                </select>
            </div>
            <div class="form-group">
                <select name="product_id" class="form-control">
                    <option value="">Product</option>
                    <option value="1">Pepsi Cola</option>
                    <option value="2">Coca Cola</option>
                    <option value="3">Fanta</option>
                </select>
            </div>
            <div>
                <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Filter</button>
            </div>
        </form>
        </div>
    </div>

    <!-- Order History -->
    <div class="container">
        <h3 class="text-left">Order History</h3>
        <table class="table table-striped table-bordered">
            <thead>
                <th>User</th>
                <th>Product</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Total</th>
                <th>Date</th>
                <th>Actions</th>
            </thead>
            <tbody>
                <?php
                    $sql="SELECT * FROM orders o JOIN user u ON o.user_id=u.user_id JOIN product p ON o.product_id=p.product_id";
                    $query=$conn->query($sql);
                    while($row=$query->fetch_array()){
                ?>
                    <tr>
                        <td><?php echo $row['user_name']; ?></td>
                        <td><?php echo $row['product_name']; ?></td>
                        <td><?php echo $row['product_price']; ?></td>
                        <td><?php echo $row['order_quantity']; ?></td>
                        <td><?php echo $row['order_price']; ?></td>
                        <td><?php echo $row['order_date']; ?></td>
                        <td>
                            <a href="#editorder<?php echo $row['order_id']; ?>" data-toggle="modal" class="btn btn-success btn-sm"><span class="glyphicon glyphicon-pencil"></span> Edit</a> || <a href="#deleteorder<?php echo $row['order_id']; ?>" data-toggle="modal" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span> Delete</a>
                                <?php include('order_modal.php'); ?>
                        </td>
                    </tr>
                <?php                
                    }
                ?>
            </tbody>
        </table>
    </div>

</body>
</html>

<?php
    include('conn.php');

    $search_date=$_POST['search_date'];
    $user_id=$_POST['user_id'];
    $product_id=$_POST['product_id'];


    $sql="select * from orders where user_id='$user_id' OR product_id='$product_id'";
    $query=$conn->query($sql);
    $row=$query->fetch_array();

    header('location:orderhistory.php');
?>
<?php include('header.php'); ?>
<body>
<?php include('navbar.php'); ?>
    <!-- Page header -->
    <div class="container">
        <h1 class="page-header text-center">Order system</h1>
    </div>

    <!-- Search -->
    <div class="container">
        <h3 class="text-left">Search</h3>
        <div class="col-md-4">
        <form method="post" action="">
            <div class="form-group">
                <select name="search_date" class="form-control">
                    <option value="100000">Date</option>
                    <option value="100000">All time</option>
                    <option value="7">Last 7 days</option>
                    <option value="0">Today</option>
                </select>
            </div>
            <div class="form-group">
                <select name="user_id" class="form-control">
                    <option value="%">User</option>
                    <option value="1">John Smith</option>
                    <option value="2">Laura Stone</option>
                    <option value="3">Jon Olseen</option>
                </select>
            </div>
            <div class="form-group">
                <select name="product_id" class="form-control">
                    <option value="%">Product</option>
                    <option value="1">Pepsi Cola</option>
                    <option value="2">Coca Cola</option>
                    <option value="3">Fanta</option>
                </select>
            </div>
            <div>
                <button type="submit" name="search" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Filter</button>
            </div>
        </form>
        </div>
    </div>

    <!-- Order History -->
    <div class="container">
        <h3 class="text-left">Order History</h3>
        <table class="table table-striped table-bordered">
            <thead>
                <th>User</th>
                <th>Product</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Total</th>
                <th>Date</th>
                <th>Actions</th>
            </thead>
            <tbody>
                <?php
                    if(isset($_POST["search"])){
                        $sql="SELECT * FROM orders o JOIN user u ON o.user_id=u.user_id JOIN product p ON o.product_id=p.product_id WHERE o.product_id LIKE '$_POST[product_id]' AND o.user_id LIKE '$_POST[user_id]' AND o.order_date>=DATE(NOW()) - INTERVAL '$_POST[search_date]' DAY";
                    }else{
                        $sql="SELECT * FROM orders o JOIN user u ON o.user_id=u.user_id JOIN product p ON o.product_id=p.product_id";
                    }
                        $query=$conn->query($sql);
                        while($row=$query->fetch_array()){
                ?>
                    <tr>
                        <td><?php echo $row['user_name']; ?></td>
                        <td><?php echo $row['product_name']; ?></td>
                        <td><?php echo $row['product_price']; ?></td>
                        <td><?php echo $row['order_quantity']; ?></td>
                        <td><?php echo $row['order_price']; ?></td>
                        <td><?php echo $row['order_date']; ?></td>
                        <td>
                            <a href="#editorder<?php echo $row['order_id']; ?>" data-toggle="modal" class="btn btn-success btn-sm"><span class="glyphicon glyphicon-pencil"></span> Edit</a> || <a href="#deleteorder<?php echo $row['order_id']; ?>" data-toggle="modal" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash"></span> Delete</a>
                                <?php include('order_modal.php'); ?>
                        </td>
                    </tr>
                <?php                
                    }
                ?>
            </tbody>
        </table>
    </div>

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM