我正在构建一个php通知系统(有点像facebook的)。
它按原样运作,以我想要的方式列出所有事件。但是我想让它明白,如果它在同一个帖子上有3个以上的事件,它会将这些事件加入到一个通知中。
现在看起来像这样:
User 1 wrote on article-x
User 2 wrote on article-x
user 3 wrote on article-x
user 1 wrote on article-y
相反,我希望它像这样打印:
user 1 and 2 others wrote on article-x
试图阅读我需要做的事情,但没有运气。任何指针都非常感激。 它被这样查询:
使用完整代码进行编辑
$sesname = $_SESSION['user_name'];
$sesid = $_SESSION['full_name'];
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
$stmt = $dbh->prepare("select * from comments WHERE full_name = '$sesid' and checked ='1' ");
$stmt->bindParam(':full_name', $safe_name, PDO::PARAM_INT, 5);
$stmt->execute();
$result = $stmt -> fetchAll();
foreach( $result as $row ) {
$name = $row['name'];
$pid = $row['pid'];
$commentid = $row['id'];
$fullink = $row['fullink'];
$datetime = $row['dt'];
if ($name == $sesname){
} else {
echo "<div id='commentnote'><a href='http://{$fullink}'>{$name}</a>kommenterte ditt <a href='{$fullink}'>blogginnlegg</a><br><div class='updentry' style='color: red ! important; cursor: pointer; margin-top: -35px; position: absolute; right: 8px;'>x</div></div>";
}
}
尝试使用你的答案,但没有运气。谢谢你回答,我非常感谢:)
答案 0 :(得分:1)
将文章转换为多维数组怎么样? (举个例子,结构不是最优的,因为我不知道你有什么数据需要。)
if(count($post['comments']) >= 3) {
echo $post['user'][0] ."and". (count($post['comments'])-1). " others wrote on ..";
} else {
//Just one
}
答案 1 :(得分:0)
也许你可以像这样创建一个二维数组:
Array
(
[article-x] => Array
(
[0] => User1
[1] => User2
)
[article-y] => Array
(
[0] => User1
)
)
答案 2 :(得分:0)
如果您想在PHP中执行此操作,请执行以下操作:
$events = array();
foreach( $result as $row ) {
if( isset($events[$row['article']) ){
$events[$row['article']]['counter']++;
}else{
$row['counter'] = 0;
$events[$row['article']] = $row;
}
}
foreach( $events as $event ){
echo "<div id='commentnote'>{$event['user']}".($event['counter']?' and '.$event['counter'].' others':'')." wrote on {$event['article']}</div>";
}