drl6054
2017-04-10 11:17
采纳率: 0%
浏览 61
已采纳

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

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 is company id),

This is my url in which i want to rate...

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

Here company_id is getting from database. 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

$(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.

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);
        }
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题