【问题标题】:why the php function load in wrong place为什么php函数加载到错误的地方
【发布时间】:2021-05-22 16:19:56
【问题描述】:

您好,我的 PHP 代码有问题 我有一个功能可以为我网站上的每个帖子获取评论 该功能正在工作,但数据位于错误的位置 正如您在图片中看到的那样,cmets 在错误的位置,我需要将它们移动到图片中的位置

<?php
class posts{
static function getposts(){
    $query = database::query("select * from posts join frinds on frinds.user1_id = posts.user_id or 
 frinds.user2_id= posts.user_id  where frinds.user1_id = ? or frinds.user2_id = 
  ?",array($_COOKIE['uid'],$_COOKIE['uid']));
    if($query->rowCount() > 0){
        $rows=$query->fetchAll(PDO::FETCH_ASSOC);
        foreach($rows as $row){
             $id = $row['user_id'];
            // if($id != $_COOKIE['uid']){
                if($id != $_COOKIE['uid']){
                    $post_text = $row['text'];
                    $id= $row['id'];
                    echo posts::postbody($id,$post_text);
               
                  
                }
                
                
        }
        if($id == $_COOKIE['uid']){
            $query = database::query("select * from posts where user_id = ?",array($_COOKIE['uid']));
            if($query->rowCount() > 0){
                $rows=$query->fetchAll(PDO::FETCH_ASSOC);
                foreach($rows as $row){
                    $post_text = $row['text'];
                    echo posts::postbody($row['id'],$post_text);
                }
            }
          }
        }
    }
    static function postbody($id,$post_text){
   
    $body =" <div class='post'>
                    <div class='post_texts' ><p class='post_text'>$post_text</p>
                    
                    
                    </div>
                    <div class='comments'>
                   " .posts::comments($id)."
                   </div>
                    <form method='post'>
                    <input type='text' name='comment'>
                    <input type='submit' name='addcoment'>
                    </form>
                    
                    </div> ";
                    return $body;
   }
   static function creatpost(){
       $query = database::query("INSERT INTO `posts` (`user_id`, `text`) VALUES (?, 
 ?);",array($_COOKIE['uid'],$_POST['text']));
  }
  static function comments($id){
    $query = database::query("select * from comments join posts on comments.post_id = posts.id where 
  posts.id = ?",array($id));
    if($query->rowCount() > 0){
        $rows=$query->fetchAll(PDO::FETCH_ASSOC);
        foreach($rows as $row){
            $comment = $row['comment'];
        echo"<p>$comment</p>";
        }
        
    }
   }
 }
?>

如您所见 是我有帖子和

的地方 我应该在哪里有 cmets

【问题讨论】:

  • 请分享更多细节。您尝试过什么来解决问题?这与 CSS 有什么关系?
  • 我认为是设计问题
  • 请使用一些合理的代码缩进正确地格式化您的代码。当事情像那样到处都是时,真的很难理解发生在哪里。
  • 我在这个网站上更改格式我会修复它

标签: php html css


【解决方案1】:

问题是您的静态方法调用posts::comments($id) 已经使用echo 来输出cmets。虽然帖子正文存储在变量$body 中,但稍后才会回显。

您需要更改代码,以便在从数据库中收集 cmets 时不会立即回显它们。但是它们要么放置在输出缓冲区中,要么返回到调用范围。这样它们就可以成为您尝试创建的实际帖子正文的一部分。

换句话说:当调用posts::comments($id) 时,你期望得到一些你试图连接到你的$body 变量的东西。但是您的方法永远不会返回任何东西。它只创建立即发送到客户端的输出。因此,它永远不会在您尝试收集帖子正文的地方获得该变量的一部分。


更新:

您如何“收集”这些 cmets 以将它们全部归还?

static function comments($id){
    $query = database::query("select * from comments join posts on comments.post_id = posts.id where 
  posts.id = ?",array($id));

    if($query->rowCount() > 0) {
        $rows = $query->fetchAll(PDO::FETCH_ASSOC);

        $comment = [];
        foreach ($rows as $row) {
            $comments[] = $row['comment'];
        }
        return implode("\n", $comments);
    }
    return "<p>No comments so far...</p>;
}

【讨论】:

  • 那么我该怎么办
  • 如果我使用 return 它可以工作,但只有一行 @@
  • 当然,您需要收集该方法中的所有 cmets 并返回 所有 个。
  • 那么我怎样才能收集它们并将它们归还它们,请您出飞机^_^
  • 我添加了一个答案作为更新。
猜你喜欢
  • 1970-01-01
  • 2023-01-24
  • 2012-04-01
  • 2021-12-19
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多