so let's just forget rewrite and do it manually like you wanted in the first place I think.
try this version, which includes the topic ID in the query as well as the URL redefined:
Text Formatted Code
function phpblock_mostPopularbyDate()
{
global $_TABLES, $_CONF;
$result = DB_query("SELECT sid,tid,title,hits FROM {$_TABLES['stories']}
WHERE (draft_flag = 0) AND (date <= NOW()) AND (date >= (DATE_SUB(NOW(), INTERVAL 30 DAY)))"
. COM_getPermSQL ('AND') . " ORDER BY hits desc LIMIT 5");
$nrows = DB_numRows($result);
if( $nrows > 0 ){
$string = '';
$popular = array();
for( $i = 0; $i < $nrows; $i++ ){
$A = DB_fetchArray( $result );
$string .= $poplist . '<br>';
$popular[] = '<a href="' . $_CONF['site_url'] . '/'
. $A['tid'] . '/article.php/' . $A['sid'] . '">' . $A['title']
. '</a> (' . $A['hits'] . ')';
}
if( !empty( $popular )){
$poplist = COM_makeList( $popular, 'list-popular-stories' );
}
}
return $poplist;
}