首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >数据库条目不能正确输出

数据库条目不能正确输出
EN

Stack Overflow用户
提问于 2012-11-20 01:33:11
回答 3查看 93关注 0票数 0

我无法让我的网站正确显示数据库条目。我正在将带换行符的格式化文本复制并粘贴到MySQL数据库中。如果我在终端中查看条目,它将正确地显示带有换行符的信息。但是,当我在浏览器中查看时,它的输出就好像忽略了所有的格式化规则,使其成为一个长文本字符串,正好到达DIV的边缘,并创建了一个没有格式的新行。我是新手,所以如果我的问题看起来含糊不清,我提前道歉,只要询问任何你可能会发现有用的额外信息,以提供准确的解决方案。

代码语言:javascript
运行
复制
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>
        Test Site!    
        </title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <LINK href="CLLProfile.css" rel="stylesheet" type="text/css">
                    <script language="javascript" type="text/javascript">


// Roshan's Ajax dropdown code with php
// This notice must stay intact for legal use
// Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
// If you have any problem contact me at http://roshanbh.com.np
function getXMLHTTP() { //function to return the xml http object
        var xmlhttp=false;  
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {       
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
    }

    function getCategory(CatID) {       

        var strURL="Categories.php?category="+CatID;
        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('categorydiv').innerHTML=req.responseText;                      
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);
        }       
    }

    function getSub(CatID) {        

        var strURL="SubCategories.php?category="+CatID;
        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('subcategorydiv').innerHTML=req.responseText;                       
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);
        }       
    }

</script>

    </head>

    <body>
        <div id="wrapper">
        <div class="container">

            <div id="header">
                <a href="index.php"><img src="images/Logo.jpg"></a>

                <div class="login"> 
                    <a href="register.php">Sign Up</a> <a href="main_login.php">Login</a>
                </div>

                <div class="search">
                    <form id ="search" action="" method="post" enctype="multipart/form-data">
                        <label>Search:</label>
                        <input type="text" name="searchBox" id="searchBox" /> 
                        <input type="submit" id="Submit" value="Submit" />
                    </form> 
                </div>

            </div>

            <div class="navigation">

                <form id ="categoryForm" action="businesses.php" method="post">
                    <div id="categorydiv">
                        <select name="thing" onChange="getSub(this.value)">
                            <option value=0>Select Main Category<?=$options?>
                        </select> </div>

                    <div id="subcategorydiv">
                        <select name="subCats" >
                             <option>Select Sub Category</option>
                        </select></div>

                        <input type="submit" value="Submit" id="Submit" />
                </form>
                </div>


                <div class="content">

                   <?PHP                             


                     echo "<img src=" . "users/" . $username . "/images/" . $logo . " " . "width=" . "200" . " " . "height=" . "auto" . " " . "border=" . "0" . "/>" . "</a>" . "<br>";


                  ?>

                    <div class="description">

                   <?PHP                             
                     echo "<h2>" . $name . "</h2>";
                     echo "<p>" . $description . "</p>";

                  ?>
                    </div>



                    </div>
                </div>

        </div>


    </body>

</html>

文本即时消息粘贴到MySQL数据库中时,将在description字段中输出:

1930年,查尔斯·海德和大卫·弗雷登堡在爱荷华州比肯斯菲尔德开了一家小杂货店。这家商店后来发展成为Hy-Vee --一家以优质服务和低廉价格著称的公司。作为一家员工所有的公司,Hy-Vee鼓励我们超过56,000名员工中的每一位来帮助指导公司。

我们的卓越成功体现了他们的辛勤工作和清晰的愿景。通过他们的努力,Hy-Vee已经成为高质量产品、低价格和优质客户服务的代名词。我们的口号“在每一个过道上都有帮助的微笑”表达了我们的企业理念的基础。

Hy-Vee是中西部消费者对饮食、营养和健康主题信息的渴望的试金石。以天然和有机产品为特色的HealthMarket部门;店内营养师的咨询服务;NuVal营养评分系统;消费者和员工健康计划;以及Hy-Vee铁人三项,强调了公司对健康生活方式的承诺。

我们的公司办公室位于爱荷华州的西得梅因。Hy-Vee校区包括一个12,000平方英尺的会议中心,用于会议、培训和继续教育项目。

我们的配送业务总部设在爱荷华州的Chariton,在那里我们拥有超过150万平方英尺的最先进的仓库空间。另一个大型配送中心位于爱荷华州切诺基,占地650000平方英尺,销售干货和一般商品。

Hy-Vee在中西部八个州拥有超过73亿美元的销售额和235多家零售店,位居美国前20大连锁超市和前50家私营公司之列。食品行业的权威声音“超市新闻”向该公司授予了“全健康企业奖”,以表彰其在提供促进健康生活方式的服务和项目方面的领导地位。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-20 01:41:10

为此,您可以使用nl2br(),它将\r\n\n替换为<br />

代码语言:javascript
运行
复制
$text = "some\ntext\nwith new lines";
$text = nl2br($text);

输出

代码语言:javascript
运行
复制
some<br />
text<br />
with new lines

因此,在您的代码中,将其更改为:

代码语言:javascript
运行
复制
   echo "<h2>" . nl2br(htmlentities($name)) . "</h2>";
   echo "<p>" . nl2br(htmlentities($description)) . "</p>";

我还添加了htmlentities()以防止XSS,并使用正确的HTML实体对特殊字符进行编码。

票数 2
EN

Stack Overflow用户

发布于 2012-11-20 01:35:30

需要将换行符("\n“和/或"\r")转换为换行符标签()才能在web浏览器中正确显示

代码语言:javascript
运行
复制
str_replace("\n", "<br />", "My string\n has line breaks");

如果你想使用段落标签,你可以这样做:

代码语言:javascript
运行
复制
echo "<p>".str_replace("\n", "</p><p>", "My string\n has line breaks")."</p>";
票数 4
EN

Stack Overflow用户

发布于 2012-11-20 01:38:35

浏览器会忽略换行符(不过,当您检查源代码时,您会看到它们)。

您需要将它们转换为<br /> (最简单的解决方案是$str = str_replace ("\n", "<br />", $str);)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13459080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档