简体   繁体   中英

PHP: HTML inside if statement inside while loop

I've used HTML inside loops and if statements before without problems. But now I got stuck.

I have a while loop and an if statement in it like this:

while ($row = mysqli_fetch_array($result)) { 
    $gcode = $row ['code'];
    $gname = $row ['gname'];
    $firstletter = substr($gname, 0, 1);
    if ($gid == $firstletter) {?><div>some html</div><?php}
}

It just won't work. No warning message or anything, just a blank page. However, if I substitute the HTML with echo like this, it works.

while ($row = mysqli_fetch_array($result)) { 
    $gcode = $row ['code'];
    $gname = $row ['gname'];
    $firstletter = substr($gname, 0, 1);
    if ($gid == $firstletter) {echo "some html";}
}

Could anyone please advise. Thank you!

When I put your code in my editor I had an error. It does not like the fact there no space in between the php and the }.

Try it like this:

while ($row = mysqli_fetch_array($result)) { 
    $gcode = $row ['code'];
    $gname = $row ['gname'];
    $firstletter = substr($gname, 0, 1);
    if ($gid == $firstletter) {?><div>some html</div><?php } //<--Just add a space.

}

您只需要添加一些空间, if ($gid == $firstletter) { ?><div>some html</div><?php } ,它将起作用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM