在此代码中,单击like按钮后,数据已添加到数据库中。我现在要做的是在添加数据后,我想查询所选项目的总数,并在不加载页面的情况下显示它。
这是我现在的代码:
我的观点:
<p id='state'><i class='fa fa-thumbs-up'></i><span id="likeThis"><?php echo $countLike;?></span> likes • <i class='fa fa-thumbs-down'></i><?php echo $countDisLike;?> dislikes •<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:
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();
}
此代码不会显示total_likes。这个怎么了?
答案 0 :(得分:1)
这看起来像是一个不错的代码
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);
}
只需一次尝试..改变你的
返回json_encode($ result)
到
echo json_encode($ result)
此示例可能会在将来帮助您jquery ajax php example
答案 1 :(得分:1)
After all the helps and research. This is the running code of this problem.
In the View:
{!!\App\Library\TableHelper::colorText($product->number)!!}
Javascript:
<?php namespace App\Library;
class TableHelper
{
public static function colorText($text)
{
switch ($text)
{
case '1' : $text = '<span color="green"> ' . $text . '</span>'; break;
//...
}
return $text;
}
//to only get the color:
public static function getColor($text)
{
switch ($text)
{
case '1' : return 'green' ; break;
//...
default: return 'white';
}
}
}
Controller:
<p id='state'><i class='fa fa-thumbs-up'></i><span class="likeThis"><?php echo $countLike;?></span> likes • <i class='fa fa-thumbs-down'></i><?php echo $countDisLike;?> dislikes •<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>
Model:
<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>
This code runs perfectly now. Thanks for the help anyway.
答案 2 :(得分:0)
试试这个。
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']);
$no_likes = $this->photoCheese_model->get_like_total($data,$upload);
$result['no_likes'] = $no_likes;
$this->output->set_content_type('application/json')->set_output(json_encode($result));
}
public function get_like_total($data,$uplaod){
$success = $this->db->insert('tbl_like',$data);
if($success){
$this->db->select('SUM(`like`) as no_likes');
$this->db->where('uploadID',$upload);
$query = $this->db->get('tbl_like');
$res = $query->row_array();
return $res['no_likes'];
}
return 0;
}
答案 3 :(得分:0)
public function like_total(){
$data = array('like' => 1,
'userID'=>$this->session->userdata('userID'),
'uploadID'=>$_GET['like_id']);
$no_likes = $this->photoCheese_model->get_like_total($data,$this->input->get('like_id'));
$result['no_likes'] = $no_likes;
$this->output->set_content_type('application/json')->set_output(json_encode($result));
}
public function get_like_total($data,$this->input->get('like_id')){
$success = $this->db->insert('tbl_like',$data);
if($success){
$this->db->select('SUM(`like`) as no_likes');
$this->db->where('uploadID',$this->input->get('like_id'));
$query = $this->db->get('tbl_like');
$res = $query->row_array();
return $res['no_likes'];
}
return 0;
}
答案 4 :(得分:-1)
public function article_search($search){
$word = explode(' ',$search);
$query = "select c.*,d.name,d.image_link from tbl_product as d inner join tbl_order as c on d.id=c.product_id
where 1 and ";
$i=1;
foreach($word as $wd){
if($i==1){
$query.= "( c.customer like '%".$wd."%'";
}else{
$query.= " or c.customer like '%".$wd."%'";
}
$i++;
}
foreach($word as $wd){
if($i==1){
$query.= "( c.order_no like '%".$wd."%'";
}else{
$query.= " or c.order_no like '%".$wd."%'";
}
$i++;
}
foreach($word as $wd){
if($i==1){
$query.= "( d.name like '%".$wd."%'";
}else{
$query.= " or d.name like '%".$wd."%'";
}
$i++;
}
foreach($word as $wd){
if($i==1){
$query.= "( d.commission like '%".$wd."%'";
}else{
$query.= " or d.commission like '%".$wd."%'";
}
$i++;
}
foreach($word as $wd){
if($i==1){
$query.= "( d.base_price like '%".$wd."%'";
}else{
$query.= " or d.base_price like '%".$wd."%'";
}
$i++;
}
$query.= " or c.customer like '%".$search."%'";
$query.= " or c.order_no like '%".$search."%' ";
$query.= " or d.name like '%".$search."%' ";
$query.= " or d.commission like '%".$search."%' ";
$query.= " or d.base_price like '%".$search."%' )";
$query = $this->db->query($query);
//echo $this->db->last_query();die;
$record = $query->result_array();
foreach($record as $key=>$value){
$cat = $value['product_id'];
$query_tag = $this->db->query("select * from tbl_product where id in(".$cat.")");
$record[$key]['all_pro'] = $query_tag->result_array();
}
return $record;
}