Я хочу проверить, существует ли массив для imagesid и imgs или imagesid, и imgs имеют одинаковое значение, и он не выдает сообщение об ошибке Notice: Undefined offset:
.
У меня есть этот код дляполучить связанные изображения для сообщений:
$sql = "SELECT * FROM multiple_image";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$imgs[$row['imageid']][$row['id']]= "<img width='' src='../images/".$row['name']."' >"; // array of image id's, with arrays of images inside them.
}
}
$sql = "SELECT * FROM posts ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$commentsToImages[$row['commentid']] = $row['imagesid']; // array of post id's to picture ids
$comments[$row['commentid']] = $row['comment']; // array of post id's to comment text.
}
}
Этот код отображает все связанные изображения с содержанием:
foreach($commentsToImages as $commentID =>$imagesID) {
?>
<div class='main'>
<div class='comments'>
<?php echo $comments[$commentID]; // render the comment ?>
</div>
<div class='pics'>
<?php
foreach($imgs[$imagesID] as $img) { // foreach loop that will render all the images...
echo $img;
}
?>
</div>
</div>
<?php
}
?>