简体   繁体   English

为 PhP 中的 for 循环中的每个元素赋予不同的 ID

[英]Give different ID to each element in for loop in PhP

This is my output, clicking like button should add +1 to the likes column in MySQL .这是我的 output,单击“赞”按钮应在 MySQL 的“赞”列中添加 +1

I use a while loop to iterate buttons我使用 while 循环来迭代按钮

For example, the "button" is displayed multiple times in the picture below.例如,“按钮”在下图中多次显示。 I have a tag in the while loop, so it outputs the button several times.我在while循环中有一个标签,所以它会多次输出按钮。 And that name comes from the database.这个名字来自数据库。

MY Question .我的问题 All the buttons in the will have the same ID.中的所有按钮都将具有相同的 ID。 Currently, the user can only click the first button.目前,用户只能点击第一个按钮。 I would like to give each element a different ID if possible.如果可能的话,我想给每个元素一个不同的 ID。 Then I would like to use jQuery to add a click event.然后我想用 jQuery 添加点击事件。 So, if I click on the 4th button, the like count for that comment should be increased.所以,如果我点击第四个按钮,该评论的点赞数应该会增加。

What I need.我需要的。 How I can assign a different ID to each element in the far loop, so it does only make the first Image clickable but instead all elements' clickables?如何为远循环中的每个元素分配不同的 ID,这样它只会使第一个图像可点击,而是所有元素的可点击?

<?php
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        ?>
        <div class="single-item">
             <div class="cmt_user"><div class="circle2">
                  <h5>
                     <?php
                         $name = $row['name'];
                         $f_letter = strtoupper($name[0]);
                         echo $f_letter; 
                      ?>
                  </h5>
                  </div>
                      <h4><a href=""><?php echo $row['name']; ?></a></h4>
                  </div>
                       <p><?php echo $row['comment']; ?></p><div class="lcr">
                       <ul>
                           <li id = "modified" >  </li>
                       </ul>
                       <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" class="form">
                           <button onclick="imageClick(<?php echo $row['id_vnr']; ?>)" name="like" value="<?php echo $row['id'] ?>" class="like_btn"><i class="fa fa-heart"></i> Like</button>
                       </form>

this is Php Code to insert likes count这是插入喜欢计数的 Php 代码

<?php
     $like = $_POST['like'];
     if($like){
            $sql = "UPDATE comments set likes = likes + 1 where id = '".$row['id']."'"; 
            echo $sql;
            $result=mysqli_query($link,$sql);
     }
         
?>

It's really simple.这真的很简单。 do it like this像这样做

$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        ?>
        <div class="single-item">
             <div class="cmt_user"><div class="circle2">
                  <h5>
                     <?php
                         $name = $row['name'];
                         $f_letter = strtoupper($name[0]);
                         echo $f_letter; 
                      ?>
                  </h5>
                  </div>
                      <h4><a href=""><?php echo $row['name']; ?></a></h4>
                  </div>
                       <p><?php echo $row['comment']; ?></p><div class="lcr">
                       <ul>
                           <li id = "modified" >  </li>
                       </ul>
                       <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" class="form">
                            <input name="like" value="1"> 
                            <input name="row_id" value="<?php echo $row['id'] ?>"> 
                           <button class="like_btn"><i class="fa fa-heart"></i> Like</button>
                       </form>

And get POST values like this并获得这样的 POST 值

<?php
     $like = $_POST['like'];
     $row_id = $_POST['row_id'];
     if(isset($like) && $like == 1){
            $sql = "UPDATE comments set likes = likes + 1 where id = '".$row_id."'"; 
            echo $sql;
            $result=mysqli_query($link,$sql);
     }
         
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM