如何使用codeigniter

时间:2016-08-28 17:27:39

标签: javascript php mysql ajax codeigniter-3

我在这里不包括表单输入Etc.因为我的代码工作正常。 我希望像这个查询的表单验证是选择交付表,如果“车辆1”出现在数据库上5次并且在同一交货日期。警告车辆1需要在该日期仅交付5次的错误。“

数据库:
      vehicle_name | delivery_date
      车辆1 8/29/2016
      车辆1 8/29/2016
      车辆1 8/29/2016
      车辆1 8/29/2016
      车辆1 8/29/2016

在6日输入        输入:车辆1
       输入:8/29/2016

我的输入没有问题。我只想要一个验证,如果在2016年8月29日在交货台上存在5次“车辆1”,则无法再添加任何交货。我将如何编码?“

控制器

private function _validate()
{
    $data = array();
    $data['error_string'] = array();
    $data['inputerror'] = array();
    $data['status'] = TRUE;

    if($this->input->post('client_firstname') == '')
    {
        $data['inputerror'][] = 'client_firstname';
        $data['error_string'][] = 'First name is required';
        $data['status'] = FALSE;
    }

    if($this->input->post('client_lastname') == '')
    {
        $data['inputerror'][] = 'client_lastname';
        $data['error_string'][] = 'Last name is required';
        $data['status'] = FALSE;
    }

    if($this->input->post('client_contact') == '')
    {
        $data['inputerror'][] = 'client_contact';
        $data['error_string'][] = 'Customer contact is required';
        $data['status'] = FALSE;
    }

    if($this->input->post('client_address') == '')
    {
        $data['inputerror'][] = 'client_address';
        $data['error_string'][] = 'Address is required';
        $data['status'] = FALSE;
    }

    if($this->input->post('tracking_no') == '')
    {
        $data['inputerror'][] = 'tracking_no';
        $data['error_string'][] = 'Tracking number is required';
        $data['status'] = FALSE;
    }

    if($this->input->post('vehicle_id') == '')
    {
        $data['inputerror'][] = 'vehicle_id';
        $data['error_string'][] = 'Vehicle Assignation Required';
        $data['status'] = FALSE;
    }


    if($this->input->post('delivery_status') == '')
    {
        $data['inputerror'][] = 'delivery_status';
        $data['error_string'][] = 'Delivery status is required';
        $data['status'] = FALSE;
    }


    if($this->input->post('delivery_date') == '')
    {
        $data['inputerror'][] = 'delivery_date';
        $data['error_string'][] = 'Delivery Date is required';
        $data['status'] = FALSE;
    }


    if($data['status'] === FALSE)
    {
        echo json_encode($data);
        exit();
    }
}

模型

public function save($data)
{
    $this->db->insert($this->table, $data);
    return $this->db->insert_id();
}

在我的剧本

function add_delivery()
{
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
$('#modal_form').modal('show'); // show bootstrap modal
$('.modal-title').text('Add Delivery'); // Set Title to Bootstrap modal title
}

function save()
{
$('#btnSave').text('saving...'); //change button text
$('#btnSave').attr('disabled',true); //set button disable 
var url;

if(save_method == 'add') {
    url = "<?php echo base_url('index.php/delivery/ajax_add')?>";
    $('#btnSave').text('Add Delivery');
} else {
    url = "<?php echo base_url('index.php/delivery/ajax_update')?>";
}

// ajax adding data to database
$.ajax({
    url : url,
    type: "POST",
    data: $('#form').serialize(),
    dataType: "JSON",
    success: function(data)
    {

        if(data.status) //if success close modal and reload ajax table
        {
            $('#modal_form').modal('hide');
            reload_table();
            alert('Successfully Added');
        }
        else
        {
            for (var i = 0; i < data.inputerror.length; i++) 
            {
                $('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
                $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
            }
        }
        $('#btnSave').text('Save Delivery'); //change button text
        $('#btnSave').attr('disabled',false); //set button enable 


    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Error adding / update data');
        $('#btnSave').text('Save Delivery'); //change button text
        $('#btnSave').attr('disabled',false); //set button enable 

    }
});

}

1 个答案:

答案 0 :(得分:1)

在模型中

function getVeichleCount($vehicle_name, $delivery_date)
{
    $query = $this->db->query("SELECT count(id) FROM table_name WHERE vehicle_name = '$vehicle_name' AND delivery_date = $delivery_date ");
    $result = $query->result_array();
    return $result;
}

在回调功能中

$count = $this->model_name->getVeichleCount($vehicle_name, $delivery_date)[0];
if ($count >= 5) {
    # count already have 5 records
    # your error goes here
}