草庐IT

PHP线程评论分页

coder 2023-10-25 原文

我正在使用来自 http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/ 的线程评论而且我不知道如何实现分页系统。如果有人因为我正在寻找解决方案但没有找到任何东西而可以指出我的方向或其他东西......

public $parents  = array();
public $children = array();

function __construct($comments)
{
    foreach ($comments as $comment)
    {
        if ($comment['parent_id'] === NULL)
        {
            $this->parents[$comment['id']][] = $comment;
        }
        else
        {
            $this->children[$comment['parent_id']][] = $comment;
        }
    }
}

private function format_comment($comment, $depth)
{
     If($depth == 0){
        ?>
        <br /><?php echo $comment['name']; ?><br /><?php echo $comment['datetime']; ?><br /><?php echo $comment['text']; ?></div>
        <a href="javascript:toggleDiv('<?php echo $comment['id']; ?>');">Raspunde</a>
        <div id="<?php echo $comment['id']; ?>" style="display: none;">
        The content in this div will hide and show (toggle) when the toggle is pressed. 
        </div>
        <?php
     }

     If($depth > 0){
        ?>
        <div style="margin-left: 20px;">
        <br /><?php echo $comment['name']; ?><br /><?php echo $comment['datetime']; ?><br /><?php echo $comment['text']; ?></div>
        </div>
        <?php
     }   


}

private function print_parent($comment, $depth = 0)
{
    foreach ($comment as $c)
    {
        $this->format_comment($c, $depth);

        if (isset($this->children[$c['id']]))
        {
            $this->print_parent($this->children[$c['id']], $depth + 1);
        }
    }
}

public function print_comments()
{
    foreach ($this->parents as $c)
    {
        $this->print_parent($c);
    }
}}

$username = "Netra";
$SQL = "SELECT * FROM profile_comments WHERE name = '$username' ORDER BY datetime DESC";
$result = mysql_query($SQL) or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {
    $id = $row['id'];
    $parent_id = $row['parent_id'];
    $name = $row['name'];
    $text = $row['text'];
    $datetime = $row['datetime'];

    $comments[] = array(
        'id' => $id,
        'parent_id' => $parent_id,
        'name' => $name,
        'text' => $text,
        'datetime' => $datetime
    );
}

    $threaded_comments = new Threaded_comments($comments);

    $threaded_comments->print_comments();

最佳答案

分页只会改变您查询中的两件事。它将根据当前页面设置不同的 LIMIT 和 OFFSET。涉及到几个部分,主要是知道offset。这很简单,它始终是 (PAGE_NUMBER * NUMBER_PER_PAGE) - NUMBER_PER_PAGE。然后,您可以根据当前页面动态更改您的 sql!

看起来像这样:

<?php 

class Pagination{

    public $total_results;
    public $total_pages;
    public $per_page;
    public $offset;
    public $page;

    public function __construct($per_page=20, $total_results=0, $page=1){
        $this->per_page = $per_page;
        $this->total_results = $total_results;
        $this->page = $page;
        $this->set_total_pages();
        $this->set_offset();
        $this->prepare_displays();
    }

    public function set_total_pages(){
        $this->total_pages = ceil($this->total_results / $this->per_page);
    }

    public function set_offset(){
        $this->offset = ($this->page * $this->per_page) - $this->per_page;
    }

    public function has_next_page(){
        if($this->page < $this->total_pages){
            return true;
        }else{
            return false;
        }
    }

    public function has_previous_page(){
        if($this->total_pages > 1 && $this->page > 1){
            return true;
        }else{
            return false;
        }
    }


    public function check_page_exists(){
        return (($this->total_pages > 0) && ($this->page > $this->total_pages)) || $this->page < 1 ? false : true;
    }


}




?>

关于PHP线程评论分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12235489/

