gpt4 book ai didi

PHP线程评论分页

转载 作者:可可西里 更新时间:2023-11-01 08:52:08 25 4
gpt4 key购买 nike

我正在使用来自 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com