如果databasetable获得新条目,则显示消息

时间:2014-06-04 14:48:26

标签: javascript php jquery

我正在为我的朋友和我编写一个小型社交网络。我需要一个显示消息的函数,如果数据表中的新条目来自新闻源中的帖子,就像Twitter上的tweetbox中的帖子一样。表的名称是'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 }} ?>

这就是显示数据库中的entrys的代码,它们在CSS-File中用CSS3设置样式 有人可以帮我弄这个吗?我不是最好的PHP,jQuery或Javascript!谢谢!

1 个答案:

答案 0 :(得分:0)

如果您愿意学习框架,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秒进行一次数据库查询 - 但这可能不会成为真正的问题,因为您的用户数量有限。