【问题标题】:Nested looping in PHP - layout issuesPHP中的嵌套循环 - 布局问题
【发布时间】:2012-11-25 13:37:42
【问题描述】:

我正在构建的网站中有评论和回复部分。回复部分设置为向右移动,边距为 10%。但是当我测试代码时,它会将每个回复向右移动,而不是所有回复一起移动。我被卡住了,因为回复在 cmets 内循环,我不知道如何解决它。这是代码...

        // Comments section
        // Get Username and ID from database
        if ($login->isLoggedIn()) {
            $userquery = "SELECT * FROM settings WHERE user_id=$userid";
            $usersql = mysql_query($userquery) or die('SQL Error :: '.mysql_error());
            $userqueryrow = mysql_fetch_assoc($usersql);
            $usercommentname = $userqueryrow['display_name'];
            } else {
            }

        // Posted Comments 
        $commentquery = "SELECT * FROM comments WHERE listing_id=$listingid AND response_to_id='0' ORDER BY comment_ts ASC";
        $commentsql = mysql_query($commentquery) or die('SQL Error :: '.mysql_error());

         if(mysql_num_rows($commentsql) == 0){ 
            echo '<div align="center">No comments have been posted yet.</div>';                        
                } else {    
            while($commentrow=mysql_fetch_array($commentsql))
            {
            // Get comment display name from database
            // Get Username and ID from database
            $commentid = $commentrow['comment_id'];
            $commenterid = $commentrow['user_id'];
            $commenterquery = "SELECT * FROM settings WHERE user_id=$commenterid";
            $commentersql = mysql_query($commenterquery) or die('SQL Error :: '.mysql_error());
            $commenterqueryrow = mysql_fetch_assoc($commentersql);
            $commentername = $commenterqueryrow['display_name'];
            echo '<div class="comment"><a href="mypage.php?user='.$commenterid.'">'.$commentername.'</a> said: '.stripslashes($commentrow['comment']).'';

            if ($login->isLoggedIn()) {
            // Has this user already reported this comment?
            $reportedcommentquery = "SELECT * FROM report WHERE comment_id=$commentid AND reporter_id=$userid AND listing_id=$listingid";
            $reportedcomment = mysql_query($reportedcommentquery) or die('SQL Error :: '.mysql_error());

        if (mysql_num_rows($reportedcomment) > "0") {
            echo '  <span class="small_gray">-Reported and under review-</span>';
            } else {
            echo '  <a href="item.php?item='.$listingid.'&report='.$commentid.'" class="small_graylink">Report</a>';
            }

            echo '</div>';

                // Get responses
                $responsesquery = "SELECT * FROM comments WHERE listing_id=$listingid AND response_to_id=$commentid ORDER BY comment_ts ASC";
                $responsessql = mysql_query($responsesquery) or die('SQL Error :: '.mysql_error());

                 if(mysql_num_rows($responsessql) == 0){ 
                    echo '';                        
                    } else {    
                    while($responsesrow=mysql_fetch_array($responsessql))
                    {
                    // Get responder display name from database
                    $responderid = $responsesrow['user_id'];
                    $responseid = $responsesrow['comment_id'];
                    $responderquery = "SELECT * FROM settings WHERE user_id=$responderid";
                    $respondersql = mysql_query($responderquery) or die('SQL Error :: '.mysql_error());
                    $responderqueryrow = mysql_fetch_assoc($respondersql);
                    $respondername = $responderqueryrow['display_name'];
                    $responderid = $responderqueryrow['user_id'];
                    echo '<div class="response"><a href="mypage.php?user='.$responderid.'">'.$respondername.'</a> said: '.stripslashes($responsesrow['comment']).'';

                    if ($login->isLoggedIn()) {
                        // Has this user already reported this comment?
                        $reportedresponsequery = "SELECT * FROM report WHERE comment_id=$responseid AND reporter_id=$userid AND listing_id=$listingid";
                        $reportedresponse = mysql_query($reportedresponsequery) or die('SQL Error :: '.mysql_error());

                    if (mysql_num_rows($reportedresponse) > "0") {
                    echo'  <span class="small_gray">-Reported and under review-</span>';
                    } else {
                    echo'  <a href="item.php?item='.$listingid.'&report='.$responsesrow['comment_id'].'" class="small_graylink">Report</a>';
                        }
                    }
                    }

                    echo '</div>';
                    echo '<div class="clear"></div>';
                        }
                    }

              // Response form
              if ($login->isLoggedIn()) {
              echo '<div class="responsebox">Respond to this comment:<br /><form id="newresponseto" name="newresponseto" method="post" action="item.php?item='.$listingid.'&comment='.$commentrow['comment_id'].'">
              <textarea name="new_response" cols="43" rows="2">'.$usercommentname.' says:</textarea><br />
              <input type="submit" name="Submit" value="Submit" /></form></div><hr>';
                              } else {
                              }
                }
            }

          // Comment form
          if ($login->isLoggedIn()) {
echo'         <div class="commentbox">Post a comment:<br /><form id="newcommentform" name="newcommentform" method="post" action="item.php?item='.$listingid.'">
            <textarea name="new_comment" cols="50" rows="2">'.$usercommentname.' says:</textarea><br />
            <input type="submit" name="Submit" value="Submit" />
          </form></div>';
          } else {
          echo '<div align="center">Log in to post comments or responses.</div>';
          }

