First, you need a while loop not a do..while as you do not read a row until the end of your first loop with the do..while.
Second, I'm assuming that the variable $totalRows_Topic is a count of the number of records (threads) for each topic.
You do not show the query which gives the result but if you only want changes in topic_id and a count, use a group by clause in the query.
$sql = "select topic_id, topic, created, count(topic_id) as totalRows_Topic from tablename group by topic_id,topic,created"
hth