dongxian7194 2016-01-12 13:35 采纳率: 0%
浏览 34

Php循环在输出中提供额外的HTML元素

<?php
$sun=mysql_query("SELECT * FROM `red_users` WHERE `role`='user' AND `userID` != '$userID' ORDER BY `id` DESC LIMIT 10 ") or die(mysql_error());

while($su[]=mysql_fetch_array($sun)){

}

for($i=0;$i<count($su);$i++){

    $sud=$su[$i]['userID'];
    $sname=$su[$i]['name'];
    $dop=$su[$i]['dp'];
    $sxx=$su[$i]['sex'];

    if($dop != ''){
        $sey=$dop;
    }

    if($dop =='' AND $sxx=='MALE'){
        $sey="../images/male.png";
    }

    if($dop =='' AND $sxx=='FEMALE'){
        $sey="../images/female.png";
    }
?>

<a href="profile.php?frnd=<?php echo $sud ?>">
    <div class="inbox-item">
        <div class="inbox-item-img"><img src="<?php echo $sey ?>" class="img-circle" alt=""></div>
        <p class="inbox-item-author"><?php echo $sname; ?></p>
    </div>
</a>
<?php } ?

In output an extra Html element (with no dynamic data) is displaying.
After the Real elements which are populated by mysql query an extra element is displaying which contain no data.

  • 写回答

1条回答 默认 最新

  • douji0588 2016-01-12 13:39
    关注

    Instead of

    while($su[]=mysql_fetch_array($sun)){
    
    }
    

    and further processing of $su

    process every record in the same while:

    while($su=mysql_fetch_array($sun)){
        // do stuff
    }
    

    Additional note by @mloureiro:

    This because the while keeps running till it find a false value, so when the mysql_fetch_array doesn't have anything else to return, it will give false that will be passed to the $su[] array.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程