如何选择要显示在下表中的所有搜索选项?

时间:2019-11-05 13:16:51

标签: php sql

我正在创建表格以选择要在下表中显示的所有选项,我想在选择了在表中显示的选项时进行搜索。我要做的部分是让我更改“交易类型”,然后显示下表。怎样的功能可以让我选择所有选项以显示在下表中?希望任何人都可以给我示例或在我的代码中进行编辑以指导我如何做。我希望输出可以遵循我选择的日期范围和交易类型来显示表格。

transaction_history文件以显示前端,创建表单并将数据传递到后端。

    <?php
 $system_user_type = $user_type;

     $lang = $_COOKIE["Language"];
   $new_cur_date = date("d-m-Y", strtotime($cur_date));
 require_once("language/lang_transaction_" . $lang . ".php");
    ?>

   <html>
  <head>
   </head>
     <body>


      <div class="row">
        <div class="col-lg-12">
            <div class="box form-group">
                <header>
                    <h5><?php echo $language["LIST_TITLE1"]; ?></h5>
                    <!-- .toolbar -->
                    <div class="toolbar">
                        <nav style="padding: 8px;">
                            <a href="javascript:;" class="btn btn-default btn-xs collapse-box">
                                <i class="fa fa-minus"></i>
                            </a>
                        </nav>
                    </div><!-- /.toolbar -->
                </header>
                <section class="content">
                    <div class="col-lg-12  form-group" >
                        <label for="text1" class="form-group control-label col-lg-2"><?php echo $language['type']; ?>:</label>
                        <div class="col-lg-2">
                            <select id="select_type" class="form-group form-control required" onchange="show_table();">
                                <option value="transfer" selected><?php echo $language["transfer"]; ?></option>
                                <option value="withdraw"><?php echo $language["withdraw"]; ?></option>
                                <option value="upgrade"><?php echo $language["upgrade"]; ?></option>
                                <option value="register">Register</option>
                                <option value="receive"><?php echo $language["receive"]; ?></option>

                            </select>
                        </div>
                        <div class="col-lg-8"></div>
                    </div>
                       <div class="col-lg-12 form-group">
                            <label for="text1" class="form-group control-label col-lg-2">Date Range:</label>
                            <div class="col-lg-2">
                                <?php echo custom_period_opt(); ?>
                            </div>
                            <label for="text1" class="form-group control-label col-lg-2">Date Created</label>
                            <div class="col-lg-2">
                                <input type="text" class="form-group form-control datepicker" id="start_date" name="start_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
                            </div>
                            <label for="text1" class="form-group control-label col-lg-2">To</label>
                            <div class="col-lg-2">
                                <input type="text" class="form-group form-control datepicker" id="end_date" name="end_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
                            </div>
                        </div>
  <div class="col-lg-12" style="text-align:center; padding-bottom:10px; padding-top:10px;">
                <button id="search_button" type="button" class="btn btn-sm btn-primary" onclick="search2_('search', 'bill_table', 'billing');">Search</button>  
                <button id="clear" type="button" class="btn btn-sm btn-default" onclick="clearData()">Clear</button>    
            </div>
                    <div class="" id="table_result">

                    </div>
                       </section>
                </div>
            </div>
        </div>

     </body>
     </html>




           <script>
                                $(function() {
                                   show_table();
                                    $("#select_type").on("change", function() {
                                        show_table();
                                    });
                                });
                                function show_table() {
                                    //alert(123);
                                    var select_type = $("#select_type").val();
                                    $.ajax({
                                        url: "?f=transaction_table",
                                        type: "POST",
                                        data: {
                                            select_type: select_type
                                        },
                                        before_send: function() {
                                            show_overLay();

         //$('#patient_result').html('');
                                        },
                                        success: function(data) { 
                                            hide_overLay('');
                                            //alert(data);
                                            if (data) {

      $("#table_result").html("");

  $("#table_result").append(data);
                                                // 
     $('.dataTable').dataTable();
                                            } else {
                                                alert("Please fill in the field.");
                                            }
                                        }
                                    });
                                }

      </script>

      <style>
       .myClass
     {
    display: none;
         }

       </style>

