okay... if I am hearing you correctly, you need to dynamically make
<A HREF='#section1'>Section 1</A>
and
<A NAME='section1'></A>
Tags....
and I'm assuming that the issue is that you echo the "contence" at the top and then loop through the query and place the output.
IF this is the case, what you might want to consider is not echoing untill the very end.. let me try to give you an example...
<?
// the following info junk is an attempt to emulate MySQL output
$info[0] = array('section_id' => '1', 'title' => 'Section 1' , 'content' => 'this is section 1');
$info[1] = array('section_id' => '2', 'title' => 'Section 2' , 'content' => 'this is section 2');
$info[2] = array('section_id' => '3', 'title' => 'Section 3' , 'content' => 'this is section 3');
$info[3] = array('section_id' => '4', 'title' => 'Section 4' , 'content' => 'this is section 4');
$info[4] = array('section_id' => '5', 'title' => 'Section 5' , 'content' => 'this is section 5');
$info[5] = array('section_id' => '6', 'title' => 'Section 6' , 'content' => 'this is section 6');
$info[6] = array('section_id' => '7', 'title' => 'Section 7' , 'content' => 'this is section 7');
$info[7] = array('section_id' => '8', 'title' => 'Section 8' , 'content' => 'this is section 8');
$info[8] = array('section_id' => '9', 'title' => 'Section 9' , 'content' => 'this is section 9');
$info[9] = array('section_id' => '10', 'title' => 'Section 10' , 'content' => 'this is section 10');
$info[10] = array('section_id' => '111', 'title' => 'Section 11' , 'content' => 'this is section 11');
// End attempt to emulate MySQL output
$toc = "<A NAME=0></A>";
$content = "";
foreach($info as $row)
{
$toc .= "<A HREF='#" . $row['section_id'] . "'>" . $row['title'] . "</A><BR>";
$content .= "<A NAME='" . $row['section_id'] . "'></A><B>" . $row['title'] . " <FONT SIZE=-2><A HREF='#0'>Back To Top</A></FONT><BR>" . $row['content'] . "<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>";
}
echo $toc,$content;
?>