【问题标题】:Echo inside the html tag在 html 标签内回显
【发布时间】:2014-10-22 05:10:54
【问题描述】:

PHP只在我直接用HTML标签回显内容时才起作用(echo在标签外),如下

include('db.php');
$blogurl="http://www.com/view/";
$results = mysql_query("SELECT * FROM product ORDER BY `id` DESC LIMIT 1");
while ($row = mysql_fetch_array($results)) {
echo "<h2><a href=" . $blogurl . $row['url'] . ">" . $row['title'] . "</a></h2>";

}

但是当我尝试这种风格时它不起作用:

<?php
    include('db.php');
    $results = mysql_query("SELECT * FROM product ORDER BY `id` ASC");
    while ($row = mysql_fetch_array($results)) {
        $blogurl="http://www.com/view";
        $url=$row['url'];
        $title=$row['title'];
?>
        <td>
            <a href="<?php echo $blogurl;?>/<?php echo $url;?>"><?php echo $title;?></a>   
        </td>
<?php
    }
?>

我想要改变从数据库中回显数据的方式。但是第二种风格有什么问题呢?

【问题讨论】:

  • -1 表示这样的问题
  • 代码中缺少分号。您可以使用phpcodechecker.com 进行语法错误检查...
  • 请发布生成的 HTML。您使用的是有效的 html 吗?
  • 我的意思是你应该学会调试你的代码!!逻辑错误,不良做法..是另一回事!为什么其他人要修复您的语法错误和拼写错误 :) 例如混合视图代码和数据库模型)代码不是一件好事!,您最终会得到重复的、不可维护的代码

标签: php


【解决方案1】:

我刚刚解决了。我认为问题在于我们无法从数据库中获取具有 html 扩展名的内容。所以解决方案是我必须创建一个字符串或一个 var 或(我不知道我们在 php 中如何称呼它)如下:

&lt;?php echo $blogurl;?&gt;/&lt;?php echo $title.".html"?&gt;

解决方案是我不需要数据库中的 url 行。我只需要呼应标题并在其后面加上“.html”。

感谢任何试图帮助我的人。

干杯

【讨论】:

    【解决方案2】:

    代码应该是:

    <?php
                include('db.php');
                $results = mysql_query("SELECT * FROM product ORDER BY `id` ASC");
                $blogurl="http://www.com/view";
                while ($row = mysql_fetch_array($results)) {
                           $url=$row['url'];
                ?>
    
            <td><a href="<?php echo $blogurl;?>/<?php echo $url;?>"><?php echo $title; ?></a></td>
    
    
                <?php
                 }
                ?>
    

    【讨论】:

    • 它不起作用。我唯一知道的是问题来自
    【解决方案3】:

    试试这个代码

         <?php
            include('db.php');
            $results = mysql_query("SELECT * FROM product ORDER BY `id` ASC");
            $blogurl="http://www.com/view";
            while ($row = mysql_fetch_array($results)) {
    
            $url=$row['url'];
            $title=$row['title'];
            ?>
    
        <td><a href="<?php echo $blogurl.'/'. $url;?>"><?= $title;?></a></td>
    
    
            <?php
             }
            ?>
    

    【讨论】:

      【解决方案4】:

      试试这个

          <?php
          include('db.php');
          $blogurl="http://www.com/view";
          $results = mysql_query("SELECT * FROM product ORDER BY `id` DESC LIMIT 1");
          while ($row = mysql_fetch_array($results)) {
          echo "<h2><a href='" . $blogurl."/".$row['url']  . "'>" . $row['title'] . "</a></h2>";
      }
          ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-16
        • 2011-04-25
        • 2015-08-24
        • 1970-01-01
        • 1970-01-01
        • 2011-09-28
        • 1970-01-01
        相关资源
        最近更新 更多