我正在尝试将其他人的代码片段插入到我自己的更大的脚本中。我需要提取每条评论的评论ID。
这是输出代码:
<?php
$comments = array();
$result2 = mysql_query("SELECT * FROM `comments` WHERE `page_id` = '$pageID' ORDER BY `id` ASC");
while($row2 = mysql_fetch_array($result2))
{
$comments[] = new Comment($row2);
}
print_r($comments);
$i = 0;
foreach($comments as $c) {
echo $c->markup();
echo $i."<br>";
$currCommID = $c->id;
echo "<br/>current id is: ".$currCommID;
$i++;
}
正如预期的那样,收到错误:注意:未定义的属性:Comment :: $ id
这是我用print_r获得的数据($ comments); - 我可以看到我想要的ID,但我无法找到它们。
Array
(
[0] => Comment Object
(
[data:Comment:private] => Array
(
[0] => 2
[id] => 2
[1] => gocat
[name] => gocat
[2] => 857
[page_id] => 857
[3] => this is some other comment
[body_txt] => this is some other comment
[4] => 2015-10-29 09:35:53
[dt] => 2015-10-29 09:35:53
[5] => 0
[up] => 0
[6] => 0
[down] => 0
)
)
[1] => Comment Object
(
[data:Comment:private] => Array
(
[0] => 8
[id] => 8
[1] => lolcat
[name] => lolcat
[2] => 857
[page_id] => 857
[3] => funny comment
[body_txt] => funny comment
[4] => 2015-10-29 09:35:53
[dt] => 2015-10-29 09:35:53
[5] => 0
[up] => 0
[6] => 0
[down] => 0
)
)
)
在这种情况下,我想要的值是2和8。
是的,我可以自己进行查询。我想保留它的原因是markup();功能。它输出的预测结果完全符合我的要求。
不幸的是,我不了解这个阵列的结构,希望你能对它有所了解。 感谢。
更新:抱歉,由于我被标记为重复,因此无法发布回答。 TBH,我在其他帖子中找不到解决方案。
所以,我编辑了注释类,如下所示:
<?php
private $data = array();
public function __construct($row)
{
$this->data = $row;
}
?>
现在我有:
<?php
private $data = array();
public function __construct($row)
{
$this->data = $row;
}
public function commentids()
{
$e = &$this->data;
return $e['id'];
}
?>
然后我将输出代码更改为:
<?php
$i = 0;
foreach($comments as $c) {
echo $c->markup();
$currCommID = $c->commentids();
echo "<br/>current id is: ".$currCommID;
$i++;
}
?>