Php 如何从mysql查询中同时分组行?
我有以下代码:Php 如何从mysql查询中同时分组行?,php,mysql,while-loop,Php,Mysql,While Loop,我有以下代码: //set $message and run database query $message .= '<body style="margin: 0;">'; $message .= '<div style="padding: 0 20px;">'; $message .= '<span style="display: block; font-size: 22px; font-weight: bold; margin: 25px 0 -15px 0;"
//set $message and run database query
$message .= '<body style="margin: 0;">';
$message .= '<div style="padding: 0 20px;">';
$message .= '<span style="display: block; font-size: 22px; font-weight: bold; margin: 25px 0 -15px 0;"> User Activity on ' . $website . ' for ' . $yesterday . '</span>';
//query the database for today's history
$result = mysql_query("SELECT * FROM user_history WHERE date = '$yesterday' ORDER by name, time, title")
or die(mysql_error());
$old_user = '';
while($row = mysql_fetch_array( $result )) {
$new_user = $row['uid'];
if ($new_user != $old_user) {
$message .= '<br /><br /><hr />' . '<span style="font-size: 18px; font-weight: bold;">' . $row['name'] . '</span>' . '<br />' . $row['company'] . '<br />' . $row['email'] . '<br />' . $row['phone'] . '<br /><br />';
$old_user = $new_user;
}
$message .= '<ul><li>' . $row['time'] .
'<ul><li>' . $row['title'] . ' (<a href="' . $row['url'] . '">' . $row['url']. '</a> )' . '</li></ul>' . '<br /><br />' .
'</li></ul>';
}
//if no user activity, simply say so
if($new_user == "") {
$message .= '<br /><br /><hr /><span style="font-size: 18px; font-weight: bold;">No user activity to report</span>';
}
$message .= '</div>';
$message .= '<div style="position: fixed; bottom: 0; height: 40px; width: 100%; margin: 20px 0 0 0; background: #000; color: #FFF; line-height: 40px; padding: 0 20px;">' .
'Report generated ' . $date . ' ' . $time .
'</div>';
$message .= '</body>';
//设置$message并运行数据库查询
$message.='';
$message.='';
$message.='User Activity on'$网站。”为了‘’$昨天",;
//查询数据库以了解今天的历史记录
$result=mysql_query(“从用户_历史记录中选择*,其中日期=“$forday”按名称、时间、标题排序”)
或者死(mysql_error());
$old_user='';
while($row=mysql\u fetch\u数组($result)){
$new_user=$row['uid'];
如果($new\u user!=$old\u user){
$message.='
'.$row['name'].'
'.$row['company'].
'.$row['email'.
'.$row['phone'.
;
$old\u user=$new\u user;
}
$message.='- 。$row['time']。
“
- ”.$row['title'].()”.
.“
”。
“
”;
}
//如果没有用户活动,简单地说
如果($new_user==“”){
$message.='
无需报告的用户活动';
}
$message.='';
$message.=''。
“已生成报告”$日期$时间
'';
$message.='';
它在邮件功能中工作得很好。像这样的输出电子邮件组
约翰·多伊约翰·多伊公司
johndoe@johndoeco.com
800-555-0000
*下午1:30
*视频1
*下午1:47
*视频2
*下午1:47
*视频3
==================================
相反,我希望这样输出:
约翰·多伊
约翰·多伊公司
johndoe@johndoeco.com
800-555-0000
*下午1:30
*视频1
*1:47下午
*视频2
*视频3
==================================
有人能告诉我如何做到这一点吗?谢谢 只需使用与为不同用户(
if($new_user!=$old_user){
)相同的技术跟踪当前时间值,但要跟踪时间值
差不多
if ($new_user != $old_user) {
//your existing code
$old_time = null;
$end_tag = false;
}
if ($old_time == null || $row['time'] != $old_time) {
$old_time = $row['time'];
$message .= '<ul><li>' . $row['time'];
$end_tag = true;
}
$message .= '<ul><li>' .
$row['title'] . ' (<a href="' .
$row['url'] . '">' . $row['url']. '</a> )' .
'</li></ul>';
if ($end_tag) {
$message .= "</li></ul>";
$end_tag = false;
}
if($new\u user!=$old\u user){
//您现有的代码
$old_time=null;
$end_tag=false;
}
如果($old_time==null | |$row['time']!=$old_time){
$old_time=$row['time'];
$message.='- 。$row['time'];
$end_tag=true;
}
$message.='
- '。
$row['title'].()'。
“
”;
如果($end_标记){
$message.=“
”;
$end_tag=false;
}
虽然这种编码方式很快就开始变得难看,应该被分解成函数。这有帮助吗
$old_time = '';
$video_cache = '';
while($row = mysql_fetch_array( $result ))
{
$new_user = $row['uid'];
if ($new_user != $old_user)
{
$message .= '<br /><br /><hr />' . '<span style="font-size: 18px; font-weight: bold;">' . $row['name'] . '</span>' . '<br />' . $row['company'] . '<br />' . $row['email'] . '<br />' . $row['phone'] . '<br /><br />';
$old_user = $new_user;
}
$new_time = $row['time'];
if ($old_time != $new_time)
{
$video_cache = '<ul><li>' . $old_time . '</li>' . $video_cache . '</ul>';
$message .= $video_cache;
$video_cache = '';
}
$video_cache .= '<li>' . $row['title'] . ' (<a href="' . $row['url'] . '">' . $row['url']. '</a> )' . '</li>';
}
$old_time='';
$video_cache='';
while($row=mysql\u fetch\u数组($result))
{
$new_user=$row['uid'];
如果($new\u user!=$old\u user)
{
$message.='
'.$row['name'].'
'.$row['company'].
'.$row['email'.
'.$row['phone'.
;
$old\u user=$new\u user;
}
$new_time=$row['time'];
如果($old\u time!=$new\u time)
{
$video\u cache='- '.$old\u time.
'.$video\u cache.
';
$message.=$video\u缓存;
$video_cache='';
}
$video_cache.=''.$row['title']..()'. ';
}
数据库结构是:uid | | name | | | | company | | | email | | | phone | | date | | time | | url | | | | | | | | | | title | | | | |我知道你并不是在问这个问题,但我想说的你已经解决了你自己的问题:)你对老用户和新用户使用的技巧也应该解决你的计时问题,不是吗?@pelshoff-当我在if语句中放入$row['time']时,它没有正确输出-只给出了一次(在本例中,有两个不同的时间)这有一些奇怪的结果,没有显示每个用户的所有记录。我想我知道我做错了什么。我真的不在乎实际输出,也不在乎到达那里的方式。我更新了它;这是否更符合您的期望?我必须更改此代码…如果($old_time!=$new_time){$video\u cache='- '.$row['time'].$old\u time. '.$video\u cache.