如果数据库获得新条目,则显示一条消息


Show a message, if databasetable gets new entry

我正在为我和我的朋友编写一个小型社交网络。我需要一个功能,谁显示了一条消息,如果有一个新的条目在数据库从新闻源的帖子,喜欢在推特在其推特盒。表的名称是'community_posts',下面是发帖系统的代码:

<?php $getComments = mysql_query("SELECT * FROM community_posts ORDER BY id DESC LIMIT ".$query_min.", 20") or die(mysql_error()); ?>
<?php if(mysql_num_rows($getComments) == 0){ 
echo 'Empty!'; } 
else { while($Comments = mysql_fetch_assoc($getComments)){
$getUserInfo = mysql_query("SELECT * FROM users WHERE id = '".$Comments['userid']."'");
$userInfo = mysql_fetch_array($getUserInfo); ?>

                <div id=maggibox class="rounded-min shadow">
                    <div style="background:<?php if($userInfo['titel_picture'] == 0) { ?>#fff;border-bottom:1px solid #DFDFE1;<?php } else { ?>url(/dir/userdatas/titelbilder/gross/<?php echo $userInfo['titel_picture']; ?>);<?php } ?>width:100%;height:50px;background-size:cover;background-position:center;">
                        <div class=shadow style="border:3px solid #fff;background:url(<?php if($userInfo['profile_picture'] == 0) { ?>/assets/data/images/icons/noimage.jpg<?php } else { ?>/dir/userdatas/userbilder/small/<?php echo $userInfo['profile_picture']; ?><?php } ?>);background-size:cover;background-position:<?php if($userInfo['profile_picture'] == 0) { ?>0 100%<?php } else { ?>center top<?php } ?>;width:80px;height:80px;position:absolute;margin:-25px 0 0 20px;"></div>
                        <div style="margin:15px 0 0 120px;position:absolute;"><?php if($userInfo['visibility'] == "NOBODY"){ ?><div class="lt hint--right ptr" data-hint="<?php if($userInfo['id'] == $my_id){ echo 'Du hast dein Profil ausgeschaltet'; } else { echo 'Dieses Profil wurde ausgeschaltet'; } ?>" style="background:url(/assets/data/images/icons/disabledprofile.png);height:13px;width:13px;background-size:cover;background-position:center;margin-top:5px;margin-right:7px;"></div><?php } ?><span style="color:white;font-size:18px;font-weight:bold;text-shadow:0 0 5px black;"><?php echo $userInfo['vorname']; ?> <?php echo $userInfo['nachname']; ?></span></div>
                    </div>
                    <div id=n-box class="rounded-min" style="z-index:100;margin-top:15px;"> 
                        <div class="pv20 ph30" style="">
                            <span style="color:#333;font-size:15px;font-weight:normal;"><?php echo $Comments['comment']; ?></span>
                        </div>

                        <div class="p10" style="border-top:1px solid #DFDFE1;">
                            <input type=button class="btn_id6 rounded-min ptr" value="Optionen" /> &nbsp;&nbsp; 
                            <input type=button name="like" class="btn_id6 rounded-min ptr" value="Gefällt mir" />
                            <?php if($userInfo['visibility'] == "EVERYONE" or $userInfo['id'] == $my_id){ ?><a style="text-decoration:none;" href="/community/userprofile/<?php echo $userInfo['username']; ?>"><input type=button onclick="showme('loader');" class="btn_id6 rounded-min ptr rt"value="Profil besuchen" /></a><?php } else { ?><input type=button class="btn_id7 rounded-min rt"value="Profil ausgeschaltet" /><?php } ?>
                                    <div class=cl></div>
                        </div>
                    </div>
                </div>
<?php }} ?>

这只是显示数据库条目的代码,在css文件中使用CSS3样式有人能帮我一下吗?我不是最好的PHP, jQuery或Javascript!谢谢!

如果你愿意学习一个框架,ReactiveExtensions就是为你想做的事情而构建的。

如果没有,一个简单的方法是使用轮询:

每5秒检查一次的JavaScript:

setInterval(function() {
   var currentCount = $("#msgs .msg").length,
       newCount = $.get("api.php?getCount=" + threadId);
   if (currentCount != newCount) {
        $("#msgs").append($.get("api.php?getNewMsgs=" + threadId));
   }
}, 5000);

这是一个非常基本的例子,让你开始,不是一个完整的工作解决方案。

请注意,这个解决方案意味着你每5秒做一个db查询-但这可能不是一个真正的问题,因为你将有有限的用户数量。