Correct it, dispalying dynamic menu from database table?

Hi all,
I am collecting the data from different tables and i prepare one dynamic menu…
but the problem is without having sub-categorys a category is displaying right arrow…(I think this is presentation problem)
See this page when mouseover on products for better understand…
http://rfcables.org/flexdropdown_new.php

<?php
function convertstring($src)
            {
			$explod1=explode("-",$src);//break string by -(hypen)
			$implod1=implode("_",$explod1);//replace -(hypen) with _(underscore)
			$explod2=explode(" ",$implod1);//break string by " "(space)
			$implod2=implode("-",$explod2);//replace " "(space) with -(hypen)
			return($implod2);
           }
$result = mysql_query("select * from cat");
while($data=mysql_fetch_row($result))
{
	$htmg=convertstring($data[1]);
	print("<li><a href='http://www.rfcables.org/products-subcat/$htmg.html'>$data[1]</a>");
	$result1 = mysql_query("select * from cat1 where c_name='$data[1]'");
	print "<ul>";
	while($data1=mysql_fetch_row($result1))
	{
	$htmg1=convertstring($data1[2]);
	print("<li><a href='http://www.rfcables.org/products-subcat2/$htmg/$htmg1.html'>$data1[2]</a>");
	$result2 = mysql_query("select * from cat2 where s_name='$data1[2]'");
	while($data2=mysql_fetch_row($result2))
	{
	print "<ul>";
	$htmg2=convertstring($data2[3]);
	print("<li><a href='http://www.rfcables.org/products-subcat3/$htmg/$htmg1/$htmg2.html'>$data2[3]</a>");
	$result3 = mysql_query("select * from cat3 where s_name1='$data2[3]'");

	while($data3=mysql_fetch_row($result3))
	{
	print "<ul>";
	$htmg3=convertstring($data3[4]);
	print("<li><a href='http://www.rfcables.org/products-subcat4/$htmg/$htmg1/$htmg2/$htmg3.html'>$data3[4]</a></li>");
	}
	print "</ul>";
	print "</ul>";
	}
	}
	print "</li></ul>";
}
?>

Hi

you open more ul then you close. also if a item doesn’t have a sub menu you shouldn’t open a sub list:

try:
$result1 = mysql_query(“select * from cat1 where c_name=‘$data[1]’”);

if (mysql_num_rows($result1)!=0) print "&lt;ul&gt;";

while($data1=mysql_fetch_row($result1))......

and then
if (mysql_num_rows($result1)!=0) print “</ul>”;

Thanks kai555…
I solved this problem, with your help…