duan0514324 2011-08-18 08:15
浏览 62
已采纳

有关使用mt_rand()刷新传入消息的建议吗?

is this a good way of doing it? or should i use a date time stamp with seconds rather?

while i am on a page, the new message must come through without me refreshing the page

my code is written in php:

<div id='div_longgray_gradient2'>
<div id='div_float_img'>
<table width='96%' border='0'>
<tr>
    <td rowspan='2' width='40px' align='left'>

        <img src='/images/rainbow/arrow.png'>
    </td>

    <td align='left'>
        <span class='spn_big_black_rbc'>MESSAGES</span>
    </td>
</tr>

<tr>
    <td align='left'>
        <span class='spn_med_lightblue_rbc'>Inbox</span>    
    </td>
    <td align='left'>
    </td>
</tr>
</table>
<br/>
<br/>

<table width='80%' align='center'>
<tr>
    <td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>Received</span></td>
    <td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>Message</span></td>
    <td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>From</span></td>
</tr>
<?php 
$cursor = $pager->getFirstIndice(); 
foreach ($pager->getResults() as $msg)
{ 
$has_freechat = false;  
//changed id to withid here
$freechat_req_link="profiles/confirmfreechat?withid=".$msg->getRcProfileTableRelatedByProfileIdFrom()->getId();
$freechat_req_link=link_to('Freechat',$freechat_req_link,'class=link_small_dark');

$cc = sizeof ($fc_records);
for($i = 0; $i < $cc; $i++) 
{
    if($fc_records[$i]->getProfileIdWith() == $msg->getProfileIdFrom())
    {       
        $has_freechat = true;       
        break;
    }
}   

$unique_code_from = $msg->getRcProfileTableRelatedByProfileIdFrom()->getUniqueCode();
$block_url = link_to('Block User',"blocklist/block?unqiue_code=$unique_code_from",'class=link_medium_blue');
echo "<tr>";
$date = add_date($msg->getCreatedAt(),$hr=2);
echo "<td class='td_show_contact_item' align='left'>".$date."</td>";
$opened_once = $msg->getOpenedOnce();
if($opened_once >= 1)
{
   echo "<td class='td_show_contact_item' align='left'>".link_to($msg->getSubject(),   'messagebox/read?cursor='.$cursor,'class=link_medium_blue')."</td>";
}    
else
{ ?>
   <td align='left'>
     <a href="<?php echo url_for('messagebox/read?cursor=').$cursor ?>" style='color:#ff0000 !important' class='spn_small_red_rbc'><?php echo $msg->getSubject();?></a>        
   </td>      
<?php 
} 
echo "<td class='td_show_contact_item' align='left'>".$unique_code_from." ( $block_url )</td>";
echo "</tr>";
++$cursor; 
}

function add_date($givendate,$hr=2)
{
$cd = strtotime($givendate);
$newdate = date('Y-m-d H:i:s', mktime(date('h',$cd)+$hr, date('i',$cd), date('s',$cd), date('m',$cd), date('d',$cd), date('Y',$cd)));
return $newdate;
}
?>
</table>
<br/>
Displaying results <?php echo $pager->getFirstIndice() ?> to  <?php echo $pager->getLastIndice() ?>.
<br/>

<?php if ($pager->haveToPaginate()): ?>

<?php echo link_to('&laquo;', 'messagebox/list?page='.$pager->getFirstPage()) ?>
<?php echo link_to('&lt;', 'messagebox/list?page='.$pager->getPreviousPage()) ?>
<?php $links = $pager->getLinks(); foreach ($links as $page): ?>
<?php echo ($page == $pager->getPage()) ? $page : link_to($page, 'messagebox/list?page='.$page) ?>
<?php if ($page != $pager->getCurrentMaxLink()): ?> - <?php endif ?>
<?php endforeach ?>
<?php echo link_to('&gt;', 'messagebox/list?page='.$pager->getNextPage()) ?>
<?php echo link_to('&raquo;', 'messagebox/list?page='.$pager->getLastPage()) ?>
<?php endif ?>

<table width='96%' border='0'>
<tr> 
    <td rowspan='2' width='40px' align='left'> 
        <img src='/images/rainbow/arrow.png'> 
    </td> 
    <td align='left'> 
        <span class='spn_big_black_rbc'></span> 
    </td> 
</tr> 
<tr>
    <td align='left'>
        <span class='spn_med_lightblue_rbc'>Sent Items</span>   
    </td>
</tr>
</table>
<br/>
<br/>
<table width='80%' align='center'>
<tr>
    <td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>Sent</span></td>
    <td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>Message</span></td>
    <td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>To</span></td>
</tr>
<?php
$cursor2 = $pager2->getFirstIndice(); 
$br = sizeof ($block_records);
foreach ($pager2->getResults() as $item)
{ 
    $link = link_to('Delete',"messagebox/deleteSentmsg?mid=".$item->getId(),'class=link_medium_blue');
    $id = $item->getRcProfileTableRelatedByProfileIdTo()->getId();    
    ?>
    <tr>
        <td class='td_show_contact_item' width='15%' align='left'><?php echo $date = add_date($item->getCreatedAt(),$hr=2); ?></td>
        <td class='td_show_contact_item' width='45%' align='left'><?php echo $item->getMessage()?></td>
        <td class='td_show_contact_item' width='25%' align='left'><?php echo $item->getRcProfileTableRelatedByProfileIdTo()->getUniqueCode()."  ".$link;?></td>
    </tr>
    <?php
     ++$cursor2;    
}
?>
</table>
<br/>
Displaying results <?php echo $pager2->getFirstIndice(); ?> to  <?php echo $pager2->getLastIndice(); ?>.
<br/>
<?php if ($pager2->haveToPaginate()): ?>
<?php echo link_to('&laquo;', 'messagebox/list?page2='.$pager2->getFirstPage(),'class=link_big_dark') ?>
<?php echo link_to('&lt;', 'messagebox/list?page2='.$pager2->getPreviousPage(),'class=link_big_dark') ?>
<?php $links = $pager2->getLinks(); foreach ($links as $page2): ?>
<?php echo ($page2 == $pager2->getPage()) ? $page2 : link_to($page2, 'messagebox/list?page2='.$page2,'class=link_big_dark') ?>
<?php if ($page2 != $pager2->getCurrentMaxLink()): ?> - <?php endif ?>
<?php endforeach ?>
<?php echo link_to('&gt;', 'messagebox/list?page2='.$pager2->getNextPage(),'class=link_big_dark') ?>
<?php echo link_to('&raquo;', 'messagebox/list?page2='.$pager2->getLastPage(),'class=link_big_dark') ?>
<?php endif ?>
</div>
</div>

can anyone give some advise please? i have no ajax/JS here so dont know how i would incorporate it

  • 写回答

1条回答 默认 最新

  • douwei1944 2011-08-18 08:44
    关注

    PHP is a server side language and will not update a page once it has been fetched by the browser. To do this you will need to use a combination of server side and client side scripts.

    One way of making your page update it's content is to use AJAX (http://api.jquery.com/jQuery.ajax/) combined with a timer (setInterval() and setTimeout()) to query a PHP script and check for updates every x seconds. The updates can then be added to the page when they are available using JavaScript / jQuery.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集