【问题标题】:Printing a php variable in a html tag which was already echoed inside a php tag在 html 标记中打印 php 变量,该变量已在 php 标记中回显
【发布时间】:2016-01-02 21:22:36
【问题描述】:

我无法在 html 标记中打印已在 php 标记中回显的变量?我想在 h1 标记中打印 $A 变量。我的代码:

  <?php 
   while($pr = mysql_fetch_assoc($qAccess))

    {  

        $id=$pr["user_id"];                             
        $A=$pr["name"];

    }

echo  "<html  

 <!-- Profile info -->

        <div id='profile_info'>

            <h1 id='name' class='transition-02'>$A</h1>

            <h4 class='line'>no heading</h4>

            <h6><span class='fa fa-map-marker'></span> San Francisco , CA</h6>

        </div>

        <!-- End Profile info -->

</html>" ;  
?>

【问题讨论】:

  • 有什么问题?你有任何错误吗?你得到了什么,你期望什么?
  • 应该已经可以了 :) eval.in/445359
  • 它与无标题和加利福尼亚州旧金山相呼应。这是你所期待的吗?
  • 应该关闭。

标签: php html


【解决方案1】:

我认为您应该将 echo 放在 while 循环中。 如果你不这样做,它只会显示你的数组的最后一次出现。

试试这个:

<?php 

    echo "<html>";

    while($pr = mysql_fetch_assoc($qAccess)) {

        $id = $pr["user_id"];
        $A = $pr["name"];

        echo "<!-- Profile info -->

            <div id='profile_info'>

                <h1 id='name' class='transition-02'>$A</h1>

                <h4 class='line'>no heading</h4>

                <h6><span class='fa fa-map-marker'></span> San Francisco , CA</h6>

            </div>

        <!-- End Profile info -->";
    }

    echo "</html>" ;  

?>

否则可能是因为&lt;html&gt; 未在您的代码中正确关闭。

【讨论】:

  • 无论是否有封闭的html标签,它都输出相同的东西。
  • 哇@jessica,你就像幸运的卢克。比你的影子快负 1。
  • 你假设最坏的人比你的影子更快。我只评论了你的帖子。
  • 该死的杰西卡,我深表歉意。那么我们在 SO 中还有另一个 Lucky Luke :-)
  • 使用这个echo "&lt;h1 id='name' class='transition-02'&gt;{$A}&lt;/h1&gt;";
【解决方案2】:

2 个选项
- 选项 1:

<div id='profile_info'>

    <h1 id='name' class='transition-02'><?=$a?></h1>

    <h4 class='line'>no heading</h4>

    <h6><span class='fa fa-map-marker'></span> San Francisco , CA</h6>

</div>

- 选项 2:

    <?php
    echo "<html>  
        <!-- Profile info -->

            <div id='profile_info'>

                <h1 id='name' class='transition-02'>".$A."</h1>

                <h4 class='line'>no heading</h4>

                <h6><span class='fa fa-map-marker'></span> San Francisco , CA</h6>

            </div>

        <!-- End Profile info -->

        </html>"
?>

【讨论】:

  • 解决办法是什么?问题本身含糊不清,怎么能作为答案引用?
  • @HuongNV 选项 1 和 2 均未返回任何内容。如果我像这样在选项 2 中使用单引号>> '.$A.'那么它只返回'..'
猜你喜欢
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多