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.

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题