如何在PHP循环中通过jquery单独获取所有输出的动态类 [英] How to get dynamic class of all output individually in a PHP while loop through jquery

查看:55
本文介绍了如何在PHP循环中通过jquery单独获取所有输出的动态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我通过php while循环从数据库获取许多输出现在我在每个输出中有一个反应列现在当用户点击反应按钮我希望它改变相关的div的值只输出,但我的问题是,当我点击一个反应按钮,它改变所有div的值有反应的类2



这是我的代码

 $ query =SELECT ph.likes,ph.image_url,ph.email,ph.username,ph.uid,ph.id,ph.avatar_path 
FROM photos as ph
内部加入关注者作为fol
在fol.user_id = ph.uid
其中fol.uid ='$ id'
ORDER BY ph.image_url DESC;
$ fire = mysqli_query($ con,$ query)或die(无法从数据库中获取数据.mysqli_error($ con));
if(mysqli_num_rows($ fire)> 0){



while($ users = mysqli_fetch_assoc($ fire)){
$ likes = $ users ['likes'];
$ username = $ users ['username'];
$ uid = $ users ['uid'];
$ pixid = $ users ['id'];
$ avatar_path5 = $ users ['avatar_path'];


?>


< div class =all>
< div class =card>
< div class =float>
< div class =avatar>
< img src =<?php echo $ avatar_path5;?> width =100%class =avatar>
< / div>

< div class =usernamestyle =font-weight:600; size:14px; text-decoration:none;>
< p><?php echo< div>< a href ='users.php?id =。$ users ['uid']。'>
< h3>。$ users ['username']。< / h3>

< / div>< / a>; ?>< / p为H.
< / div>
< / div>

< img src =<?php echo $ users ['image_url']?> alt =Avatarstyle =width:682px;>

< div class =container>
< h4> <?php echo< div>< a href ='users.php?id =。$ users ['uid']。'>

< / div>< / a>;?>
< / h4>

< / div>
< span id =countclass =likes_count><?php echo $ users ['likes']; ?>喜欢< /跨度>
< div style =padding:2px; margin-top:5px;>
<?php
if(isset($ _ POST ['likes'])){
$ postid = $ _POST ['postid'];
$ result = mysqli_query($ con,SELECT * FROM photos WHERE id = $ postid)或die(mysqli_error($ con));
$ row = mysqli_fetch_array($ result)
或die(mysqli_error($ con));
$ n = $ row ['likes'];

mysqli_query($ con,INSERT INTO likes(user_id,username,post_id)VALUES($ id,'$ fullname',$ postid))或die(mysqli_error($ con));
mysqli_query($ con,UPDATE照片SET赞= $ n + 1 WHERE id = $ postid)或死(mysqli_error($ con));

echo $ n + 1;
exit();
}
if(isset($ _ POST ['unliked'])){
$ postid = $ _POST ['postid'];
$ result = mysqli_query($ con,SELECT * FROM photos WHERE id = $ postid)或die(mysqli_error($ con));
$ row = mysqli_fetch_array($ result)或die(mysqli_error($ con));
$ n = $ row ['likes'];

mysqli_query($ con,DELETE FROM喜欢WHERE post_id = $ postid AND user_id = $ id)或死(mysqli_error($ con));
mysqli_query($ con,UPDATE照片SET赞= $ n-1 WHERE id = $ postid)或死(mysqli_error($ con));

echo $ n-1;
exit();
}
?>
< / div>

< div>

<?php
//确定用户是否已经喜欢这篇文章
$ results = mysqli_query($ con,SELECT * FROM喜欢WHERE user_id = $ id AND post_id =。$ users ['id']。)或死亡(mysqli_error($ con));

if(mysqli_num_rows($ results)== 1):?>
<! - 用户已经喜欢发布 - >
< span class =不像fas fa-heart animated bounceIndata-id =<?php echo $ users ['id'];?>>< / span>
< span class =like hide far fa-heartonclick =PlaySound()data-id =<?php echo $ users ['id'];?>>< /跨度>
<?php else:?>
< ;! - 用户尚未喜欢发布 - >
< span class =like far fa-heartonclick =PlaySound()data-id =<?php echo $ users ['id'];?>>< /跨度>
< span class =不像hide fas fa-heart animated bounceIndata-id =<?php echo $ users ['id'];?>>< / span>
<?php endif?>
< a class =comstyle =color:#929292!important;href =show.php?post_id =<?php echo $ users ['id'];?>风格= > < span class =glyphicon glyphicon-comment trigger>< / span>条评论< / a>
< div class =winkid =ex2style =color:black;
width:30px;
height:30px;
margin-top:-20px ;
margin-left:209px;
> <?php
$ query2 =SELECT * FROM gormint where post_id = $ pixid and user_id = $ id3;
$ fire2 = mysqli_query($ con,$ query2)或die(无法从数据库中获取数据.mysqli_error($ con));
$ query3 =SELECT * FROM bhai where post_id = $ pixid and user_id = $ id3;
$ fire3 = mysqli_query($ con,$ query3)或die(无法从数据库中获取数据.mysqli_error($ con));
$ query4 =SELECT * FROM famer where post_id = $ pixid and user_id = $ id3;
$ fire4 = mysqli_query($ con,$ query4)或die(无法从数据库中获取数据.mysqli_error($ con));
$ query5 =SELECT * FROM muskan where post_id = $ pixid and user_id = $ id3;
$ fire5 = mysqli_query($ con,$ query5)或die(无法从数据库中获取数据.mysqli_error($ con));
if(mysqli_num_rows($ fire2)> 0){
echo< img src ='gormint.jpg'class ='gormint2'>;
} elseif(mysqli_num_rows($ fire3)> 0){
echo< img src ='bhai.jpg'class ='bhai2'>;
} elseif(mysqli_num_rows($ fire4)> 0){
echo< img src ='famer.jpg'class ='bhai2'>;
} elseif(mysqli_num_rows($ fire5)> 0){
echo< img src ='zakhir.jpg'class ='bhai2'>;
} else {
echo< img src ='wink.png'class ='wink2'>;
}



?>< / div>

< div class =flipClassid =flip> react< / div>

< div class =panelClassid =panel>
< input type =imageonclick =PlaySound2()id =displaydata-value =<?php echo $ users ['id'];?> src =gormint2.jpgclass =close2 display gormint animated bounceIn>
< input type =imageonclick =PlaySound3()data-value =<?php echo $ users ['id'];?> id =display2src =bhai.jpgclass =close2 display2 bhai animated bounceIn>
< input type =imageonclick =PlaySound4()data-value =<?php echo $ users ['id'];?> id =display3src =famer.jpgclass =close2 display3 bhai animated bounceIn>
< input type =imageonclick =PlaySound5()data-value =<?php echo $ users ['id'];?> id =display4src =zakhir.jpgclass =close2 display4 bhai animated bounceIn>
< / div>

< / div>
< / div>
<?php
}
}
?>





