我在加入2张桌子时遇到了麻烦。我希望所有帖子都将他们的作者写在他们之下。
表用户 - idUser,FirstName,LastName等。
表格 - idPost,主题,文字,Fk_user
Fk_user是指向idUser的外键。
我希望在每篇文章下都有名字和姓氏。
我现在得到的是每个帖子上一个人的第一个和最后一个名字。
<section id="reviews" class="page-section bg-dark gradient-servquick">
<div class="pb-0">
<div class="container relative">
<?php
$query="SELECT * FROM post ORDER BY idPost DESC";
$result=mysqli_query($connection,$query) or die (mysqli_error($connection));
$getUser="SELECT post.idPost, post.Fk_user, user.FirstName, user.LastName FROM post INNER JOIN user ON post.Fk_user = user.idUser";
$resultUser=mysqli_query($connection,$getUser) or die (mysqli_error($connection));
$extractUser=mysqli_fetch_array($resultUser);
while($extract=mysqli_fetch_array($result)): ?>
<div class="row">
<div class="col-md-8 col-md-offset-2 align-center">
<h3><?php echo $extract['Subject'] ?></h3>
<div class="review review-text mt-0">
<p>
<?php echo $extract['Text'] ?>
</p>
<footer class="review-author text-center">
<?php echo $extractUser['FirstName'], ' ', $extractUser['LastName']; ?>
</footer>
</div>
</div><!--End Col-->
</div><!--End Row-->
<hr class="style-one">
<?php
endwhile;
?>
</div><!--End Container-->
</div><!-- End Slide Item -->
</section><!-- End Testimonials Section -->
答案 0 :(得分:1)
你的代码看起来似乎有点遍布。例如,你似乎仍然要查询帖子两次,一次是作者,一次没有,然后只围绕帖子,而不是作者等帖子。以下应该有效,但未经过测试,请注意差异。
<section id="reviews" class="page-section bg-dark gradient-servquick">
<div class="pb-0">
<div class="container relative">
<?php
$getPost="SELECT post.*, u.FirstName, u.LastName FROM post p INNER JOIN user u ON p.Fk_user = u.idUser";
$resultPost=mysqli_query($connection,$getPost) or die (mysqli_error($connection));
while($extract=mysqli_fetch_array($resultPost)): ?>
<div class="row">
<div class="col-md-8 col-md-offset-2 align-center">
<h3><?php echo $extract['Subject'] ?></h3>
<div class="review review-text mt-0">
<p>
<?php echo $extract['Text'] ?>
</p>
<footer class="review-author text-center">
<?php echo $extract['FirstName'], ' ', $extract['LastName']; ?>
</footer>
</div>
</div><!--End Col-->
</div><!--End Row-->
<hr class="style-one">
<?php
endwhile;
?>
</div><!--End Container-->
</div><!-- End Slide Item -->
</section><!-- End Testimonials Section -->