【问题标题】:PHP string with quotes, output into html table distorting layout带引号的PHP字符串,输出到html表格扭曲布局
【发布时间】:2013-04-11 12:47:19
【问题描述】:

我正在用 PHP 编写评论脚本。审阅者撰写的审阅文本连同其他值(例如审阅者给审阅的日期和标题)一起插入到数据库中。

我想在单独页面的表格上输出评论。我已经写过了,它工作正常,除非审阅者写了“测试”之类的东西。所以如果评论中有引号或语音标记,表格就会扭曲,评论停留在一行,不遵循<td>的宽度

代码如下:

    <?php


        echo "<tr $class>";
        echo "<td width='400' class='style1' style='color: #069; font-size: 15px;'>".$rating." &nbsp;&nbsp;&ldquo;".$row['title']."&rdquo; &nbsp;</td>";
        echo "<td width='200' class='style1'><div align='left'>Reviewed ".date_format($timeCreate, 'j F Y ')."</div></td>";
        echo "</tr>";
        echo "<tr $class>";
        echo "<td width='200'>";

        // Prints pros, cons, and other thoughts fields
        if($row['comment'])
        {

                echo "<p class='style2'>".$row['comment']."</p><div id='usname'>by ".$row['name']."<br><br></div><div class='$g'><font size='2' color='black'>Was this review helpful?</font>&nbsp;<input type='submit' value='Yes' onClick = 'myCall()' style='background-color:#556B2F;color:white;padding:2px; cursor:pointer' name='help' id='$g' class='button' /></div></td><td><img src='Themes/images/glyphicons_072_bookmark.png' width='10' height='15'></div><font size='1' style='padding-bottom:10px;'>&nbsp;&nbsp;".$row['helpful']." found this helpful</font><div id='mybox'>

        </div></td>";
            echo '

    '; 
        }
        echo "</tr><tr><td colspan='2'><hr></td></tr>";


        echo "<tr $class>";

        $id = $row['id'];
        echo "</tr>";

    }

    echo ""; 
    echo "</table>";
}
else
{
    echo "<p class='red'>Error</p>";
    echo "<p>This person does not exist.</p>";
}
?>

表格如下所示:

<table width='600' border='1' cellpadding='0' cellspacing='0'>

上面还有更多代码可以输出评论标题等数据,但我没有包含这些数据,因为我认为这并不重要。

不仅仅是审阅脚本有这个问题,所有表都将数据库中的信息放入表中。

【问题讨论】:

  • 您能说明问题吗?截图会很好。最好是一个指向问题现场演示的链接。
  • 能否请您将结果发布在表格损坏的地方。
  • 您应该为您的宽度、高度和边框等指定px。例如,400px
  • 桌子在echo "&lt;p class='style2'&gt;".$row['comment']."&lt;/p&gt;破了

标签: php html database string html-table


【解决方案1】:

我在下面的代码 sn-p 中看到了问题

echo "<td width='200'>";
        // Prints pros, cons, and other thoughts fields
        if($row['comment'])
        {

                echo "<p class='style2'>".$row['comment']."</p><div id='usname'>by ".$row['name']."<br><br></div><div class='$g'><font size='2' color='black'>Was this review helpful?</font>&nbsp;<input type='submit' value='Yes' onClick = 'myCall()' style='background-color:#556B2F;color:white;padding:2px; cursor:pointer' name='help' id='$g' class='button' /></div></td><td><img src='Themes/images/glyphicons_072_bookmark.png' width='10' height='15'></div><font size='1' style='padding-bottom:10px;'>&nbsp;&nbsp;".$row['helpful']." found this helpful</font><div id='mybox'>

        </div></td>";
            echo '

    '; 
        }
        echo "</tr><tr><td colspan='2'><hr></td></tr>";

如果设置了 cmets,那么您将添加两列,但如果未设置 cmets,那么它将打开一个 TD,然后关闭 TR。这可能会导致未格式化的 HTML。您能否添加一个 else 条件并检查,如下所示

echo "<td width='200'>";

        // Prints pros, cons, and other thoughts fields
        if($row['comment'])
        {

                echo "<p class='style2'>".$row['comment']."</p><div id='usname'>by ".$row['name']."<br><br></div><div class='$g'><font size='2' color='black'>Was this review helpful?</font>&nbsp;<input type='submit' value='Yes' onClick = 'myCall()' style='background-color:#556B2F;color:white;padding:2px; cursor:pointer' name='help' id='$g' class='button' /></div></td><td><img src='Themes/images/glyphicons_072_bookmark.png' width='10' height='15'></div><font size='1' style='padding-bottom:10px;'>&nbsp;&nbsp;".$row['helpful']." found this helpful</font><div id='mybox'>

        </div></td>";
            echo '

    '; 
        }
        else
        {
             echo "</td><td>&nbsp;</td>";
        }
        echo "</tr><tr><td colspan='2'><hr></td></tr>";

【讨论】:

  • 感谢您的帮助,但这仍然不起作用。我不需要那里的if语句,有没有不使用if语句的方法来解决它?
【解决方案2】:

如果要显示文本,则需要将特殊字符转换为 html 实体。 http://php.net/manual/en/function.htmlentities.php

这会将 & 转换为 &amp;amp; 和 " 转换为 &amp;quot;

如果您需要转换单引号,请使用 ENT_QUOTES 选项。

【讨论】:

  • 仍然遇到同样的问题。我这样做了:$com = $row['comment']; $com1 = htmlentities($com, ENT_QUOTES); echo "&lt;p class='style2'&gt;$com1&lt;/p&gt;
  • 我认为没有问题
【解决方案3】:

也许你应该使用addlashes() http://pl1.php.net/manual/en/function.addslashes.php

在显示它之前,stripcslashes()

【讨论】:

  • 这到底有什么帮助?
猜你喜欢
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多