如何使用json_encode更新foreach中的数据?

时间:2011-09-27 10:22:48

标签: php javascript html json

我有一个foreach循环浏览登录用户的消息并显示它们listSuccess.php:

$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,subject: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);
    }   
});
}

它运行正常但它用新的替换foreach列表中的第一条消息,我希望我的代码添加到列表的末尾,从而在列表的末尾显示新条目。我怎么能实现这个目标? 谢谢

1 个答案:

答案 0 :(得分: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);

   });
}