transaction_table文件以显示发送到前端页面的后端功能。下面是我的编码:

          <?php
     $select_type = $_POST['select_type'];
  if ($select_type == "withdraw") {
   echo '<table class="table table-bordered table-striped table-hover">
    <thead>
        <tr>
            <th>No</th>
            <th>Date</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>';
        $i = 1;
        $select_transfer = 'SELECT * FROM withdrawal_record bp WHERE user_id = ' . $user_id . '  ORDER BY created';
        $arr_transfer = db_conn_select($select_transfer);
        foreach ($arr_transfer as $rs_transfer) {
            echo '<tr>';
            echo '<td>' . $i++ . '</td>';
            echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
            echo '<td>' . $rs_transfer['withdraw_amount'] . '</td>';
            echo '</tr>';
        }
echo " </tbody>
     </table>";
   }elseif ($select_type == "transfer") {
      ?>
       <table class="table table-bordered table-striped table-hover">
    <thead>
        <tr>
            <th>No</th>
            <th>Date</th>
            <th>To Type</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        $select_transfer = 'SELECT * FROM transfer_history_bp WHERE user_id = ' . $user_id . '  ORDER BY created';
        $arr_transfer = db_conn_select($select_transfer);
        foreach ($arr_transfer as $rs_transfer) {
            if($rs_transfer['point_type']=="2"){
                $to_type="Register Point";
            }elseif($rs_transfer['point_type']=="3"){
                $to_type="Entertainment Point";
            }elseif($rs_transfer['point_type']=="4"){
                $to_type="Business Point";
            }
            echo '<tr>';
            echo '<td>' . $i++ . '</td>';
            echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
            echo '<td>' . $to_type . '</td>';
            echo '<td>' . $rs_transfer['total_amount'] . '</td>';
            echo '</tr>';
        }
        ?>
    </tbody>
        </table>
          <?php
        }elseif ($select_type == "upgrade") {
       ?>
       <table class="table table-bordered table-striped table-hover">
    <thead>
        <tr>
            <th>No</th>
            <th>Date</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        $select_transfer = 'SELECT * FROM upgrade_point WHERE user_id = ' . $user_id . ' ORDER BY created';
        $arr_transfer = db_conn_select($select_transfer);
        foreach ($arr_transfer as $rs_transfer) {
            echo '<tr>';
            echo '<td>' . $i++ . '</td>';
            echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
            echo '<td>' . $rs_transfer['total_amount'] . '</td>';
            echo '</tr>';
        }
        ?>
    </tbody>
         </table>
        <?php
      }elseif ($select_type == "register") {
    ?>
      <table class="table table-bordered table-striped table-hover">
    <thead>
        <tr>
            <th>No</th>
            <th>Date</th>
            <th>Amount</th>
        </tr>

    </thead>
    <tbody>
        <?php
        $i = 1;
        $select_transfer = 'SELECT * FROM transfer_history_rp WHERE user_id = ' . $user_id . ' and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" and use_type=1 ORDER BY created';
        $arr_transfer = db_conn_select($select_transfer);
        foreach ($arr_transfer as $rs_transfer) {
            echo '<tr>';
            echo '<td>' . $i++ . '</td>';
            echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
            echo '<td>' . $rs_transfer['total_amount'] . '</td>';
            echo '</tr>';
        }
        ?>
      </tbody>
       </table>
        <?php
     }elseif ($select_type == "receive") {
?>
<table class="table table-bordered table-striped table-hover">
    <thead>
        <tr>
            <th>No</th>
            <th>Date</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        $select_transfer = 'SELECT * FROM extra_point WHERE user_id = ' . $user_id . ' ORDER BY created';
        $arr_transfer = db_conn_select($select_transfer);
        foreach ($arr_transfer as $rs_transfer) {
            echo '<tr>';
            echo '<td>' . $i++ . '</td>';
            echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
            echo '<td>' . $rs_transfer['total_amount'] . '</td>';
            echo '</tr>';
        }
        ?>
    </tbody>
</table>
<?php
  }
      ?>

下面是我的输出,此输出仅可以遵循我选择的要在表中显示的onchange事务类型:

Output

以下是我的数据库信息:

Database information

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,那么您需要像在select_type中一样在请求中包括这些附加参数。

首先,在showTable函数中获取其他值,并将它们添加到请求数据中:

function show_table() {
    var select_type = $("#select_type").val();
    var start_date = $("#start_date").val();
    var end_date = $("#end_date").val();

    $.ajax({
        url: "?f=transaction_table",
        type: "POST",
        data: {
            select_type: select_type,
            start_date: start_date,
            end_date: end_date
        },
    // ...
}

然后,像使用select_type一样在PHP文件中接收值:

<?php
    $select_type = $_POST['select_type'];
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    // ...

最后,您必须相应地调整查询。由于您的查询中已经包含and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05",因此只需将硬编码日期替换为收到的日期即可。 别忘了清理您的输入日期!如果可能,请使用准备好的语句。

注释1 :您的scriptstyle标签位于网站的html部分之外。这是无效的。这些标签属于head标签或body标签。

注释2 :打开html标记之前有空格。您可能也不想避免这种情况,因为它可能会触发IE进入怪癖模式。只需更改

?>

   <html>

对此

?><html>