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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化