&这是我的剧本

 $(' 。display' )。点击( function (){
var value = $(< span class =code-keyword> this )。attr(' data-value');
$ .ajax({url: reaction.php?pollid = +值,缓存: false
成功:功能(结果){
$(' 。react2')。load(' img.php?id =<?php echo $ users [' id ' ];?>');

}});
});
$(' 。display2')。click( function (){
var value = $( this )。 attr(' data-value');
$ .ajax({url: reaction2.php?pollid = + value,cache: false
成功:功能(结果){
$('' .reaction2')。load(' bhai.php ?id =<?php echo $ users [' id ' ];?>');

}});
});
$(' 。display3')。click( function (){
var value = $( this )。 attr(' data-value');
$ .ajax({url: reaction3.php?pollid = + value,cache: false
成功:功能(结果){
$('' 。react2')。load(' famer.php ?id =<?php echo $ users [' id ' ];?>');
}});
});
$(' .display4')。click( function (){
var value = $( this )。 attr(' data-value');
$ .ajax({url: reaction4.php?pollid = + value,cache: false
成功:功能(结果){
$('' 。react2')。load(' zakhir.php ?id =<?php echo $ users [' id ' ];?>');

}});
});





什么我试过了:



i在论坛上搜索过他们建议我应该使用$(这)但我不明白如何在其中使用$(this)plzz帮助我

解决方案

query =SELECT ph.likes,ph.image_url,ph .email,ph.username,ph.uid,ph.id,ph.avatar_path
FROM照片作为ph
内部加入关注者,作为fol
on fol.user_id = ph.uid
其中fol.uid ='


id'
ORDER BY ph.image_url DESC;


fire = mysqli_query(

i have a project in which i am fetching many output from database through a php while loop now i have a reaction column in each output now when a user clicks on react button i want it to change the value of the div related to that output only but my problem is that when i click on a react button it changes values of all the div having class of react2

this is my code

$query =  "SELECT ph.likes, ph.image_url,ph.email,ph.username,ph.uid ,ph.id,ph.avatar_path
          FROM photos as ph
          inner join followers as fol
          on fol.user_id = ph.uid
          where fol.uid = '$id'
          ORDER BY ph.image_url DESC ";
$fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire)>0) {



  while ($users = mysqli_fetch_assoc($fire)) {
    $likes = $users['likes'];
    $username = $users['username'];
    $uid = $users['uid']; 
    $pixid = $users['id'];
    $avatar_path5 = $users['avatar_path'];


    ?>


<div class="all" >
<div class="card" >
  <div class="float" >
  <div class="avatar" >
    <img src="<?php echo $avatar_path5; ?>" width="100%" class="avatar">
  </div>

      <div class="username" style="font-weight: 600; size: 14px;  text-decoration: none;">
      <p><?php echo "<div><a href='users.php?id=".$users['uid']."'>
               <h3>".$users['username']."</h3>

      </div></a>"; ?></p>
</div>
</div>

  <img src="<?php echo $users['image_url']?>" alt="Avatar" style="width:682px;">

  <div class="container">
    <h4><?php echo "<div><a href='users.php?id=".$users['uid']."'>

      </div></a>";?></h4>

  </div>
 <span id="count" class="likes_count"><?php echo $users['likes']; ?> likes</span>
  <div style="padding: 2px; margin-top: 5px;">
  <?php
if (isset($_POST['liked'])) {
    $postid = $_POST['postid'];
    $result = mysqli_query($con, "SELECT * FROM photos WHERE id=$postid")or die(mysqli_error($con));
    $row = mysqli_fetch_array($result)
    or die(mysqli_error($con));
    $n = $row['likes'];

    mysqli_query($con, "INSERT INTO likes (user_id,username, post_id) VALUES ($id, '$fullname', $postid)")or die(mysqli_error($con));
    mysqli_query($con, "UPDATE photos SET likes=$n+1 WHERE id=$postid")or die(mysqli_error($con));

    echo $n+1;
    exit();
  }
  if (isset($_POST['unliked'])) {
    $postid = $_POST['postid'];
    $result = mysqli_query($con, "SELECT * FROM photos WHERE id=$postid")or die(mysqli_error($con));
    $row = mysqli_fetch_array($result)or die(mysqli_error($con));
    $n = $row['likes'];

    mysqli_query($con, "DELETE FROM likes WHERE post_id=$postid AND user_id=$id")or die(mysqli_error($con));
    mysqli_query($con, "UPDATE photos SET likes=$n-1 WHERE id=$postid")or die(mysqli_error($con));

    echo $n-1;
    exit();
  }
?>
          </div>      

      <div>

        <?php 
          // determine if user has already liked this post
           $results = mysqli_query($con, "SELECT * FROM likes WHERE user_id=$id AND post_id=".$users['id']."")or die(mysqli_error($con));

          if (mysqli_num_rows($results) == 1 ): ?>
            <!-- user already likes post -->
            <span class="unlike fas fa-heart animated bounceIn"   data-id="<?php echo $users['id']; ?>"></span> 
            <span class="like hide far fa-heart"    onclick="PlaySound()" data-id="<?php echo $users['id']; ?>"></span> 
          <?php else: ?>
            <!-- user has not yet liked post -->
            <span class="like far fa-heart"  onclick="PlaySound()" data-id="<?php echo $users['id']; ?>"></span> 
            <span class="unlike hide fas fa-heart animated bounceIn"   data-id="<?php echo $users['id']; ?>"></span> 
          <?php endif ?>
<a class="com"  style="color: #929292 !important; " href="show.php?post_id=<?php echo $users['id'];?>" style="">  <span class="glyphicon glyphicon-comment trigger" ></span>comments</a>
<div class="wink" id="ex2" style="  color: black;
    width: 30px;
    height: 30px;
    margin-top: -20px;
    margin-left: 209px;
" > <?php  
$query2 = "SELECT * FROM gormint where post_id = $pixid and user_id= $id3";
$fire2 = mysqli_query($con,$query2) or die("can not fetch data from database ".mysqli_error($con));
$query3 = "SELECT * FROM bhai where post_id = $pixid and user_id= $id3";
$fire3 = mysqli_query($con,$query3) or die("can not fetch data from database ".mysqli_error($con));
$query4 = "SELECT * FROM famer where post_id = $pixid and user_id= $id3";
$fire4 = mysqli_query($con,$query4) or die("can not fetch data from database ".mysqli_error($con));
$query5 = "SELECT * FROM muskan where post_id = $pixid and user_id= $id3";
$fire5 = mysqli_query($con,$query5) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire2)>0) {
  echo "<img src='gormint.jpg' class='gormint2' >";
}elseif (mysqli_num_rows($fire3)>0) {
  echo "<img src='bhai.jpg' class='bhai2'>";
}elseif (mysqli_num_rows($fire4)>0) {
  echo "<img src='famer.jpg' class='bhai2'>";
}elseif (mysqli_num_rows($fire5)>0) {
  echo "<img src='zakhir.jpg' class='bhai2'>";
} else{
  echo "<img src='wink.png' class='wink2'>";
}



?></div>

<div class="flipClass" id="flip">react</div>

<div class="panelClass" id="panel"> 
  <input type="image" onclick="PlaySound2()" id="display" data-value="<?php echo $users['id'];?>"  src="gormint2.jpg" class="close2 display gormint animated bounceIn " >
              <input type="image" onclick="PlaySound3()" data-value="<?php echo $users['id'];?>" id="display2" src="bhai.jpg" class="close2 display2 bhai animated bounceIn">
              <input type="image" onclick="PlaySound4()" data-value="<?php echo $users['id'];?>" id="display3" src="famer.jpg" class="close2 display3 bhai animated bounceIn">
              <input type="image" onclick="PlaySound5()" data-value="<?php echo $users['id'];?>" id="display4" src="zakhir.jpg" class="close2 display4 bhai animated bounceIn">
              </div>

        </div>
      </div>
<?php
}
}
?>



