
When I click on the 2nd, 3rd, 4th... friend, it's always the 1st friend that appears in the modal, and when I send a message through that box, it's always sent to the first friend. How can I solve this problem?
<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>
All your elements that you create for different friends have the same id.
Simplified example:
<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>
Now you click on the second div and it does what it's told, it opens modal1.
What you'll have to do is create a suffix:
$i = 0;
while ($row=mysqli_fetch_array($query3)) {
$i++;
$idsuffix = '_idsuffix' . $i;
For each id, you append $idsuffix:
<div id="openModal<?= $idsuffix ?>" class="modalDialog">
<div id="b_1<?= $idsuffix ?>">
Ok, first of all it is always god to format your code properly so you don't make these mistake.
Your first output is missing a closing 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-->
lastly you had close your if statement to quickly around the below code:
//Start Gallery Row
$output .= "
<div class='row'>
<div class='col-md-12'>
<div id='gallery-slider' class='slider responsive'>
";
} // CLOSED AT THE WRONG SPOT
Try the below:
<?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;
?>