Php 每行检索后换行

Php 每行检索后换行,php,Php,我需要在检索到的每一行之后添加一个换行符; 我已经试着用各种可能的方法添加了。。不走运 while ($line = mysql_fetch_array($result)) { ?> <style> .whiteBackground { background-color:#F5ECCE;float:left;} .grayBackground { back

我需要在检索到的每一行之后添加一个换行符; 我已经试着用各种可能的方法添加了。。不走运

while ($line = mysql_fetch_array($result))
    {
        ?>  
                <style>
                .whiteBackground { background-color:#F5ECCE;float:left;}
                .grayBackground { background-color:#CED8F6; float:right;}
                .date { font-size:10px; color:#627d79;float:right}
                </style>
        <?php

        $sender=$line["sender"];
        $classy=($sender == $uid)? 'whiteBackground': 'grayBackground';
        $q=mysql_query("select * from users where u_id='$sender'");
        $row=mysql_fetch_array($q);
        $receiver=$row['firstname'];
        //if($receiver!= $line["receiver"])
        $msg = $msg . "<tr class='$classy'>" .
            "<td>" . $receiver . ":&nbsp;</td>" .
            "<td>" . $line["msg"] . "</td>"."<td class='date'>" . $line["chattime"] . "</td></tr>";

    }
    $msg=$msg . "</table>";



    echo $msg;
我需要在每个发送者+msg+时间之后添加一个中断。//就像平常的聊天一样


尝试添加一个内循环后

首先考虑在循环之前移动CSS声明,在循环体中没有任何意义。

第二,循环中的第二个查询也应该移动到循环之前,并稍微修改一下

现在,对于您的问题,一个选项是使用div和span更改表。。大概是这样的:

<div class="message-row">
 <span class="message-user">Name</span>
 <span class="message-text">Message</span>
</div>
然后像你们以前做的那个样设计样式,并在底部添加边距:15px;到消息行定义。

您的问题来自浮点声明。它们在您的代码中是无用的:

.whiteBackground { background-color:#F5ECCE;float:left;}
.grayBackground { background-color:#CED8F6; float:right;}
.date { font-size:10px; color:#627d79;float:right}
移除它们,它将按预期工作。 如果使用和,则代码中不需要它们。的默认行为是每次添加新行时创建新行,但添加float会破坏此行为

另外,按照@bulforce建议在循环之前移动CSS:当文档中有消息时,您的代码将添加它。如果您有10条消息,CSS声明将添加10次

<style type="text/css">
    .whiteBackground { background-color:#F5ECCE; }
    .grayBackground { background-color:#CED8F6; }
    .date { font-size:10px; color:#627d79; }
</style>
<?php

while ($line = mysql_fetch_array($result))
{
    // ...
}

?>  

我通过用div标记替换table标记解决了这个问题。 Thnku@bulforce;现在我将完全遵循你的建议

 $msg="<div>";
     $date_temp="nil";
    while ($line = mysql_fetch_array($result))
    {
        ?>  
                <style>
                .whiteBackground { background-color:#F5ECCE;float:left;}
                .grayBackground { background-color:#CED8F6; float:right;}
                .date { font-size:10px; color:#627d79;float:right}
                </style>
        <?php

        $sender=$line["sender"];
        $classy=($sender == $uid)? 'whiteBackground': 'grayBackground';
        $q=mysql_query("select * from users where u_id='$sender'");
        $row=mysql_fetch_array($q);
        $receiver=$row['firstname'];
        //if($receiver!= $line["receiver"])
        /*if($date_temp!=$line["chatdate"]){
            $date_temp=$line["chatdate"];
            $msg=$msg.$line["chatdate"];
        }*/
        $msg = $msg . "<div class='$classy'>" . $receiver.":&nbsp;" . $line["msg"] ."<div class='date'>" . $line["chattime"] . "</div></div></br></br>";

    }
    $msg=$msg . "</div>";



    echo $msg;

我认为在提交答案后立即否决它肯定有一些原因。无论是谁,他/她都准备在发布后立即否决其他人的投票。我认为他/她根本没有读过这篇文章,也不知道这篇文章是否有效。浮动声明确实有用。别忘了这是一个聊天室。@AbeyJosey:如果使用and,代码中不需要它们。每次添加新行时,都要创建新行,但如果在其上添加float,则会破坏此行为。
 $msg="<div>";
     $date_temp="nil";
    while ($line = mysql_fetch_array($result))
    {
        ?>  
                <style>
                .whiteBackground { background-color:#F5ECCE;float:left;}
                .grayBackground { background-color:#CED8F6; float:right;}
                .date { font-size:10px; color:#627d79;float:right}
                </style>
        <?php

        $sender=$line["sender"];
        $classy=($sender == $uid)? 'whiteBackground': 'grayBackground';
        $q=mysql_query("select * from users where u_id='$sender'");
        $row=mysql_fetch_array($q);
        $receiver=$row['firstname'];
        //if($receiver!= $line["receiver"])
        /*if($date_temp!=$line["chatdate"]){
            $date_temp=$line["chatdate"];
            $msg=$msg.$line["chatdate"];
        }*/
        $msg = $msg . "<div class='$classy'>" . $receiver.":&nbsp;" . $line["msg"] ."<div class='date'>" . $line["chattime"] . "</div></div></br></br>";

    }
    $msg=$msg . "</div>";



    echo $msg;