Does anybody know if it is possible to optimise the following code? I have a feeling it can be done but I just don't know how to:
// start make category url
$sql_1 = "select * from ".$prefix."store_category where cat_id = $cat_id";
$result1 = mysql_query ($sql_1);
$row = @mysql_fetch_array($result1);
$catid_1 = $row["cat_id"];
$catfatherid_1 = $row["cat_father_id"];
$catname_1 = $row["category"];
$t1 = $catname_1;
$catid1 = $catid_1;
if ($catfatherid_1 <> 0)
{
$sql_lowercat = "select * from ".$prefix."store_category where cat_id = $catfatherid_1";
$result = mysql_query ($sql_lowercat);
$row = @mysql_fetch_array($result);
$catid_2 = $row["cat_id"];
$catfatherid_2 = $row["cat_father_id"];
$catname_2 = $row["category"];
$t2 = $catname_2;
$catid2 = $catid_2;
}
if ($catfatherid_2 <> 0)
{
$sql_lowercat = "select * from ".$prefix."store_category where cat_id = $catfatherid_2";
$result = mysql_query ($sql_lowercat);
$row = @mysql_fetch_array($result);
$catid_3 = $row["cat_id"];
$catfatherid_3 = $row["cat_father_id"];
$catname_3 = $row["category"];
$t3 = $catname_3;
$catid3 = $catid_3;
}
if ($catfatherid_3 <> 0)
{
$sql_lowercat = "select * from ".$prefix."store_category where cat_id = $catfatherid_3";
$result = mysql_query ($sql_lowercat);
$row = @mysql_fetch_array($result);
$catid_4 = $row["cat_id"];
$catfatherid_4 = $row["cat_father_id"];
$catname_4 = $row["category"];
$t4 = $catname_4;
$catid4 = $catid_4;
}
if ($catfatherid_4 <> 0)
{
$sql_lowercat = "select * from ".$prefix."store_category where cat_id = $catfatherid_4";
$result = mysql_query ($sql_lowercat);
$row = @mysql_fetch_array($result);
$catid_5 = $row["cat_id"];
$catfatherid_5 = $row["cat_father_id"];
$catname_5 = $row["category"];
$t5 = $catname_5;
$catid5 = $catid_5;
}
if ($catfatherid_5 <> 0)
{
$sql_lowercat = "select * from ".$prefix."store_category where cat_id = $catfatherid_5";
$result = mysql_query ($sql_lowercat);
$row = @mysql_fetch_array($result);
$catid_6 = $row["cat_id"];
$catfatherid_6 = $row["cat_father_id"];
$catname_6 = $row["category"];
$t6 = $catname_6;
$catid6 = $catid_6;
}
if ($catfatherid_6 <> 0)
{
$sql_lowercat = "select * from ".$prefix."store_category where cat_id = $catfatherid_6";
$result = mysql_query ($sql_lowercat);
$row = @mysql_fetch_array($result);
$catid_7 = $row["cat_id"];
$catfatherid_7 = $row["cat_father_id"];
$catname_7 = $row["category"];
$t7 = $catname_7;
$catid7 = $catid_7;
}
if ($catfatherid_7 <> 0)
{
$sql_lowercat = "select * from ".$prefix."store_category where cat_id = $catfatherid_7";
$result = mysql_query ($sql_lowercat);
$row = @mysql_fetch_array($result);
$catid_8 = $row["cat_id"];
$catfatherid_8 = $row["cat_father_id"];
$catname_8 = $row["category"];
$t8 = $catname_8;
$catid8 = $catid_8;
}
if ($catfatherid_8 <> 0)
{
$sql_lowercat = "select * from ".$prefix."store_category where cat_id = $catfatherid_8";
$result = mysql_query ($sql_lowercat);
$row = @mysql_fetch_array($result);
$catid_9 = $row["cat_id"];
$catfatherid_9 = $row["cat_father_id"];
$catname_9 = $row["category"];
$t9 = $catname_9;
$catid9 = $catid_9;
}
if ($t1!="")
$category=$t1;
if ($t2!="")
$category=$t2." > ".$t1;
if ($t3!="")
$category=$t3." > ".$t2." > ".$t1;
if ($t4!="")
$category=$t4." > ".$t3." > ".$t2." > ".$t1;
if ($t5!="")
$category=$t5." > ".$t4." > ".$t3." > ".$t2." > ".$t1;
if ($t6!="")
$category=$t6." > ".$t5." > ".$t4." > ".$t3." > ".$t2." > ".$t1;
if ($t7!="")
$category=$t7." > ".$t6." > ".$t5." > ".$t4." > ".$t3." > ".$t2." > ".$t1;
if ($t8!="")
$category=$t8." > ".$t7." > ".$t6." > ".$t5." > ".$t4." > ".$t3." > ".$t2." > ".$t1;
if ($t9!="")
$category=$t9." > ".$t8." > ".$t7." > ".$t6." > ".$t5." > ".$t4." > ".$t3." > ".$t2." > ".$t1;
$t1="";$t2="";$t3="";$t4="";$t5="";$t6="";$t7="";$t8="";$t9="";
// end make category url
This is basically to allow a category to display all subcategories easily.