如何从数据库中获取数据以在视图页面中显示所选数据?

时间:2019-11-13 04:35:53

标签: php html sql

我刚刚创建了一个表单,我想根据我选择的内容从数据库中获取数据,然后将其显示在视图中。但是在我看来,输出没有显示任何记录。为此,我在编码中尝试了几件事:

前端:

<div class="box inverse">
    <div class="row">
        <div class="col-lg-12">
            <header>
                <h5>Search</h5>
            </header>
            <form id="transaction_search">
                <div class="form-group">
                    <div class="col-lg-12">
                        <div class="col-lg-3">
                        </div>
                        <div class="col-lg-12">
                            <div class="form-group">
                                <div class="col-lg-12">
                                    <label for="text1"
                                           class="form-group control-label col-lg-2"><?php echo $language['type']; ?>
                                        :</label>
                                    <div class="col-lg-5">
                                        <select id="select_type" class="form-group form-control required"">
                                        <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"><?php echo $language["register"]; ?></option>
                                        <option value="receive"><?php echo $language["receive"]; ?></option>
                                        </select>
                                    </div>
                                </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>
                    </div>
                </div>
                <div class="col-lg-12" style="text-align:center; padding-bottom:10px; padding-top:10px;">
                    <button id="search" type="button" class="btn btn-sm btn-primary"
                            onclick="search_('transaction_search','transaction_result','transaction_table')">Search
                    </button>
                    <button id="clear" type="button" class="btn btn-sm btn-default" onclick="clearData()">Clear</button>
                </div>
                <div class="body" id="transaction_result" style="overflow:auto;">

                </div><!--body-->
            </form>
        </div>
    </div>
</div>

后端:

<?php
foreach ($_POST as $key => $value) {
    $_POST[$key] = trim(preg_replace('/\s+/', ' ', ($value)));
}
$arr_val = $_POST;
$loc = $arr_val['loc'];
$action = $arr_val['action'];

unset($arr_val['loc']);
unset($arr_val['action']);

$tbl_name = 'withdrawal_record';
if ($action == 'search') {
    $name_2 = $_POST['select_type'];
    if ($_POST['select_type'] != '' || $_POST['start_date'] != '' || $_POST['end_date'] != '') {
        $sql = 'SELECT * FROM ' . $tbl_name . ' WHERE id is not null';

        if ($_POST['start_date'] != '' && $_POST['end_date'] != '') {
            // $sql .=' and a.created between "' . date('Y-m-d', strtotime($_POST['start_date'])) . '" and "' . date('Y-m-d', strtotime($_POST['end_date'])) . '"';
            // }

            $result_arr['sql'] = $sql;
            $result_arr = get_data_list($sql);
            $i = 1;
            echo '<table id="dataTable_1" class="dataTable table table-bordered table-condensed table-hover table-striped" style="padding:0px;" border="1">
                <thead>
                    <tr>
                         <th>No</th>
                         <th>Date</th>
                         <th>Amount</th>                
                    </tr>
                </thead>
                <tbody>';

            foreach ($result_arr as $rs_search) {
                echo "<tr>";
                echo "<td>" . $i++ . "</td>";
                echo "<td>" . $rs_search['created'] . "</td>";
                echo "<td>" . $rs_search['withdraw_amount'] . "</td>";
                echo '</td>';
                echo "</tr>";
            }
            echo '</tbody>';
            echo '</table>';
        }
    }
?>

脚本功能:

function search_(form_id, div_id, act_file) {
    var action = 'search';
    var extra = '&action=' + action;
    var serialized = $('#' + form_id).serialize();
    var form_data = serialized + extra;

    $.ajax({
        //dataType: 'json',
        type: 'POST',
        url: '?f=' + act_file,
        data: form_data,
        beforeSend: function () {
            show_overLay();
            $('#' + div_id).html('');
        },
        success: function (data) {
            hide_overLay('');
            if (data) {
                $("#" + div_id).append(data);
                $('.dataTable').dataTable({
                    pageLength: 25,
                    destroy: true
                });
            } else {
                hide_overLay("Please fill in the field.");
            }
            //console.log(data);
        }
    });
}

我的数据库信息,这是选择退出类型表的记录:

Database

然后我的输出显示在下面,我想选择“提款”的类型,并在2019-11-04和2019-11-06之间选择日期,但是输出没有显示数据:

Output

0 个答案:

没有答案