数据表如图上部分显示,想用mysql实现图下半部分效果,新手上路,求大神帮忙$result = mysql_query("select * from table order by sj desc"); 然后接下来应该怎么写输出的呢?
4 回答
Smart猫小萌
TA贡献1911条经验 获得超7个赞
这个是你要的吗?
<?php
$data = [
[
'id' => 1,
'sj' => '2018-03-24 14:30:00',
'title' => '第一条记录',
],
[
'id' => 2,
'sj' => '2018-03-24 16:30:00',
'title' => '第二条记录',
],
[
'id' => 3,
'sj' => '2018-03-25 10:30:00',
'title' => '第三条记录',
],
];
$res = [];
foreach ($data as $item) {
$date = substr($item['sj'], 0, 10);
$res[$date][] = $item;
}
var_dump($res);
// 更新 没有实测
$res = [];
$result = mysql_query("select * from table order by sj desc");
while ($item = mysql_fetch_assoc($result)) {
$date = substr($item['sj'], 0, 10);
$res[$date][] = $item;
}
var_dump($res);
// 再做一次循环输出,应该是你要的结果
foreach($res as $k => $v){
echo date('m月d日',strtotime($k));
foreach($item as $key => $value){
echo $value['title'];
}
}
www说
TA贡献1775条经验 获得超8个赞
$oMysqli = new Mysqli($sHost,$sUser,$sPwd,$sDbname,$sPort);
$sSql = "select * from t_test1 order by sj desc";
$rResult = $oMysqli->query($sSql);
$aDate = [];
while($aResult = $rResult->fetch_assoc())
{
$sCurrentDate = date("m月d日",strtotime($aResult['sj']));
if(!in_array($sCurrentDate,$aDate))
{
var_dump($sCurrentDate);
$aDate[] = $sCurrentDate;
}
var_dump($aResult['title']);
}
$oMysqli->close();
慕标5832272
TA贡献1966条经验 获得超4个赞
@qiaoweizhen foreach($item as $key => $value) 里面的$item应该是$v,这样就可以实现了,多谢
$res = [];
$result = mysql_query("select * from table order by sj desc");
while ($item = mysql_fetch_assoc($result)) {
$date = substr($item['sj'], 0, 10);
$res[$date][] = $item;
}
var_dump($res);
// 再做一次循环输出,应该是你要的结果
foreach($res as $k => $v){
echo date('m月d日',strtotime($k));
foreach($v as $key => $value){
echo $value['title'];
}
}
- 4 回答
- 0 关注
- 862 浏览
添加回答
举报
0/150
提交
取消