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

时间:2011-08-18 08:15:04

标签: php

这是一个很好的方法吗?或者我应该使用几秒钟的日期时间戳?

当我在一个页面上时,新消息必须在没有我刷新页面的情况下通过

我的代码是用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>

有人可以给一些建议吗? 我这里没有ajax / JS,所以不知道我会如何加入它

1 个答案:

答案 0 :(得分:0)

PHP是一种服务器端语言,一旦浏览器获取页面,就不会更新页面。为此,您需要使用服务器端和客户端脚本的组合。

使页面更新内容的一种方法是使用AJAX(http://api.jquery.com/jQuery.ajax/)与计时器(setInterval()和setTimeout())结合来查询PHP脚本并每隔x秒检查一次更新。然后,可以使用JavaScript / jQuery将更新添加到页面中。