You want to run two different queries dependent on if a topic is supplied or not
select * from mytable where catname='$category' and topicname='$topic'
or
select * from mytable where catname='$category'
the second one gives you all topics in a category. You can use an if else to do this...
if (isset($topicname)) {
do first sql statement
} else {
do second sql statement
}
There are other logic structures you can use as well. Hope that gets you started.
Tim