Codeigniter:如何使用ajax将最后一个uri段插入数据库

时间:2017-04-10 11:17:32

标签: php jquery mysql ajax codeigniter

我正在创建一个评级系统,当用户对任何公司进行评级时,然后将速率与v_id表一起存储到速率中。 (v_id是公司ID),

这是我想要评分的网址

  • www.ABC.com/controller/function/company_id

这里company_id来自数据库。我想将公司评级存入费率表。当用户点击星标时。

控制器

function visa_company_profile($v_id) {

        $data['total_ratings'] = $this->Visa_mdl->total_ratings($v_id); 
        $data['total_average'] = $this->Visa_mdl->total_average($v_id); 

        $result = $this->Visa_mdl->get_company_profile($v_id);
        $data['items_company_profile'] = $result;

        $this->load->view('include/header');
        $this->load->view('hotels/company_profile',$data);
        $this->load->view('include/footer');

    }

视图 这是我将星值发送给控制器的ajax部分

$(document).ready(function(){


var click_val = 0;

$("#1_star").hover(function(){

    $("#1_star").attr("src","<?php echo base_url('assets/rating/star.png'); ?>");
    $("#2_star").attr("src","<?php echo base_url('assets/rating/blank_star.png'); ?>");
    $('#3_star').attr('src',"<?php echo base_url('assets/rating/blank_star.png'); ?>");
    $('#4_star').attr('src',"<?php echo base_url('assets/rating/blank_star.png'); ?>");
    $('#5_star').attr('src',"<?php echo base_url('assets/rating/blank_star.png'); ?>");
});

$("#1_star").click(function(){
    click_val = 1;

    $.ajax({
        url: '<?php echo base_url('Account/loggedin');?>',
        success: function(logged_in) {
            if (logged_in === "1") {
                ajaxCall();

            }else {
                $("#l_modal").modal('show');
            }
        }
    });

});
function ajaxCall() {
    $.ajax({
        method : 'POST',
        data: {'click_val':click_val},
        url: '<?php echo base_url('Hotels/ratings/');?>',
        success: function() {
            location.reload();
        }
    });

}

星形控制器将费率存储到数据中 在这里,我试图从url获取公司ID并存储到列(v_id)费率表中。

function ratings() {
            date_default_timezone_set('Asia/Kolkata');
            $last = $this->uri->total_segments();
            $record_num = $this->uri->segment($last);
            $value = array (
                'rate' => $this->input->post('click_val'),
                'date' => date('Y-m-d H:i:s'),
                'v_id' => $record_num
                );
            $this->Visa_mdl->ratings($value);
        }

模型

function ratings($value) {

            $this->db->insert('user_ratings',$value);
        }

2 个答案:

答案 0 :(得分:4)

您只需修改ajax函数输入值

即可
function ajaxCall() {
    $.ajax({
        method : 'POST',
        data: {'click_val':click_val,'company_id':<?php echo $this->uri->segment(3)},
        url: '<?php echo base_url('Hotels/ratings/');?>',
        success: function() {
            location.reload();
        }
    });
}

您可以再次在控制器功能酒店/评级中捕获公司ID。

答案 1 :(得分:0)

function visa_company_profile($v_id) {

        $data['v_id'] = $v_id;
        $data['total_ratings'] = $this->Visa_mdl->total_ratings($v_id); 
        $data['total_average'] = $this->Visa_mdl->total_average($v_id); 

        $result = $this->Visa_mdl->get_company_profile($v_id);
        $data['items_company_profile'] = $result;

        $this->load->view('include/header');
        $this->load->view('hotels/company_profile',$data);
        $this->load->view('include/footer');

}

如果你在url中传递了click值,那么ajax脚本应该是这样的:

function ajaxCall() {
    var company_id = '<?php echo $v_id; ?>';
    $.ajax({
        method : 'POST',
        data: {'click_val':click_val, 'company_id':company_id},
        url: '<?php echo base_url('Hotels/ratings/');?>'+click_val+'/'+company_id,
        success: function() {
            location.reload();
        }
    });

}

因为你没有在url中传递点击值所以控制器应该是这样的:

function ratings() {
    date_default_timezone_set('Asia/Kolkata');
    $record_num = $this->input->post('company_id', true);
    $value = array (
        'rate' => $this->input->post('click_val', true),
        'date' => date('Y-m-d H:i:s'),
        'v_id' => $record_num
        );
    $this->Visa_mdl->ratings($value);
}

和控制器:

function ratings($record_num = 0, $company_id = 0) {
    date_default_timezone_set('Asia/Kolkata');

    $value = array (
        'rate' => $record_num,
        'date' => date('Y-m-d H:i:s'),
        'v_id' => $company_id
        );
    $this->Visa_mdl->ratings($value);
}