2011-09-27 20 views
0

我有一個foreach通過消息循環爲登錄用戶和displayng他們listSuccess.php:如何使用json_encode更新foreach中的數據?

$cursor = $pager->getFirstIndice(); 
foreach ($pager->getResults() as $msg) 
{ 
$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 id='td_id' value='.$cursor.'>"; 
    $date = add_date($msg->getCreatedAt(),$hr=2); 
echo "<td class='td_show_contact_item' align='left' id='td_date'>".$date."</td>"; 
    $opened_once = $msg->getOpenedOnce(); 
    <td align='left' id='td_subject'> 
     <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' id='td_from'>".$unique_code_from." </td>"; 
     echo "<td>(".$block_url.")</td>"; 
     echo "</tr>"; 
     ++$cursor; 
} 

我有日期標籤:td_date,主題:td_subject和誰消息來自:td_from 然後我有我的actions.class.php

public function executeNewMessageDetails(sfWebRequest $request) 
{ 

    $profile_id = $this->getUser()->getAttribute('profile_id','zero'); 
    $new_msgs = RcMessageBoxTablePeer::getNewMessages($profile_id); 
    foreach ($new_msgs as $row) 
    { 
     $date = $row->getCreatedAt(); 
     $subject = $row->getSubject(); 
     $from = $row->getProfileIdFrom(); 
     $id = $row->getId(); 
     $uc_record = RcProfileTablePeer::getById($from); 
     $uc_from = $uc_record->getUniqueCode(); 
     //$block_url = 'Block User',"blocklist/block?unqiue_code=$uc_from",'class=link_medium_blue'); 
    } 
    $output = array("td_date" => $date, "td_subject" => $subject, "td_from" => $uc_from, "td_id" => $id); 
    return $this->renderText(json_encode($output)); 
} 

那麼JS:

function ax_get_new_msg_details() 
{ 
    var mTimer; 
    mTimer = setTimeout('ax_get_new_msg_details();',30000); 
    $.getJSON('/apps_dev.php/messagebox/newMessageDetails', function(data) 
{ 
    var td_id = $('#td_id'); 
    var str='<tr>'; 
    str += "<td class='td_show_contact_item' align='left' id='td_date'>"+data.td_date+'</td>'; 
    str += "<td align='left' id='td_subject'><a href='#' style='color:#ff0000 !important' class='spn_small_red_rbc'>"+data.td_subject+"</a></td>"; 
    str += "<td class='td_show_contact_item' align='left' id='td_from'>"+data.td_from +"</td>"; 
    //str += "<td id='block_url'>"+data.block_url+"</td>"; 
    str +='<tr>'; 
    var tbl = $('#td_date').parents('table'); 
    if (td_id == data.td_id) 
    { 

    } 
    else 
    { 
     $(tbl).append(str); 
    } 
}); 
} 

它運行良好,但它取代新的列表中的第一條消息,我希望我的代碼添加到列表的末尾,從而在列表的末尾顯示新條目。我怎麼能做到這一點? 謝謝

回答

0
function ax_get_new_msg_details() 
{ 
    var mTimer; 
    mTimer = setTimeout('ax_get_new_msg_details();',30000); 
    $.getJSON('/apps_dev.php/messagebox/newMessageDetails', function(data) 
{ 
    var str='<tr>'; 
    str += "<td class='td_show_contact_item' align='left' id='td_date'>"+data.td_date+'</td>'; 
    str += "<td align='left' id='td_subject'> 
     <a href='#' style='color:#ff0000 !important' class='spn_small_red_rbc'> 
    "+data.td_subject+"</a></td>"; 
    str += "<td class='td_show_contact_item' align='left' id='td_from'>"+data.td_from +" </td><td>block url</td>"; 
    str +='<tr>'; 

    var tbl = $('#td_date').parents('table'); 

    $(tbl).append(str); 

    }); 
} 
+0

謝謝你看起來它運作良好,除了它是重複整個事情從而重新顯示遍地的傳入消息?我只需要一次顯示新消息? 謝謝 –

+0

你可以在你的上添加$ cursor,並且保持這個順序在ajax update conditionally。 – mahadeb

+0

抱歉,但不確定你的意思?我從來沒有做過這件事,對不起,請你詳細說明一下。謝謝...請看代碼添加到頂部如果IM在正確的道路上:) –

相關問題