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条回答 默认 最新

  • dongtang5057 2017-04-10 11:49
    关注

    You can do it simply modifying you ajax function input value

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP