我在使用自定义库在Codeigniter 3中创建线程注释时遇到了一些麻烦。
当我在控制器中手动声明一个数组时,库正在工作,但是当我尝试从模型中传递一个数组时,它没有。
使用编码到控制器中的数组的示例:
$comments = array(
array (
'first_name' => 'James',
'last_name' => 'Smith',
'country' => 'UK',
'profile' => 'avatar87.jpg',
'id' => '1',
'member_id' => '18',
'policy_id' => '6',
'comment' => '<p>This is my comment.Do you agree?</p>',
'submitted' => '2015-09-22 07:20:21',
'parent_id' => NULL),
array (
'first_name' => 'Peter',
'last_name' => 'Green',
'country' => 'Australia',
'profile' => 'avatar88.jpg',
'id' => '2',
'member_id' => '18',
'policy_id' => '6',
'comment' => '<p>This is my comment.Do you agree?</p>',
'submitted' => '2015-09-22 07:20:21',
'parent_id' => '1'),
array (
'first_name' => 'Ollie',
'last_name' => 'Ford',
'country' => 'Australia',
'profile' => 'avatar85.jpg',
'id' => '3',
'member_id' => '18',
'policy_id' => '6',
'comment' => '<p>This is my comment.Do you agree?</p>',
'submitted' => '2015-09-22 07:20:21',
'parent_id' => '1'),
);
$this->data['comments'] = $comments;
以上一切正常,但是当我尝试这个时:
$comments = $this->question_model->get_policy_comments($id);
$this->data['comments'] = $comments;
它不起作用。
这是从模型返回的$ comments数组。
array (size=3)
0 =>
array (size=10)
'first_name' => string 'Ollie' (length=5)
'last_name' => string 'Falle' (length=5)
'country' => string 'Australia' (length=9)
'profile' => string 'avatar87.jpg' (length=12)
'id' => string '1' (length=1)
'member_id' => string '18' (length=2)
'policy_id' => string '6' (length=1)
'comment' => string '<p>This is my comment.Do you agree?</p>' (length=39)
'submitted' => string '2015-09-22 07:20:21' (length=19)
'parent_id' => string 'NULL' (length=4)
1 =>
array (size=10)
'first_name' => string 'Ollie' (length=5)
'last_name' => string 'Falle' (length=5)
'country' => string 'Australia' (length=9)
'profile' => string 'avatar87.jpg' (length=12)
'id' => string '2' (length=1)
'member_id' => string '18' (length=2)
'policy_id' => string '6' (length=1)
'comment' => string '<p>Blah blah blah</p>' (length=21)
'submitted' => string '2015-09-22 15:00:00' (length=19)
'parent_id' => string '1' (length=1)
2 =>
array (size=10)
'first_name' => string 'Ollie' (length=5)
'last_name' => string 'Falle' (length=5)
'country' => string 'Australia' (length=9)
'profile' => string 'avatar87.jpg' (length=12)
'id' => string '3' (length=1)
'member_id' => string '18' (length=2)
'policy_id' => string '6' (length=1)
'comment' => string '<p>Hello is it me your looking for.</p>' (length=39)
'submitted' => string '2015-09-22 13:39:46' (length=19)
'parent_id' => string '1' (length=1)
我错过了数组的东西吗?
任何帮助都将不胜感激。
供参考,这里是使用它的库。
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Threaded
{
public $parents = array();
public $children = array();
/**
* @param array $comments
*/
public function arrange($comments)
{
foreach ($comments as $comment)
{
if ($comment['parent_id'] === NULL)
{
$this->parents[$comment['id']][] = $comment;
}
else
{
$this->children[$comment['parent_id']][] = $comment;
}
}
$this->print_comments();
}
private function tabulate($depth)
{
for ($depth; $depth > 0; $depth--)
{
// echo "t";
}
}
/**
* @param array $comment
* @param int $depth
*/
private function format_comment($comment, $depth)
{
//echo "n";
$this->tabulate($depth+1);
echo "<li>";
echo $comment['first_name'];
echo $comment['last_name'];
echo $comment['country'];
echo $comment['profile'];
echo $comment['member_id'];
echo $comment['policy_id'];
echo $comment['comment'];
echo $comment['submitted'];
echo $comment['id'];
echo $comment['parent_id'];
echo "</li>";
}
/**
* @param array $comment
* @param int $depth
*/
private function print_parent($comment, $depth = 0)
{
$this->tabulate($depth);
echo "<ul>";
foreach ($comment as $c)
{
$this->format_comment($c, $depth);
if (isset($this->children[$c['id']]))
{
$this->print_parent($this->children[$c['id']], $depth + 1);
}
}
$this->tabulate($depth);
echo "</ul>";
}
private function print_comments()
{
foreach ($this->parents as $c)
{
$this->print_parent($c);
}
}
}
答案 0 :(得分:2)
不确定处理数据的逻辑是什么,但(除非是拼写错误)静态数组看起来像
array(data), array(data), array(data) //with an unopened )
并且从模型返回的数组是
array( array(data), array(data), array(data) )
你想要对数组做什么?你是如何从模型中获得输出的(即:var_dump();, print_r();)?另外你在第一个例子中如何定义$ comments?
答案 1 :(得分:0)
您可以在一行中分配评论
$this->data['comments'] = $this->question_model->get_policy_comments($id);
只需通过下面的代码查看它,看看数组的模式
echo '<pre>'; print_r($comments); die();
答案 2 :(得分:0)
所以现在阵列看起来是正确的。但是我认为你的问题是你在静态数组中实际传递一个NULL值,在返回的数组中你的'NULL'值实际上被解释为一个字符串而不是NULL。
在传递数组之前:
for($i=0; $i<count($comments); $i++){
foreach($comments[$i] as $k => $v){
if(strtoupper($v) === 'NULL'){
$comments[$k] = NULL;
}
}
}
或在你的模特中
public function arrange($comments)
{
foreach ($comments as $comment)
{
if ($comment['parent_id'] === NULL || $comment['parent_id']==='NULL')
{
$this->parents[$comment['id']][] = $comment;
}
else
{
$this->children[$comment['parent_id']][] = $comment;
}
}
$this->print_comments();
}