$prev = ($i == 0) ? $total : $i-1; download : Dan Wilton