使用PHP和MongoDB获取记录计数

时间:2013-11-18 14:29:48

标签: mongodb nosql

我有一个带有部门选择框的HTML页面。当最终用户选择任何部门时,我没有向他展示该部门的雇员。员工收集

<select multiple="" class="chosen-select choice" id="form-field-select-4" data-placeholder="Choose a Department..." onchange="show_employee_count(this.value)">
<option value="Management">Senior Management</option>
<option value="Legal">Legal</option>
<option value="Operations">Operations</option>
<option value="Administration">Administration</option>
<option value="R&D">R&D</option>
<option value="Sales & Marketing">Sales & Marketing</option>
<option value="Finance">Finance</option>
<option value="Technology">Technology</option>
<option value="Customer Service">Customer Service</option>
<option value="Pro Services">Pro Services</option>
<option value="HR">HR</option>
</select>

要在此容器div中显示的员工数

<div id="emp_count"></div>

1 个答案:

答案 0 :(得分:0)

我试过这个:

HTML

<select name='department' id='department'>
<option value="Management">Senior Management</option>
<option value="Legal">Legal</option>
<option value="Operations">Operations</option>
<option value="Administration">Administration</option>
<option value="R&D">R&D</option>
<option value="Sales & Marketing">Sales & Marketing</option>
<option value="Finance">Finance</option>
<option value="Technology">Technology</option>
<option value="Customer Service">Customer Service</option>
<option value="Pro Services">Pro Services</option>
<option value="HR">HR</option>
</select>
<div id="leads_coount"></div>

JS代码

<script type="text/javascript">
        $(function(){
            $("#department").change(function(){
                var selectedDepartment = $(this).find(":selected").val();
                console.log("the department you selected: " + selectedDepartment);

                $.ajax({
                    type: "POST",
                    url: "ajax.php",
                    cache: false,
                    data: { method: "get_lead_count", department: selectedDepartment }
                })
                .done(function( leads ) {
                    $( "#leads_coount" ).html(leads);
                }).fail(function( jqXHR, textStatus ) {
                    alert( "Request failed: " + textStatus );
                });

            });
        });
        </script>

ajax.php

<?php require_once('DataAccessBean.class.php');
if($_POST['method'] == 'get_lead_count') {
    // select a collection
    $collection = new MongoCollection($db, 'employs');
    // find the collections with department
    $clause = array('Department' => $_POST['department']);
    $cursor = $collection->find($clause)->count();
    echo 'There are '.$cursor.' employees available in this department';
    //echo $_POST['department'];
}

有更好的方法吗?