有关PHP线程评论分页的更多相关文章

  1. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  2. ruby-on-rails - 简单的 Ruby on Rails 问题——如何将评论附加到用户和文章? - 2

    我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。

  3. ruby - 如何让Ruby捕获线程中的语法错误 - 2

    我正在尝试使用ruby​​编写一个双线程客户端,一个线程从套接字读取数据并将其打印出来,另一个线程读取本地数据并将其发送到远程服务器。我发现的问题是Ruby似乎无法捕获线程内的错误,这是一个示例:#!/usr/bin/rubyThread.new{loop{$stdout.puts"hi"abc.putsefsleep1}}loop{sleep1}显然,如果我在线程外键入abc.putsef,代码将永远不会运行,因为Ruby将报告“undefinedvariableabc”。但是,如果它在一个线程内,则没有错误报告。我的问题是,如何让Ruby捕获这样的错误?或者至少,报告线程中的错误?

  4. ruby - 如何在 ruby​​ 中运行后台线程? - 2

    我是ruby​​的新手,我认为重新构建一个我用C#编写的简单聊天程序是个好主意。我正在使用Ruby2.0.0MRI(Matz的Ruby实现)。问题是我想在服务器运行时为简单的服务器命令提供I/O。这是从示例中获取的服务器。我添加了使用gets()获取输入的命令方法。我希望此方法在后台作为线程运行,但该线程正在阻塞另一个线程。require'socket'#Getsocketsfromstdlibserver=TCPServer.open(2000)#Sockettolistenonport2000defcommandsx=1whilex==1exitProgram=gets.chomp

  5. ruby - Rails 开发服务器、PDFKit 和多线程 - 2

    我有一个使用PDFKit呈现网页的pdf版本的Rails应用程序。我使用Thin作为开发服务器。问题是当我处于开发模式时。当我使用“bundleexecrailss”启动我的服务器并尝试呈现任何PDF时,整个过程会陷入僵局,因为当您呈现PDF时,会向服务器请求一些额外的资源,如图像和css,看起来只有一个线程.如何配置Rails开发服务器以运行多个工作线程?非常感谢。 最佳答案 我找到的最简单的解决方案是unicorn.geminstallunicorn创建一个unicorn.conf:worker_processes3然后使用它:

  6. ruby - .gemrc 评论? - 2

    这是一个基本问题.gemrc文件中是否允许注释?如果是,你会怎么做?我这里查了没用docs.rubygems.org/read/chapter/11 最佳答案 文档说:Theconfigfileitselfisin’’’YAML’’’format.这意味着您可以拥有commentsstartingwith#,例如:#Ilikedocsrdoc:--inline-source--line-numbers 关于ruby-.gemrc评论?,我们在StackOverflow上找到一个类似的问题

  7. ruby - 每个页面上的 Jekyll 分页 - 2

    据我们所知,Jekyll默认分页仅支持index.html,我想创建blog.html并在那里包含分页。有什么解决办法吗? 最佳答案 如果您创建一个名为/blog的目录并在其中放置一个index.html文件,那么您可以向_config.yml表示paginate_path:"blog/page:num"。不是使用根文件夹中的默认index.html作为分页器模板,而是使用/blog/index.html。分页器将根据需要生成类似/blog/page2/和/blog/page3/的页面。这将使您到达yourwebsite.com/b

  8. ruby - Ruby 1.9.1 中的 native 线程,对我有什么好处? - 2

    所以,Ruby1.9.1现在是declaredstable.Rails应该与它一起工作,并且正在慢慢地将gem移植到它。它具有native线程和全局解释器锁(GIL)。自从GIL到位后,原生线程是否比1.9.1中的绿色线程有任何优势? 最佳答案 1.9中的线程是原生的,但它们被“放慢了速度”,一次只允许一个线程运行。这是因为如果线程真的并行运行,它会混淆现有代码。优点:IO现在在线程中是异步的。如果一个线程阻塞在IO上,那么另一个线程将继续执行直到IO完成。C扩展可以使用真正的线程。缺点:任何非线程安全的C扩展都可能存在使用Thre

  9. ruby - 使写入文件线程安全 - 2

    我在一个ruby​​文件中有一个函数可以像这样写入一个文件File.open("myfile",'a'){|f|f.puts("#{sometext}")}这个函数在不同的线程中被调用,使得像上面这样的文件写入不是线程安全的。有谁知道如何以最简单的方式使这个文件写入线程安全?更多信息:如果重要的话,我正在使用rspec框架。 最佳答案 您可以通过File#flock给锁File.open("myfile",'a'){|f|f.flock(File::LOCK_EX)f.puts("#{sometext}")}

  10. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

随机推荐