在日期范围之间搜索数据并在预定义日期范围之间添加数据

时间:2015-07-18 07:15:31

标签: javascript php mysql fifo stock

我正在尝试构建一个搜索应用程序,人们可以根据日期范围查看数据,但我也想添加预定义的日期范围,并将此结果添加到最终输出。

我一直在寻找解决方案,但我没弄明白该怎么做。我想我一定是错过了什么。有谁愿意为我分享知识?

仅供参考我是编码方面的新手。

这是我的表格:

    <table id="tbl_rekapbeli" class="easyui-datagrid" style="width:900px;height:360px"
        url="get_lap_beli2.php" pagination="true" idField="id"
        title="Rekap Pembelian Berkala" toolbar="#tb"
        singleSelect="true" fitColumns="true" showFooter="true">
    <thead>
            <th field="no_po" width="50" editor="{type:'validatebox',options:{required:true}}">No PO</th>
            <th field="tgl_po" width="50" editor="{type:'datebox',options:{required:true}}">Tgl PO</th>
            <th field="nama_outlet" width="50" editor="{type:'validatebox',options:{required:true}}">Customer</th>
            <th field="kode_barang" width="50" editor="{type:'validatebox',options:{required:true}}">Kode Barang</th>
            <th field="nama_barang" width="90" editor="{type:'validatebox',options:{required:true}}">Nama Barang</th>
            <th field="qty_beli" width="50" editor="{type:'validatebox',options:{required:true}}">Jumlah beli</th>
            <th field="harga_beli" width="50"  editor="{type:'validatebox',options:{required:true}}">Harga beli</th>
            <th field="jum_harga_beli" width="50" formatter="formatrp" editor="{type:'numberbox'}">Total</th>               
        </tr>
    </thead>
</table>

<div id="tb" style="padding:5px;height:auto">
    <div>
        Date From : <input id="date1" class="easyui-datebox" style="width:80px">&nbsp;&nbsp;
        To : <input id="date2" class="easyui-datebox" style="width:80px">&nbsp;&nbsp;
        <a href="#" id="aaa" class="easyui-linkbutton" iconCls="icon-search">Cari</a>
    </div>
</div>

功能脚本:

var d1 = 0
var d2 = 0

$('#date1').datebox({
    onSelect: function (date) {
        d1 = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
    }
})

$('#date2').datebox({
    onSelect: function (date) {
        d2 = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
    }
})

$('#aaa').click(function () {
    $('#tbl_rekapbeli').datagrid('options').url = "get_lap_beli2.php?start_date=" + d1 + "&end_date=" + d2;
    $('#tbl_rekapbeli').datagrid('reload');
    //alert(“dari: ” + d1 + ” sampai: ” + d2);
})

function formatrp(val, row) {
    return number_format(val, '', ',', '.');
};

function number_format(num, dig, dec, sep) {
    x = new Array();
    s = (num < 0 ? "-" : "");
    num = Math.abs(num).toFixed(dig).split(".");
    r = num[0].split("").reverse();
    for (var i = 1; i <= r.length; i++) {
        x.unshift(r[i - 1]);
        if (i % 3 == 0 && i != r.length) x.unshift(sep);
    }
    return "Rp " + s + x.join("") + (num[1] ? dec + num[1] : "");
}

并采取行动:

$begin_date = '2015-07-01';
$start_date = $_REQUEST['start_date'];
$end_date = $_REQUEST['end_date'];

include '../../libs/conn.php';

$rs = mysql_query("SELECT count(*) FROM beli WHERE tgl_po between '$begin_date' and '$start_date' AND tgl_po between '$start_date' and '$end_date'");
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];

$rs = mysql_query("SELECT * FROM beli WHERE tgl_po between '$begin_date' and '$start_date' AND tgl_po between '$start_date' and '$end_date'");

$items = array();
while($row = mysql_fetch_object($rs)){
array_push($items, $row);
}
$result["rows"] = $items;

$rs = mysql_query("SELECT SUM(jum_harga_beli) AS value_sum FROM beli WHERE tgl_po between '$begin_date' and '$start_date' AND tgl_po between '$start_date' and '$end_date'");
$rw = mysql_fetch_assoc($rs);
$sum = $rw['value_sum'];

$result["footer"]=array(array("harga_beli" => "Total Pembelian",'jum_harga_beli' => $sum));

echo json_encode($result);

1 个答案:

答案 0 :(得分:0)

您的查询已

  AND tgl_po between '$begin_date' and '$start_date'
  AND tgl_po between '$start_date' and '$end_date'

SQL的工作方式,您需要OR而不是AND

  AND    (    tgl_po between '$begin_date' and '$start_date'
           OR tgl_po between '$start_date' and '$end_date')

如果你在where子句中提到AND,它只接受符合两个条件的记录。如果您希望它与 条件匹配,则需要OR

您希望行匹配第一个日期范围或第二个日期范围。因此,您必须指定OR,而不是AND。在SQL查询中,AND缩小选择范围并OR扩大选择范围。