- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我发现这个类用于使用 php 和 MySQL 的线程评论:
<?php
class Threaded_comments
{
public $parents = array();
public $children = array();
/**
* @param array $comments
*/
function __construct($comments)
{
foreach ($comments as $comment)
{
if ($comment['parent_id'] === NULL)
{
$this->parents[$comment['id']][] = $comment;
}
else
{
$this->children[$comment['parent_id']][] = $comment;
}
}
}
/**
* @param array $comment
* @param int $depth
*/
private function format_comment($comment, $depth)
{
for ($depth; $depth > 0; $depth--)
{
echo "\t";
}
echo $comment['text'];
echo "\n";
}
/**
* @param array $comment
* @param int $depth
*/
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);
}
}
}
这适用于这个数组:
$comment = array( array('id'=>1, 'parent_id'=>NULL, 'text'=>'Parent'),
array('id'=>2, 'parent_id'=>1, 'text'=>'Child'),
array('id'=>3, 'parent_id'=>2, 'text'=>'Child Third level'),
array('id'=>4, 'parent_id'=>NULL, 'text'=>'Second Parent'),
array('id'=>5, 'parent_id'=>4, 'text'=>'Second Child')
);
结果:
$tc = new Threaded_comments($comment);
$tc->print_comments();
结果是:
Parent
Child
Child Third level
Second Parent
Second Child
这确实有效但是对于定义评论列表(html),我需要将所有 html 代码添加到 private function format_comment($comment, $depth)
中。这不是好方法,我认为需要将 html
与 php class
分开。
编辑:(这是我的显示/打印线程评论页面)
function _comments_($id,$type){
$DB_QUERY = mySqli::f("SELECT id,user,email,message,timestamp,parent_id,rfield_1,rfield_2,rfield_3,rfield_4,rfield_5 FROM " . NEWS_COMMENTS . " LEFT JOIN " . NEWS_REVIEWS . " ON " . NEWS_COMMENTS . ".id = " . NEWS_REVIEWS . ".cid WHERE
pid = ? AND type = ? AND approved = 1 ORDER BY timestamp DESC LIMIT 12", $id, $type);
foreach($DB_QUERY as $row){
$commentdata[] = $row;
}
return $commentdata;
}
$comments_list = _comments_('125','book');
foreach($comments_list as $row){
$comments[] = array(
'id' => $row['id'],
'parent_id' => $row['parent_id'],
'name' => $row['user'],
'text' => $row['message'],
'datetime' => $row['timestamp']
);
}
$tc = new Threaded_comments($comments);
$tc->print_comments();
在我的页面中,线程评论显示为 format_comment
并且有效。如果我需要使用来自 php 类的单独 html 来设计输出。
在这种情况下,我们如何将 html 代码与类分开?!
最佳答案
你是对的。
1) 最简单的解决方案是将数组传递给模板。此模板不包含逻辑,仅包含 html 和 php(例如,foreach 数组)。
2) 为了更好的分离,使用模型- View - Controller 模式:
采用这种分层结构,每一层都有自己的职责。从不在模型和 Controller 中使用 html。仅在模型中查询。
Laravel 和 CodeIgniter 等框架实现了这种模式。
关于php - 将 html 与 php 注释类分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32409664/
我像那样遍历数组。 NSArray *array = [[currentRaum raumattribute] allObjects]; NSString *compositeString =
我想找到所有引用这种模式的子字符串:一些字符+一些字符+第一个字符。现在我在 Python 2.7 中有了这个: T = "i was here" m = re.findall(r"([a-z])[a
我想使用与 tidyr 分开将一列字符串(例如 [1, 58, 10] )分成几列。我的问题是有时列较短(永远不会更长)。我在同一个数据框中有很多列有这个问题。 加载包 require(tidyr)
我正在开发一个具有图形用户界面的网络测试工具。我现在面临的问题是,我无法将基础数据与 GUI 类分开。该应用程序由一个 QMainWindow 组成,它随后生成多个其他 QDialogs 并具有一些
我经常听到“策略与机制分离”的口头禅,尤其是在 Unix 哲学的背景下。这是什么意思,有哪些具体的例子?什么时候/为什么是/不是一件好事? 最佳答案 它基本上是将需求或业务功能与技术实现分离。机制是技
我正在使用 writeToFile:atomically: 方法将一些加密数据写入文本文件。问题是,需要保存的文件必须是用户加密的文件,并带有我选择的扩展名。这是我到目前为止所拥有的: [encryp
我有这串 abcdef x y z 或这个 "ab cd ef" x y z 我正试图将其解析为 s1 = "abcdef" arr = ["x","y","z"] 或者 s1 = "ab cd e
这个问题已经有答案了: One big javascript file or multiple smaller files? [duplicate] (7 个回答) 已关闭 6 年前。 我有 4 种类
我有这样的事情 - function DetailCtrl($scope) { $scope.persons = [{ id: 1, name: "Mark"
在操作(复制/移动)包含合并单元格的范围时,我总是收到错误消息“您的粘贴与合并单元格重叠。请取消合并单元格,然后重试”。但是,当尝试使用 Range#breakApart 取消合并范围内的单元格时,我
我有一个包含一些 TextFields 的 TableView。所述 TextFields 的值链接到二维数组(NSMutableArrays 的 NSArray)中的某些位置。 一个初始的干净数组定
我定义了一个标签,其中一半需要在左侧,另一半文本需要在右侧。我怎样才能解决这个问题,让另一半拉对? 我添加了 margin-right 以使文本向右拉,但它与其他 div 不一致。
我正在尝试创建一个正则表达式来将 JavaScript 中的每个单词与 .(点)分开。 function myFunction() { var url = "in.k1.k2.k3.k4.com"
如何使用 CSS 将网站的正文/内容区域与背景分开。为了向您展示我的意思,请看附图。因此,两侧的背景将扩展到拥有超大显示器的人,但内容将始终保持相同大小。 谢谢,阿马尔 http://i.imgur.
有可能用 CSS 将两个背景图像对 Angular 分开吗? 我知道如何只用一张图片制作它,但我不能用两张图片制作它。 这是一个例子: |-------------| | /|
这是一个JSFiddle我创建了展示代码的外观。我将如何给予这些 它们之间是否存在间隙,没有一个元素低于另一个元素? .main-content { width: 50%; float: le
我正在处理具有这样数据的项目(我使用带有 python 的 pandas 框架): days rain 0 1 2 0 3 1 1
我正在尝试编写一个宏来获取信息并将该信息发送到另一个函数,方法是将原始 va_list 拆分为字符串,然后从原始 va_list 生成另一个 va_list。 下面是我的代码。 调用宏 /* Usag
我需要来自 SharedToDomains 和 SharedFromDomains 的键和值数据。我想打印这些值。 var LogResponse = DeserializeFromJson(sLog
我现在正在使用 Alamofire 来发出发布请求。我首先在 ViewController 中构建它并开始工作。但后来我试图通过在另一个 class 中构建它来分离它。我使用 singleton 并且
我是一名优秀的程序员,十分优秀!