消息始终发送给第一位朋友

时间:2017-04-28 21:02:23

标签: javascript php html mysql sql

enter image description here

当我点击第2,第3,第4 ...朋友时,它始终是模态中出现的第一个朋友,当我通过该框发送消息时,它总是被发送给第一个朋友。我该如何解决这个问题?

  <div id="friends_list">
  <?php
    $query2 = "SELECT id,username,photo_profil FROM accounts WHERE id 
    in (SELECT id_res FROM frends WHERE (id_emtr='$id' or 
    id_res='$id') and statu='1' 
    UNION SELECT id_emtr FROM frends WHERE (id_emtr='$id' or 
    id_res='$id') and statu='1')";

    $query3 = mysqli_query($con,$query2);
    while ($row=mysqli_fetch_array($query3))
    {
        $friend_name = $row['username'];
        $friend_photo = $row['photo_profil'];
      $friend_id = $row['id'];
        if ($friend_id != $id) {
            ?>
              <div id="a">
                  <div id="a_1">
                    <img <?php echo 'src="'.$friend_photo.'"'; ?>>
                    <a href="#"><p><?php echo $friend_name; ?> </p>
                 </a>
                  </div>
                  <div id="a_2">
                      <button  id="delete" <?php 
                  echo'name="'.$friend_id.'"'; ?>  >Delete</button>
                      <a href="#openModal"><button class="message" 
                id="message" >Message</button></a>  
              <div id="openModal" class="modalDialog">
              <div id="b_1">
                <a href="#close" title="Close" class="close">x</a>
                <p>Message to : <?php echo $friend_name; ?></p>
                <form action="update_process.php" method="POST">
                  <div id="txt_area"><textarea name="msg_offline">
                 </textarea></div>
                  <input type="hidden" name="idf" <?php 
                  echo'value="'.$friend_id.'"';  ?>>
                  <div id="button_sub"><center><button type="submit" 
                  name="sub"><span id="send1">Send</span></button>
                  </center>
               </div>
                </form>
              </div>
              </div>
                </div>    
                        </div>

                     <?php
                       }
                  }

                      ?>
                      </div>

2 个答案:

答案 0 :(得分:0)

您为不同朋友创建的所有元素都具有相同的id

简化示例:

<div id="modal1">This is a modal</div>
<div id="block1" onclick="showmodal('#modal1');">Friend 1</div>

<div id="modal1">This is a modal</div>
<div id="block1" onclick="showmodal('#modal1');">Friend 2</div>

现在你点击第二个div并按照它说的做,它会打开modal1

您需要做的是创建一个后缀:

$i = 0;
while ($row=mysqli_fetch_array($query3)) {
    $i++;
    $idsuffix = '_idsuffix' . $i;

对于每个id,您附加$idsuffix

<div id="openModal<?= $idsuffix ?>" class="modalDialog">
    <div id="b_1<?= $idsuffix ?>">

答案 1 :(得分:0)

好的,首先,正确格式化代码总是上帝所以你不会犯这些错误。

您的第一个输出缺少结束div

<div class='container yep team-wrap'>
    <div class='row'>        
        <div class='col-md-6'>
            <img class='img-responsive' src='cdn/assets/artist/$ProfileImage'>
        </div>

        <div class='col-md-6'>
            <strong>$FullName<br>$JobTitle</strong>
            <br>
            <p>$Bio</p>
            <a href='mailto:$Email' class='btn btn-info'>Contact Me</a>
        </div> 
    </div>
</div> <!-- This was missing-->

最后你已经关闭了你的if语句,快速绕过下面的代码:

//Start Gallery Row
    $output .= "
        <div class='row'>
         <div class='col-md-12'>
        <div id='gallery-slider' class='slider responsive'>
       ";
} // CLOSED AT THE WRONG SPOT

尝试以下方法:

<?php
    $stmt = $db->prepare("my query");
    $stmt->execute();
    $result = $stmt->get_result();

    $output = "";
    $checker = [];

    while ($row = mysqli_fetch_assoc($result)) {
        $ID = $row['ID'];        
        $FullName = $row['FullName'];    
        $Email = $row['Email'];   
        $JobTitle = $row['JobTitle'];
        $Bio = $row['Bio'];
        $Photo = $row['Photo'];  


        $GalleryImage = explode(',', $row['GalleryImage']);


        if (isset($Photo) && ! empty($Photo)) {
            $ProfileImage = "$Photo";
        } else {
            $ProfileImage= "avatar.jpg";
        }       


        if(!in_array($row['ID'], $checker)) {

            $output .= "
                <div class='container yep team-wrap'>
                    <div class='row'>        
                        <div class='col-md-6'>
                            <img class='img-responsive' src='cdn/assets/artist/$ProfileImage'>
                        </div>

                        <div class='col-md-6'>
                            <strong>$FullName<br>$JobTitle</strong>
                            <br>
                            <p>$Bio</p>
                            <a href='mailto:$Email' class='btn btn-info'>Contact Me</a>
                        </div> 
                    </div>
                </div> <!-- This was missing-->
            ";

           //End of info row


            $output .="<br /><br /><br />";


           //Start Gallery Row

           $output .= "
                <div class='row'>
                    <div class='col-md-12'>
                        <div id='gallery-slider' class='slider responsive'>
            ";

            foreach ($GalleryImage as $img) {
               //Display this row as many times as needed by data in this row.
               $output .= "<img class='img-responsive' src='cdn/assets/gallery/$img'>";                     
            }

            $output .= "
                        </div>
                    </div>
                </div>
            ";

            // End gallery row   
            array_push( $checker, $row['ID']); 
        }
    }  


    $output .= "</div>";

    echo $output;
?>