我在Yii中使用PHP和来自数据库的动态数据进行通知。这是我的CSS代码
<style>
#notification_count
{
padding: 3px 7px 3px 7px;
background: #cc0000;
color: #ffffff;
font-weight: bold;
border-radius: 9px;
-moz-border-radius: 9px;
-webkit-border-radius: 9px;
position: absolute;
margin-top: -11px;
font-size: 11px;
}
</style>
这是HTML
<div id="notificationContainer" style="width:300px">
<div id="notificationTitle" style="padding: 8px; font-size: 13px; font-weight: bold; color:#000000; border-bottom: 1px solid #dddddd;">Notifications</div>
<div id="notificationsBody" class="notifications item-test" style="padding: 8px; color:#000000; overflow-y:auto;max-height:300px" >
<?php
if($count == 0){
$notif = '<div align="center" style="font-style:italic;color:#d3d3d3; padding-bottom:5px;">No Notification</div>';
echo $notif;
}else{
for($i=1; $i<=$count; $i++){
$notif_query = Yii::app()->db->createCommand("SELECT NOTIF_DETAIL FROM (SELECT NOTIF_DETAIL, ROWNUM AS RN FROM (SELECT NOTIF_DETAIL FROM TRN_NOTIFICATION WHERE RECIPIENT = '$user' AND SENDER != '$user' AND UNREAD = 'UNREAD' ORDER BY NOTIF_ID DESC)WHERE ROWNUM <= $i)WHERE RN = $i")->queryScalar();
$date = Yii::app()->db->createCommand("SELECT UPDATE_DATE FROM (SELECT UPDATE_DATE, ROWNUM AS RN FROM (SELECT UPDATE_DATE FROM TRN_NOTIFICATION WHERE RECIPIENT = '$user' AND SENDER != '$user' AND UNREAD = 'UNREAD' ORDER BY NOTIF_ID DESC)WHERE ROWNUM <= $i) WHERE RN = $i")->queryScalar();
$notif_value = Yii::app()->db->createCommand("SELECT NOTIF_ID FROM (SELECT NOTIF_ID, ROWNUM AS RN FROM (SELECT NOTIF_ID FROM TRN_NOTIFICATION WHERE RECIPIENT = '$user' AND SENDER != '$user' AND UNREAD = 'UNREAD' ORDER BY NOTIF_ID DESC)WHERE ROWNUM <= $i) WHERE RN = $i")->queryScalar();
$notif = '<div>'.$notif_query.'</div>'; ?>
<div style="border-bottom: 1px solid #dddddd;" name="notificationValue" id="notificationValue" value="<?php echo $notif_value; ?>" method="POST"><?php echo $notif; ?><div align="right" style="color:grey; font-style:italic;"><?php echo $date; ?></div></div>
<?php
}
}
?>
</div>
<div id="notificationFooter" style="background-color: #e9eaed; text-align: center; font-weight: bold; padding: 8px; font-size: 12px; border-top: 1px solid #dddddd;"><a href="<?php echo Yii::app()->baseUrl; ?>/index.php?r=TrnNotification/admin">See All</a></div>
</div>
这是我重定向到我的控制器的JavaScript
$(function(){
$('#notificationValue').on('click',function(){
window.location.href = "<?php echo Yii::app()->baseurl; ?>/index.php?r=TrnNotification/notif";
});
});
这允许我在有人推荐或更改与用户相关的内容时发出通知。但问题是我无法发送所选通知的值,以便该值可以发送到我的控制器并将每个通知的标志更改为标记为&#34;读取&#34;并且不再显示在通知列表中。
这是我试图在我的控制器中获取选定值的一些代码
public function actionNotif(){
$value = $_POST['notificationValue'];
if(isset($value)){
$this->redirect(array('TrnProjectIT/admin'));
}
}
我总是收到undefined index : notificationValue
的错误消息。如何将动态通知值传递给控制器,以便根据用户选择的通知重定向到特定页面?我可能认为我的循环中存在问题并传递了我的价值,但我没有任何解决方案。有人能帮助我吗?
提前致谢
答案 0 :(得分:0)
您总是会收到undefined index : notificationValue
错误,因为您没有向服务器发送任何内容。
method
中有name
,value
和<div>
属性不允许的div。只需使用href
重定向,然后将您想要的参数传递给GET中的服务器。因此,请删除<div name="notificationValue">
和Javascript并改为使用:
<a href="<?= Yii::app()->baseurl ?>/index.php?r=TrnNotification/notif¬ificationValue=<?= $notif_value; ?>"><?= echo $date; ?></a>
和服务器端控制器:
public function actionNotif(){
if(isset($_GET['notificationValue'])){
$this->redirect('TrnProjectIT/admin');
}
}