& this is my script

$('.display').click(function(){
    var value=$(this).attr('data-value');
    $.ajax({url:"reaction.php?pollid="+value,cache:false,
      success:function(result){
        $('.react2').load('img.php?id=<?php echo $users['id'];?>');

    }});
}); 
  $('.display2').click(function(){
    var value=$(this).attr('data-value');
    $.ajax({url:"reaction2.php?pollid="+value,cache:false,
      success:function(result){
       $('.react2').load('bhai.php?id=<?php echo $users['id'];?>');

    }});
});
 $('.display3').click(function(){
    var value=$(this).attr('data-value');
    $.ajax({url:"reaction3.php?pollid="+value,cache:false,
      success:function(result){
   $('.react2').load('famer.php?id=<?php echo $users['id'];?>');
}});
});
 $('.display4').click(function(){
    var value=$(this).attr('data-value');
    $.ajax({url:"reaction4.php?pollid="+value,cache:false,
      success:function(result){
$('.react2').load('zakhir.php?id=<?php echo $users['id'];?>');

    }});
});



What I have tried:

i have searched on forums there they suggested that i should use $(this) but i dont understand how to use $(this) in it plzz help me

解决方案

query = "SELECT ph.likes, ph.image_url,ph.email,ph.username,ph.uid ,ph.id,ph.avatar_path FROM photos as ph inner join followers as fol on fol.user_id = ph.uid where fol.uid = '


id' ORDER BY ph.image_url DESC ";


fire = mysqli_query(


这篇关于如何在PHP循环中通过jquery单独获取所有输出的动态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