使用不同的查询选择和排序数据

时间:2014-01-19 16:13:15

标签: php mysql

示例,在test.php中,如果用户选择$period = 'current'$sort = earliest,则只会显示当月的数据。 order(sort)dotest.php显示在test.php上。我怎么在桌子上?如何调出所选的具体数据?

<tr> <td class="alt"><label for="period"><b>Transaction Period:</b> </label></td> <td><input type="radio" name="period" value="current">Current Month<br> <input type="radio" name="period" value="current_first">Last 1 Month and Current Month<br> <input type="radio" name="period" value="current_second">Last 2 Months and Current Month</td> </tr> <tr> <td class="alt"><label for="sort"><b>Sort According To: </b></label></td> <td> <select name="sort"> <option value="latest">Latest Transaction First</option> <option value="earliest">Earliest Transaction First</option> <option value="codes">Transaction Codes</option> <option value="credit">Credit Only</option> <option value="debit">Debit Only</option> </select> </td> </tr> <input type="submit" value="Submit"></input>

dotest.php

<?php $period = $_POST['period']; $sort = $_POST['sort']; if ($period == 'current'){ $query1 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND MONTH(CURDATE())= MONTH(date)"; } elseif ($period == 'current_first'){ $query1 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND date BETWEEN CURRENT_DATE - INTERVAL 1 MONTH AND CURRENT_DATE"; } else if ($period == 'current_second'){ $query1 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND date BETWEEN CURRENT_DATE - INTERVAL 2 MONTH AND CURRENT_DATE"; } else { } $result1 = mysqli_query( $link, $query1 ) or die( mysqli_error( $link ) ); ?> <?php if ($sort == 'latest'){ $query2 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' ORDER BY date DESC"; } else if ($sort == 'earliest'){ $query2 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' ORDER BY date ASC"; } else if ($sort == 'codes'){ $query2 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' ORDER BY transaction_code ASC"; } else if ($sort == 'credit'){ $query2 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND debit IS NULL"; } else if ($sort == 'debit'){ $query2 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND credit IS NULL"; } else { } $result2 = mysqli_query( $link, $query2 ) or die( mysqli_error( $link ) ); while ($row2 = mysqli_fetch_array($result2)) { $date1 = $row2['date']; $trans_code = $row2['transaction_code']; $reference = $row2['reference']; $debit = $row2['debit']; $credit = $row2['credit']; $date = new DATETIME($date1); ?> <tbody> <tr> <td><?php echo $date->format('d-m-Y'); ?></td> <td><?php echo $trans_code ?></td> <td><?php echo $reference ?></td> <td>$ <?php echo $debit ?></td> <td>$ <?php echo $credit ?></td> <?php } ?>

{{1}}

1 个答案:

答案 0 :(得分:0)

您应该制作排序单选按钮,以及下面的信用卡/借记卡类型单选按钮

            <tr> 
                <td class="alt"><label for="period"><b>Transaction Period:</b> </label></td>
                <td><input type="radio" name="period" value="current">Current Month<br>
                    <input type="radio" name="period" value="current_first">Last 1 Month and Current Month<br>
                    <input type="radio" name="period" value="current_second">Last 2 Months and Current Month</td>          
            </tr>
            <tr>
                <td class="alt"><label for="sort"><b>Sort According To: </b></label></td>
                <td><input type="radio" name="sort" value="latest">Latest Transaction First<br>
                    <input type="radio" name="sort" value="earliest">Earliest Transaction First<br>
                    <input type="radio" name="sort" value="codes">Transaction Codes</td>
            </tr>
            <tr>
                <td class="alt"><label for="type"><b>Credit/Debit Type:</b></label></td>
                <td><input type="radio" name="type" value="both">Credit and Debit<br>
                    <input type="radio" name="type" value="credit">Credit Only<br>
                    <input type="radio" name="type" value="debit">Debit Only </td>        
            </tr>

然后在dotest.php基于以下

这些值构建您的查询
$period = $_POST['period'];
$sort = $_POST['sort'];
$type = $_POST['type'];

                if ($period == 'current'){
                    $query2 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND MONTH(CURDATE())= MONTH(date)";
                }
                elseif ($period == 'current_first'){
                    $query2 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND date BETWEEN CURRENT_DATE - INTERVAL 1 MONTH AND CURRENT_DATE";
                }
                else if ($period == 'current_second'){
                    $query2 = "SELECT * FROM transaction WHERE account_id ='$acc_id' AND user_id= '$user_id' AND date BETWEEN CURRENT_DATE - INTERVAL 2 MONTH AND CURRENT_DATE"; 
                }

                if ($type == 'credit'){
                    $query2 .= " AND debit IS NULL";
                }
                else if ($type == 'debit'){
                    $query2 .= " AND credit IS NULL";
                }

                if ($sort == 'latest'){
                    $query2 .= " ORDER BY date DESC";
                }
                else if ($sort == 'earliest'){
                    $query2 .= " ORDER BY date ASC";    
                }
                else if ($sort == 'codes'){
                    $query2 .= " ORDER BY transaction_code ASC";
                }

这样你只执行一个查询(query2)来完成你想要的所有选项。