duanjia4969 2012-07-22 12:46
浏览 36
已采纳

for循环会发布一切,但我只想要一个帖子

I want to fetch data from my MySQL and echo it out only if $featured == 1. It works, the only one with $featured == 1 will show title, but my problem is that it also makes posts from everything else in my database (empty posts). I only want it to show the posts with == 1 and nothing more nothing less. I can't manage to fix it.

<aside id="featured" class="body"><article> 
<?php
for ($i=0; $i <$num_results; $i++){
$row1 = $result1->fetch_assoc();
$featured = ($row1['featured']);
if ($featured == 1) {
echo $row1['title'];    
} else {
}
?>
<figure> 
<img src="images/black2.gif" alt="Black 2" style="width: 300px;"/> 
</figure>   
<hgroup>    
<h2>Featured Article</h2> 
<h3><a href="goodies/black2.html">
</a></h3> 
</hgroup>   
<p> </p> 
<footer class="post-info"> 
<abbr class="published" title="date">20 juli 2012</abbr>    
<address class="vcard author">By<a class="url fn" href="portfolio.html">F4LLCON</a> 
</address></footer><!-- /.post-info -->
</article>
<?php
}
$result1->free();   
?>

</aside><!-- /#featured -->

  • Will look like:

enter image description here


  • But I want it like this:

enter image description here

  • 写回答

1条回答 默认 最新

  • douzhai7873 2012-07-22 12:50
    关注

    Your for body contains printing large amount of HTML regardless of featured checking, that is, outside your if.

    You probably should move two lines

    } else { 
    }
    

    line to the bottom of the code snippet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?