简体   繁体   English

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

[英]Codeigniter : How to insert last uri segment into database using ajax

I am creating a rating system in which when user rate any company then rate stored into rate along with the v_id table. 我正在创建一个评估系统,在该系统中,当用户对任何公司进行评估时,然后将比率与v_id表一起存储到比率中。 (v_id is company id), (v_id是公司ID),

This is my url in which i want to rate... 这是我要评价的网址...

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

Here company_id is getting from database. 这是company_id从数据库获取的信息。 I want to store the company rating into rate table. 我想将公司评级存储到费率表中。 when user click on the star. 当用户点击星标时。

Controller 控制者

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');

    }

Views This is ajax part in which i am sending the star values to the controller 视图这是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();
        }
    });

}

Star Controller To store Rate into data Here i am trying to get the company id from url and store into column(v_id) rate table. 星形控制器将费率存储到数据中在这里,我试图从url获取公司ID并将其存储到column(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);
        }

Model 模型

function ratings($value) {

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

You can do it simply modifying you ajax function input value 您只需修改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();
        }
    });
}

Again you can catch the company ID in your controller Function Hotels/ratings. 同样,您可以在控制器功能酒店/等级中获取公司ID。

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');

}

If you pass click value in url then ajax script should be like this: 如果您在url中传递点击值,那么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();
        }
    });

}

Because you are not passing click value in url so controller should be like that: 因为您没有在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);
}

And controller: 和控制器:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM