是否可以<?php
if(isset($_REQUEST['submit_album']))
{
$album_titlee=$_REQUEST['album_titlee'];
$user_id=$_SESSION['ses_userid'];
$sql=mysqli_query($conn,"INSERT INTO tbl_album(user_id,album_title,album_status) VALUES ('".$user_id."','$album_titlee','Y')");
echo("<script type='text/javascript'>$.colorbox({inline:true,width:'50%', href:'#inline_content'});</script>");
}
?>
foreach的结果只有一次?
这就是我想要实现的目标:
echo
我的代码
18-sept-2016 | 08:00 AM | some text
11:00 AM | some text
19-sept-2016 | 09:00 AM | some text
09:10 AM | some text
12:00 AM | some text
我目前的结果:
<?php if (!empty($notification)): ?>
<table class="table" id="notification_table">
<tbody>
<?php foreach ($notification as $row): ?>
<tr>
<td nowrap>
<?php echo date('d M',strtotime($row['created_on'])) ?>
</td>
<td><?php echo date('h:m A', strtotime($row['created_on'])) ?></td>
<td>
<?php echo $row['notification_text'] ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php else: ?>
<div class="alert alert-warning">
No notification
</div>
<?php endif ?>
我觉得如果我18-sept-2016 | 08:00 AM | some text
18-sept-2016 | 11:00 AM | some text
19-sept-2016 | 09:00 AM | some text
19-sept-2016 | 09:10 AM | some text
19-sept-2016 | 12:00 AM | some text
只有一次日期,它看起来比我目前的效果更有效,但我不知道怎么做
答案 0 :(得分:1)
简单就像这样:
$dates = [];
foreach ($notification as $row)
$date = date('d M',strtotime($row['created_on']));
if( !in_array($dates, $date ) ){
echo $date;
$dates[] = $date;
}
.....
}
检查数据是否在日期数组中,如果没有,则回显并将其存储在日期数组中以供下次使用。
答案 1 :(得分:0)
另一种方法是在temp变量中保存最后一个日期,并将其与当前迭代日期进行比较:
<?php if (!empty($notification)):
$date = ''; ?>
<table class="table" id="notification_table">
<tbody>
<?php foreach ($notification as $row): ?>
<tr>
<td nowrap>
<?php if($date != $row['created_on']){
echo date('d M',strtotime($row['created_on']));
$date = $row['created_on'];
} ?>
</td>
<td><?php echo $row['notification_text'] ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php else: ?>
<div class="alert alert-warning">No notification</div>
<?php endif ?>
答案 2 :(得分:0)
首先初始化一个像$is_same_date = ''
这样的变量空白,我按以下方式做出答案,因为你的数据是序列化的。
制作if条件以检查您的数据是否已打印,如果它在$is_same_date
中,则无需再次显示日期,否则您必须显示日期并将日期存储到{{ 1}}。
$is_same_date
答案 3 :(得分:0)
你可以用这种方式获得它
<tbody>
<!-- define $previous_value variable here to keep tmp value -->
<?php $previous_value = ""; ?>
<?php foreach ($notification as $row): ?>
<tr>
<td nowrap>
<?php
if($previous_value != $row['created_on']){
echo date('d M',strtotime($row['created_on']));
}
$previous_value = $row['created_on'];
?>
</td>
<td><?php echo date('h:m A', strtotime($row['created_on'])) ?></td>
<td>
<?php echo $row['notification_text'] ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
答案 4 :(得分:0)
这将有助于日期未在输入数组中排序,如下所示:
<?php
$notification=array(0=>array('created_on'=> '18-sept-2016 08:00 AM','notification_text'=>'some text1'),
1=>array('created_on'=> '19-sept-2016 09:10 AM','notification_text'=>'some text2'),
2=>array('created_on'=> '19-sept-2016 09:00 AM','notification_text'=>'some text3'),
3=>array('created_on'=> '18-sept-2016 11:00 AM','notification_text'=>'some text4'),
4=>array('created_on'=> '19-sept-2016 12:00 AM','notification_text'=>'some text5'));
$new_array=array();
foreach($notification as $index => $value){
$item=array();
$item['time']=date('h:m A', strtotime($value['created_on']));
$item['text']=$value['notification_text'];
$new_array[date('d M',strtotime($value['created_on']))][]=$item;
}
?>
<?php if (!empty($new_array)): ?>
<table class="table" id="notification_table">
<tbody>
<?php foreach ($new_array as $index=>$row): ?>
<?php foreach ($row as $index_item=>$row_item): ?>
<tr>
<td nowrap>
<?php if($index_item ==0) echo $index ?>
</td>
<td><?php echo $row_item['time'] ?></td>
<td>
<?php echo $row_item['text'] ?>
</td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
</tbody>
</table>
<?php else: ?>
<div class="alert alert-warning">
No notification
</div>
<?php endif ?>