php 中的 foreach 对所有图片都一视同仁

foreach in php is treating all pictures the same

我做了一个foreach循环,但是我只能投第一部电影post。如果我尝试为其他图像投票,则更改不会反映出来,我只能看到对第一个 post.

所做的更改
   <?php foreach($imgsqlResults as $imgre): ?>
      <?php 
      $sql2 =  "SELECT * FROM posts";
      $result2  =  mysqli_query($conn, $sql2);
      while ($rows = mysqli_fetch_assoc($result2)) {
       $postid        = $rows['post_id'];
       $post_owner    = $rows['user_unam'];
       $post_pic_path = $rows['pic_path'];
      }
      ?>
             <div class='postsContainer' id='<?php echo $postid ?>'>
                <div class='profile-cont'>
                    <div class='who-post'>
                    <div class='who-post-name'><a href='$imgrefrence'>Marwan Mason</a></div>
                    <div class='who-post-img'><img src='photos/<?php echo $imgre['pic_path'] ?>'></div>
                  </div>

                       <div class='img-ctn'><img src='photos/<?php echo $imgre['pic_path'] ?>'></div>
                        <div class='star-rating'>
                    <form method='POST' >
                    <input type='h' name='imgId<?php echo $imgre['id'];?>' value="<?php echo $imgre['id'];?>"> 
                    <input id='star-1' type='radio' name='rating' value='star-5'>
                    <label for='star-1' title='Perfection'></label>
                    <input id='star-2' type='radio' name='rating' value='star-4'>
                    <label for='star-2' title='Amazing'></label>
                    <input id='star-3' type='radio' name='rating' value='star-3'>
                    <label for='star-3' title='wow'></label>
                    <input id='star-4' type='radio' name='rating' value='star-2'>
                    <label for='star-4' title='Nice'></label>
                    <input id='star-5' type='radio' name='rating' value='star-1'>
                    <label for='star-5' title='Not Bad'></label>
                    <input type="submit" name="submit" value="submit"/>
                  </form>
                  </div>


      </div>
      </div>
      <?php
      if(isset($_POST['submit'])){
       if(isset($_POST['rating'])){
         echo "You have selected ";
         }else{ echo "<span>Please choose any radio button.</span>";}
         }


               ?>
          <?php endforeach ?>

你可以: (虽然我不推荐你的编码模式)

foreach.

中为每个表单设置唯一的 ID

使所有 forminput 独一无二(或至少能够单独处理它们)。

像这样

<?php
foreach ($imgsqlResults AS $key => $item) {
    ?>
    <div class='postsContainer'>
        <div class='profile-cont'>
            <div class='who-post'>
                <div class='who-post-name'><a href='$imgrefrence'> Marwan Mason </a></div>
                <div class='who-post-img'><img src='photos/<?php echo $item['pic_path'] ?>'></div>
            </div>

            <div class='img-ctn'><img src='photos/<?php echo $item['pic_path'] ?>'></div>
            <div class='star-rating'>

                <form method='POST'>
                    <!-- This unique_id is to handle each form separately -->
                    <input type="hidden" name="unique_id" value="<?= $item['your_record_primary'] ?>"/>

                    <!-- This your_record_primary will make every input name unique -->
                    <input id='star-1<?= $key ?>' type='radio' name='rating<?= $item['your_record_primary'] ?>'
                           value='star-5'>
                    <label for='star-1' title='Perfection'></label>
                    <input id='star-2<?= $key ?>' type='radio' name='rating<?= $item['your_record_primary'] ?>'
                           value='star-4'>
                    <label for='star-2' title='Amazing'></label>
                    <input id='star-3<?= $key ?>' type='radio' name='rating<?= $item['your_record_primary'] ?>'
                           value='star-3'>
                    <label for='star-3' title='wow'></label>
                    <input id='star-4<?= $key ?>' type='radio' name='rating<?= $item['your_record_primary'] ?>'
                           value='star-2'>
                    <label for='star-4' title='Nice'></label>
                    <input id='star-5<?= $key ?>' type='radio' name='rating<?= $item['your_record_primary'] ?>'
                           value='star-1'>
                    <label for='star-5' title='Not Bad'></label>
                    <input type="submit" name="submit" value="submit"/>
                </form>
            </div>
        </div>
    </div>
    <?php
    if (isset($_POST['submit']) && (!empty($_POST['unique_id']) && $_POST['unique_id'] === $item['your_record_primary'])) {
        if (isset($_POST['rating' . $item['your_record_primary']])) {
            echo "<span>You have selected :<b> " . $_POST['rating' . $item['your_record_primary']] . "</b></span>";
        } else {
            echo "<span>Please choose any radio button.</span>";
        }
    }
}
?>

id='star-1<?= $key?>' 基本上会给你:

<!-- First iteration will look like -->
<input id='star-10 />
.
.
<input id='star-50 />
<!-- Next iteration will look like -->
<input id='star-11 />
.
.    
<input id='star-51 />

注意: 如果您在 JS 中使用这些 input 元素的 id 属性,也许您必须找到一种方法来处理这些动态ids.

解释:

因为在您的代码中 if(isset($_POST['submit']) 在循环内。

我们需要在提交表单时评估 ONLY 的正确性 if(isset($_POST['submit']),为了实现这一点,我们引入了额外的输入 <input type="hidden" name="unique_id" value="<?= $item['your_record_primary'] ?>"/> 来维护独特性。

我们再替换if(isset($_POST['submit'])

if (isset($_POST['submit']) && (!empty($_POST['unique_id']) && $_POST['unique_id'] === $item['your_record_primary']))。这样只有具有正确 unique_id 的请求才能进入其 if 条件。希望我说得足够清楚。

你可以这样使用

   <html>
<body>
<form method='POST'>
                  <input id='star-1' type='radio' name='rating[]' value='star-5'>
                  <label for='star-1' title='Perfection'></label>
                  <input id='star-2' type='radio' name='rating[]' value='star-4'>
                  <label for='star-2' title='Amazing'></label>
                  <input id='star-3' type='radio' name='rating[]' value='star-3'>
                  <label for='star-3' title='wow'></label>
                  <input id='star-4' type='radio' name='rating[]' value='star-2'>
                  <label for='star-4' title='Nice'></label>
                  <input id='star-5' type='radio' name='rating[]' value='star-1'>
                  <label for='star-5' title='Not Bad'></label>
                  <input type="submit" name="submit" value="submit"/>
                </form>


</body> 
</html>
    <?php
    if(isset($_POST['submit'])){
     if(isset($_POST['rating'])){

       echo "<span>You have selected :<b> ".$_POST['rating'][0]."</b></span>";
       }else{ echo "<span>Please choose any radio button.</span>";}
       }