> but you ONLY want to add +1 to $total pages IF there is a fractional part in ($total/20)

You are right, it should be
$totalpages = ceil($total/20);


>" what you actually get in the list of topics is 42 topics


This is fixed by
search for:
$start = (($p -1) * 20) - 1;

replace with:
$start = (($p -1) * 20);