这里是对应的CSS..

.comment {
    float: left;
    width: 100%;
    background-color:#CCCCCC;
    margin-bottom: 10px;

}

.commentbox {
    float: left;
    width: 100%;
    margin-bottom: 10px;
}

.response {
    float: left;
    background-color:#EDEFF4;
    margin-bottom: 10px;

}

.responsecontainer {
    float: left;
    margin-left: 10%;
    width: 90%;
    margin-bottom: 10px;
}

.responsebox {
    float: left;
    margin-bottom: 10px;
}

知道如何保持嵌套循环,但强制响应(以及实际上任何后续的 cmets)布局而不循环左边缘?

【问题讨论】:

  • 能否贴出浏览器编译的这个例子的源代码?用 PHP echo 写的 HTML 结构很难理解
  • Sean 找出了问题所在,但我要感谢您抽出宝贵时间来查看此问题。鉴于我发布的代码,我意识到这不是一个尝试解决的有趣问题。将来,我会记得发布输出的来源......这样会更容易。

标签: php html css


【解决方案1】:

我在您发布的代码中没有看到您调用了.responsecontainer 类,这会将margin-left 设置为10%。

但是responses 的嵌套是因为你没有关闭你的 &lt;div class="response"&gt;while 循环中,仅在您关闭循环后的末尾。所以你会遇到这样的情况-

<div class="response"><a href="mypage.php?user=1">user1</a> said: Yes
<div class="response"><a href="mypage.php?user=2">user2</a> said: I agree
<div class="response"><a href="mypage.php?user=3">user3</a> said: Are You Sure?
</div>

所以最后 2 个将嵌套在第一个 &lt;div&gt; 中,而不是直接在下面。

您需要在response while 循环周围添加&lt;div class="responsecontainer"&gt;,然后在循环内添加&lt;/div&gt;

截断的脚本-

            // Get responses
            ...
             echo '<div class="responsecontainer">';  // Add <div class="responsecontainer"> to make all responses margin-left 10% as a group 
             if(mysql_num_rows($responsessql) == 0){ 
                ...    

                while($responsesrow=mysql_fetch_array($responsessql))
                {
                // Get responder display name from database
                $responderid = $responsesrow['user_id'];
                $responseid = $responsesrow['comment_id'];
                $responderquery = "SELECT * FROM settings WHERE user_id=$responderid";
                $respondersql = mysql_query($responderquery) or die('SQL Error :: '.mysql_error());
                $responderqueryrow = mysql_fetch_assoc($respondersql);
                $respondername = $responderqueryrow['display_name'];
                $responderid = $responderqueryrow['user_id'];
                echo '<div class="response"><a href="mypage.php?user='.$responderid.'">'.$respondername.'</a> said: '.stripslashes($responsesrow['comment']).'';

                if ($login->isLoggedIn()) {
                    // Has this user already reported this comment?
                    $reportedresponsequery = "SELECT * FROM report WHERE comment_id=$responseid AND reporter_id=$userid AND listing_id=$listingid";
                    $reportedresponse = mysql_query($reportedresponsequery) or die('SQL Error :: '.mysql_error());

                if (mysql_num_rows($reportedresponse) > "0") {
                echo'  <span class="small_gray">-Reported and under review-</span>';
                } else {
                echo'  <a href="item.php?item='.$listingid.'&report='.$responsesrow['comment_id'].'" class="small_graylink">Report</a>';
                    }
                }
                echo '</div>';  // Missing </div> inside while loop
                }

                echo '</div>';  // Ends <div class="responsecontainer">
                echo '<div class="clear"></div>';
                }

【讨论】:

  • 非常感谢您花时间看这个。我已经在这个网站上连续工作了 12 个小时,我只是看不出问题出在哪里。你完全正确。
  • 没问题。看了这么久的代码,很容易错过这些东西,多看几眼总是有帮助的。
【解决方案2】:

首先连接你的输出以获得对它的更多控制..

每次要打印内容时不要使用 echo:

echo '<div class="response"><a href="mypage.php?user='.$responderid.'">'.$respondername.'</a> said: '.stripslashes($responsesrow['comment']).'';

将您的输出集中在一个变量中:

$output = '<div class="response">';
$output .= '<a href="mypage.php?user='.$responderid.'">'.$respondername.'</a> ';
$output .= 'said: '.stripslashes($responsesrow['comment']);
$output .= '</div>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 2016-06-23
    • 2020-06-20
    • 1970-01-01
    • 2011-01-29
    • 2016-05-21
    • 2017-09-20
    • 1970-01-01
    相关资源
    最近更新 更多