当面对这样一个复杂的结构时,有时最好创建一个面向对象的解决方案,然后使用对象来创建您需要的数组。
例如,根据您的上述情况,我可能会定义以下类:
class Comment{
protected $id;
protected $children;
protected $content;
public function __construct( $id, $content ){
$this->id = $id;
$this->content = $content;
$this->children = array();
}
public function addChild( $child ){
$this->children[] = $child;
}
}
现在,我们使用此对象将您的数据库转移到工作内存中,如下所示:
$workingMemory = array(); //a place to store our objects
$unprocessedRows = array(); //a place to store unprocessed records
// here, add some code to fill $unproccessedRows with your database records
do{
$row = $unprocessedRows; //transfer unprocessed rows to a working array
$unprocessedRows = array(); //clear unprocessed rows to receive any rows that we need to process out of order.
foreach( $row as $record ){
$id = $record[0]; //assign your database value for comment id here.
$content = $record[1]; //assign your database value for content here.
$parentId = $record[2]; //assign your database value for parent id here
$comment = new Comment( $id, $content );
//for this example, we will refer to unlinked comments as
//having a parentId === null.
if( $parentId === null ){
//this is just a comment and does not need to be linked to anything, add it to working memory indexed by it's id.
$workingMemory[ $id ] = $comment;
}else if( isset( $workingMemory[ $parentId ] ) ){
//if we are in this code block, then we processed the parent earlier.
$parentComment = $workingMemory[ $parentId ];
$parentComment->addChild( $comment );
$workingMemory[ $id] = $comment;
}else{
//if we are in this code block, the parent has not yet been processed. Store the row for processing again later.
$unprocessedRows[] = $record;
}
}
}while( count( $unprocessedRows ) > 0 );
一旦所有的 unprocessedRows 都完成了,您现在可以将您的 cmets 表示完全存储在变量 $workingMemory 中,并且该数组的每个单元格都是一个 Comment 对象,它有一个 $id、一个 $content 和指向所有孩子们 $cmets。
我们现在可以遍历这个数组并制作我们想要的任何数据数组或表格。我们必须记住,我们存储数组的方式,我们可以直接访问 $workingMemory 数组中的任何注释。
如果我使用它为网站生成 HTML,我将遍历 workingMemory 数组并仅处理父 cmets。然后每个进程将遍历子进程。通过从父母而不是孩子开始,我们可以保证我们不会两次处理相同的评论。
我会更改我的 Comment 类以使其更容易:
class Comment{
protected $id;
protected $children;
protected $content;
protected $isRoot;
public function __construct( $id, $content ){
$this->id = $id;
$this->content = $content;
$this->children = array();
$this->isRoot = true;
}
public function addChild( $child ){
$child->isRoot = false;
$this->children[] = $child;
}
public function getChildren(){ return $this->children; }
public function getId(){ return $this->id; }
public function getContent(){ return $this->content; }
}
更改后,我可以按如下方式创建我的 HTML:
function outputCommentToHTML( $aComment, $commentLevel = 0 ){
//I am using commentLevel here to set a special class, which I would use to indent the sub comments.
echo "<span class'comment {$commentLevel}' id='".($aComment->getId())."'>".($aComment->getContent())."</span>";
$children = $aComment->getChildren();
foreach( $children as $child ){
outputCommentToHTML( $child, $commentLevel + 1 );
}
}
foreach( $workingMemory as $aComment ){
if( $aComment->isRoot === true ){
outputCommentToHTML( $aComment );
}
}
这会将数据库列转换为您需要的格式。例如,如果我们有以下数据:
comment_id content parent_id
1 xxx 0
2 xxx 0
3 xxx 1
4 xxx 3
5 xxx 4
6 xxx 3
... ... ...
它会以 HTML 格式输出:
Comment_1
Comment_3
Comment_4
Comment_5
Comment_6
Comment_2
这是在函数中递归完成的,它在移动到评论 2 之前完全处理 Comment_1。它还在移动到评论 2 之前完全处理 Comment_3,这就是评论 4、5 和 6 在评论 2 之前得到输出的方式。
上面的例子对你有用,但如果是我的个人项目,我不会混合线性和面向对象的代码,所以我会创建一个代码工厂来将 Comments 转换为 HTML。工厂从源对象生成数据字符串。您可以创建一个充当 HTML 工厂的对象,以及充当 SQL 生成器的另一个工厂,并且通过使用这样的解决方案对对象进行分层,您可以创建一个完全面向对象的解决方案,这对于一般人来说更容易理解阅读器,有时甚至是非编码人员来制作这样的东西:
//these definition files get hidden and tucked away for future use
//you use include, include_once, require, or require_once to load them
class CommentFactory{
/**** other Code *****/
public function createCommentArrayFromDatabaseRecords( $records ){
/*** add the data conversion here that we discussed above ****/
return $workingMemory;
}
}
class HTMLFactory{
public function makeCommentTableFromCommentArray( $array ){
$htmlString = "";
foreach( $array as $comment ){
if( $comment->isRoot ){
$htmlString .= $this->getHTMLStringForComment( $comment );
}
}
return $htmlString;
}
private function getHTMLStringForComment( $comment, $level=0 ){
/*** turn your comment and all it's children into HTML here (recursively) ****/
return $html;
}
}
如果操作得当,它可以清理您的活动代码文件,使其读起来几乎像这样的指令列表:
//let database be a mysqli or other database connection
//let the query function be whatever method works for your database
// of choice.
//let the $fetch_comment_sql variable hold your SQL string to fetch the
// comments
$records = $database->query( $fetch_comment_sql )
$comFactory = new CommentFactory();
$commentArray = $comFactory->createCommentArrayFromDatabaseRecords( $records );
$htmlFactory = new HTMLFactory();
$htmlResult = $htmlFactory->makeCommentTableFromCommentArray( $commentArray );
echo $htmlResult;