我在html输出中遇到了最奇怪的时间。如果你看看我的问题开始的//开始画廊行,第一个输出工作正常。
这是输出的外观
<div class='row'>
<div class='col-md-12'>
<div id='gallery-slider' class='slider responsive'>
<img class='img-responsive' src='cdn/assets/gallery/1.jpg'>
<img class='img-responsive' src='cdn/assets/gallery/3.jpg'>
<img class='img-responsive' src='cdn/assets/gallery/2.jpg'>
<img class='img-responsive' src='cdn/assets/gallery/4.jpg'>
<img class='img-responsive' src='cdn/assets/gallery/5.jpg'>
</div>
</div>
</div>
在//画廊的开头,当我查看来源时,这是输出
<div class='row'>
<div class='col-md-12'>
<div id='gallery-slider' class='slider responsive'>
<img class='img-responsive' src='cdn/assets/gallery/1.jpg'></div>
</div>
</div>
<img class='img-responsive' src='cdn/assets/gallery/3.jpg'>
<img class='img-responsive' src='cdn/assets/gallery/2.jpg'>
<img class='img-responsive' src='cdn/assets/gallery/4.jpg'>
<img class='img-responsive' src='cdn/assets/gallery/5.jpg'>
但无论我把最后一个输出DIV放在哪里都会导致问题
<?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>";
//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;
?>
SQL
$stmt = $db->prepare("
SELECT U.ID,
U.FullName,
U.Email,
U.JobTitle,
U.Bio,
U.Photo, G.GalleryImage
FROM users U
LEFT join gallery G
ON U.ID = G.ID
");
$stmt->execute();
$result = $stmt->get_result();
答案 0 :(得分:2)
好的,所以我认为最好的做法是循环你的$ result并过滤掉所有重复的值,并将图像分配给一个以[[ID]]行为键的数组然后循环在检查之后他们!
$checker = array();
$profileArray = array();
while ($row = mysqli_fetch_assoc($result))
{
if($row['GalleryImage'])
{
$profileArray[$row['ID']]['GalleryImages'][] = $row['GalleryImage'];
}
if(!in_array($row['ID'], $checker))
{
while (list ($key, $value) = each($row))
{
if($key != 'GalleryImage')
{
$profileArray[$row['ID']][$key] = $value;
}
}
$checker[] = $row['ID'];
}
}
foreach ($profileArray as $row)
{
$ID = $row['ID'];
$FullName = $row['FullName'];
$Email = $row['Email'];
$JobTitle = $row['JobTitle'];
$Bio = $row['Bio'];
$Photo = $row['Photo'];
$GalleyImages = $row['GalleryImages'];
if (isset($Photo) && !empty($Photo))
{
$ProfileImage = "$Photo";
}
else
{
$ProfileImage = "avatar.jpg";
}
$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>";
//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'>";
if(!$GalleyImages)
{
foreach ($GalleyImages 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'>";
}
}
else
{
$output .= "HTML THAT YOU WANNA DISPLAY instead of images";
}
$output .= "
</div>
</div>
</div>
</div>";
}
echo $output;
答案 1 :(得分:1)
好的,首先,正确格式化代码总是好的,这样你就不会犯这些错误。
您的第一个输出缺少结束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;
?>
答案 2 :(得分:1)
大部分时间都会尝试将PHP
与HTML
分开,以便您轻松查看错误。
$stmt = $db->prepare("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)) : ?>
<div class='container yep team-wrap'>
<div class='row'>
<div class='col-md-6'>
<img class='img-responsive' src='cdn/assets/artist/<?php echo $ProfileImage; ?>'>
</div>
<div class='col-md-6'>
<strong><?php echo $FullName; ?><br><?php echo $JobTitle; ?></strong>
<br>
<p><?php echo $Bio; ?></p>
<a href='mailto:$Email' class='btn btn-info'>Contact Me</a>
</div>
</div>
<!-- End of info row-->
<br/><br/><br/>
<!-- Start Gallery Row-->
<div class='row'>
<div class='col-md-12'>
<div id='gallery-slider' class='slider responsive'>
<!-- Display this row as many times as needed by data in this row.-->
<?php foreach ($GalleryImage as $img) : ?>
<img class='img-responsive' src='cdn/assets/gallery/<?php echo $img; ?>'>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<!-- // End gallery row-->
<?php array_push($checker, $row['ID']); endif;
}
?
示例输出