显示动态使用Ajax数据动态、数据、Ajax

2023-09-10 14:15:36 作者:繁华落尽╮散一地浮殇

在此code,点击等按钮后,该数据在数据库中添加了。我想现在要做的是,我想加入数据后,我就总喜欢询问所选择的项目,并且无需加载页面显示。

In this code, after clicking the like button, the data is added in the database already. What I wanted to do now is I would like after adding the data, I will query the total like of the chosen item and display it without loading the page.

这是我的code现在:

This is my code for now:

我的观点:

<p id='state'><i class='fa fa-thumbs-up'></i><span id="likeThis"><?php echo $countLike;?></span> likes &bull; <i class='fa fa-thumbs-down'></i><?php echo $countDisLike;?> dislikes &bull;<i class='fa fa-thumbs-down'></i><a href='<?php echo base_url();?>index.php/photoCheese/deleteUploadPic/<?php echo $row['uploadID'];?>'>Delete Picture</a></p>
 <input type="button" onclick="getVal(this.value)" class='detailButton1' name='like_name' id='like_id' value='<?php echo $link;?>' title='Like this post'><i class='fa fa-thumbs-up'></i> Like</input>

JavaScript的:

Javascript:

函数getVal(值)   {     jQuery.ajax({       键入:GET,       网址:?&LT; PHP的回声BASE_URL();&GT;的index.php / photoCheese / like_total /,       数据类型:JSON,       数据:{like_id:值},       成功:函数(RES){         警报(res.no_likes);         如果(RES){           jQuery的(#likeThis)的HTML(res.no_likes)。         }       }     });

function getVal(value) { jQuery.ajax({ type:"GET", url: "<?php echo base_url();?>index.php/photoCheese/like_total/", dataType:'json', data: {like_id : value}, success: function(res){ alert(res.no_likes); if(res){ jQuery("#likeThis").html(res.no_likes); } } });

控制器:

public function like_total(){
        $id = $this->session->userdata('userID');
        $upload = $this->input->get('like_id');
        $data = array('like' => 1,
                        'userID'=>$id,
                        'uploadID'=>$_GET['like_id']);

        $result = $this->photoCheese_model->get_like_total($data,$upload);


        return json_encode($result);
    }

型号:

public function get_like_total($data,$uplaod){
        $success = $this->db->insert('tbl_like',$data);

        if($success){
            $this->db->select('uploadID,SUM(`like`) as no_likes',false);
            $this->db->where('uploadID',$upload);
            $this->db->where('like !=',2);

            $query = $this->db->get();


        }
        return $query->result_array();
    }

这code将不会显示total_likes。有什么不对的呢?

This code will not display the total_likes. What's wrong with this one?

推荐答案

在所有的帮助和研究。这是此问题的运行code

After all the helps and research. This is the running code of this problem.

在查看:

<p id='state'><i class='fa fa-thumbs-up'></i><span class="likeThis"><?php echo $countLike;?></span> likes &bull; <i class='fa fa-thumbs-down'></i><?php echo $countDisLike;?> dislikes &bull;<i class='fa fa-thumbs-down'></i><a href='<?php echo base_url();?>index.php/photoCheese/deleteUploadPic/<?php echo $row['uploadID'];?>'>Delete Picture</a></p>
<input type="button" onclick="getVal(this.value)" class='detailButton1' name='like_name' id='like_id' value='<?php echo $link;?>' title='Like this post'><i class='fa fa-thumbs-up'></i> Like</input>

JavaScript的:

Javascript:

&LT;脚本类型=文/ JavaScript的&GT;     功能getVal(值)     {       jQuery.ajax({         键入:GET,         网址:?&LT; PHP的回声BASE_URL();&GT;的index.php / photoCheese / like_total /,         数据类型:JSON,         数据:{like_id:值},         错误:函数(结果){               $('likeThis。')追加。('&LT; P&GT;再见了世界&LT; / P&GT;');               },         成功:函数(结果){           jQuery的(likeThis。)HTML(结果)。         }       });     }  &LT; / SCRIPT&GT;

<script type="text/javascript"> function getVal(value) { jQuery.ajax({ type:"GET", url: "<?php echo base_url();?>index.php/photoCheese/like_total/", dataType:'json', data: {like_id : value}, error: function(result){ $('.likeThis').append('<p>goodbye world</p>'); }, success: function(result){ jQuery(".likeThis").html(result); } }); } </script>

控制器:

public function like_total(){
        $id = $this->session->userdata('userID');
        $upload = $this->input->get('like_id');
        $data = array('like' => 1,
                        'userID'=>$id,
                        'uploadID'=>$_GET['like_id']);

        $result = $this->photoCheese_model->get_like_total($data,$upload);

        $this->output->set_content_type('application/json');
        $this->output->set_output(json_encode($result));

        return $result;
    }

型号:

public function get_like_total($data,$upload){
        $success = $this->db->insert('tbl_like',$data);

        //Query the total likes
        if($success){
            $this->db->select()->from('tbl_like');
            $this->db->where('uploadID',$upload);
            $this->db->where('like !=',2);
            $query = $this->db->get();

            return $query->num_rows();
        }

        return 0;       
    }

这code现在完美运行。感谢您的帮助反正。

This code runs perfectly now. Thanks for the help anyway.