how to use json_encode to update data in a foreach?
i have a foreach looping through messages for the logged in user and displayng them 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;
}
i have a tag for date: td_date,subject: td_subject and who the message is from:td_from then i have my 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));
}
then the 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);
}
});
}
it runs fine BUT it replaces the first message in the foreach list with the new one, i want my code to add to the end of the list thus displaying the new entry at the end of the list. how can i achieve this? thank you
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);
});
}
精彩评论