简体   繁体   English

如何混合使用PHP和HTML代码?

[英]How can I mix PHP and HTML code?

I have a while loop and I have a HTML code inside the while loop. 我有一个while环和我有一个内部的HTML代码while循环。 Inside this HTML code there is PHP code reading a variable that depends on the condition of the while loop. 在此HTML代码中,有PHP代码读取取决于while循环条件的变量。

This variable should be evaluated by PHP. 此变量应由PHP求值。 I wrote the code below to solve this but it doesn't work. 我写了下面的代码来解决这个问题,但是没有用。

$row[0] depends on the condition of while . $row[0]取决于while的条件。 My code only outputs an empty space for this variable. 我的代码只为该变量输出空白。

<?php 
 while ($row = mysql_fetch_row($stt)) {
    $html='<div class="data">
        <?php echo nl2br($row[0]."\n"); ?>
        <p><a href="">comment</a>
        <a href="">like</a>
        <a href="">share</a></p>
        <p>
        0 <span>likes</span>
        </p>
        <p>
        _____________________
        </p>
        </div>';

    echo $html;
}
?>
>    <?php echo nl2br($row[0]."\n"); ?>

this part should be outside of $html. 这部分应该在$ html之外。

The interpreter on server side couldnt run.Accordin to php, this part is not a code block. 服务器端的解释器无法运行。根据php,这部分不是代码块。 It is just a text. 这只是一个文本。

I found the answer of my question!!! 我找到了我问题的答案!!! This is the solution: 这是解决方案:

<?php 
 while($row = mysql_fetch_row($stt)){ ?>


            <div class="data">
                  <?php echo nl2br($row[0]."\n"); ?>

                <p><a href="">comment</a>

                <a href="">like</a>

                <a href="">share</a></p>

                <p>

                0

                <span>likes</span>

                </p>

                <p>

                _____________________


                </p>


            </div>
<?php 
 }
 ?>

If you want to display something row by row, do this: 如果要逐行显示某些内容,请执行以下操作:

<?php 
while($row = mysql_fetch_row($stt)){

    $html="<div class='data'>" . nl2br($row[0]) . "\n<p><a href="">comment</a><a href=''>like</a><a href=''>share</a></p><p>0<span>likes</span></p><p></p></div>";
    echo $html;
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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