Search code examples
phpmysqliforeachratingrating-system

foreach in php is treating all pictures the same


I made a foreach loop, but I can only vote for the first movie post. If I try to vote for the other images, the changes are not reflected and I am able to see only the changes made to the first 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 ?>

Solution

  • You can: (Although I don't recommend your coding pattern)

    Make unique the ids for your each form in foreach.

    Make all form & input unique (or atleast the ability to handle them separately).

    like this

    <?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?>' will essentially give you:

    <!-- 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 />
    

    NOTE: If you're using the id properties of these input elements in your JS perhaps you must find a way to handle these dynamic ids.

    Explanation:

    Since in your code if(isset($_POST['submit']) is inside the loop.

    We need to evaluate true ONLY for the right if(isset($_POST['submit']) on submitting the form, and to achieve that we introduce and additional input <input type="hidden" name="unique_id" value="<?= $item['your_record_primary'] ?>"/>to maintain the uniqueness.

    We then replace if(isset($_POST['submit'])

    with if (isset($_POST['submit']) && (!empty($_POST['unique_id']) && $_POST['unique_id'] === $item['your_record_primary'])). So that only the request with the right unique_id gets insides its if condition. Hope I made myself clear enough.