Forum Moderators: coopster
I'm currently working on a 'new sites' script, and I'm having some trouble in trying to get the category to work for each site submitted.
What I have done is created a recursive function so that it drill downs until the parentid reaches 0. The trouble I am having is that it is repeating itself, and I don't know how to fix it.
To give you an example this is what is displays at the moment:
Home > Computers > Programming > Delphi and BCB > VCL Components >
Home > Computers > Programming > Delphi and BCB > VCL Components >
Home > Arts > Animation > Web Rings >
Home > Computers > Programming > Delphi and BCB > VCL Components >
Home > Arts > Animation > Web Rings >
Home > Arts > Animation > Web Rings >
Where it should really display this according to the SQL query:
Home > Computers > Programming > Delphi and BCB > VCL Components >
Home > Arts > Animation > Web Rings >
Home > Arts > Animation > Web Rings >
I'm not quite sure if its the foreach statement which is causing this?
The Code:
function nav ($id,$x='')
{
$result=mysql_query("select cat_name,parentid
from db_categories
where catid=$id");
list($DBcat_name,$DBparentid1)=mysql_fetch_row($result);
mysql_free_result($result);
$cat_name=str_replace(" ","-",$DBcat_name);
$x="$cat_name-$id/".$x;
if($id>0)
$x=nav($DBparentid1,$x);
return $x;
}
$result=mysql_query("select db_links.catid
from db_links,db_categories
where to_days(now()) - to_days(link_date) between 0 and 30
and db_links.catid=db_categories.catid
and approved=1
order by link_date desc limit 3");
while (list($DBcatid)=mysql_fetch_row($result))
{
$catids[]=$DBcatid;
}
foreach ($catids as $var => $value)
{
$disp_quest[]=$value;
$numrows=count($disp_quest);
}
$cnt=0;
for ($x=0;$x<$numrows;$x++)
{
$sql_result=mysql_query("select catid,cat_name,parentid
from db_categories
where catid=".$disp_quest[$cnt]."");
while (list($DBcatid,$DBcat_name,$DBparentid)=mysql_fetch_row($sql_result))
$url[]=nav($disp_quest[$cnt]);
foreach ($url as $u)
{
$u = substr($u,37);
$t=$u;
$t=str_replace("-"," ",$t);
$t=str_replace("/"," > ",$t);
$t=ereg_replace("[0-9]"," ",$t);
$location=$base_url.'/'.$u;
echo "<a href=\"$base_url/$u\" class=\"list4\">Home > $t</a><br>";
}
$cnt++;
}
Thanks again.
Woldie.
I'm not quite sure if its the foreach statement which is causing this?Use print_r ($catids) to see if you do not have duplicates in your this array.
Procedure should be:
1/ Get $catids;
2/ For each value, call the function
3/ Function - Check if id has parent
-> if yes, echo category and call again the function
-> if no, echo category and stop
Home >
Home >
Home >
So I'm nearly there! But obviously its not displaying the categories as it should.
Code:
function nav ($id,$x='')
{
$result=mysql_query("select cat_name,parentid from db_categories
where catid=$id");
list($DBcat_name,$DBparentid1)=mysql_fetch_row($result);
mysql_free_result($result);
$cat_name=str_replace(" ","-",$DBcat_name);
$x="$cat_name-$id/".$x;
if($id>0)
$x=nav($DBparentid1,$x);
return $x;
}
$result=mysql_query("select db_links.catid
from db_links,db_categories
where to_days(now()) - to_days(link_date) between 0 and 30
and db_links.catid=db_categories.catid
and approved=1
order by link_date desc limit 3");
while (list($DBcatid)=mysql_fetch_row($result))
{
$catids[]=$DBcatid;
}
foreach ($catids as $var => $value)
{
$url[]=nav($value);
$u = substr($u,37);
$t=$u;
$t=str_replace("-"," ",$t);
$t=str_replace("/"," > ",$t);
$t=ereg_replace("[0-9]"," ",$t);
$location=$base_url.'/'.$u;
echo "<a href=\"$base_url/$u\" class=\"list4\">Home > $t</a><br>";
}
Thanks
W.
Code:
$result=mysql_query("select db_links.catid
from db_links,db_categories
where to_days(now()) - to_days(link_date) between 0 and 30
and db_links.catid=db_categories.catid
and approved=1
order by link_date desc limit 10");
while (list($DBcatid)=mysql_fetch_row($result))
{
$url[]=nav($DBcatid);
$catids[]=$DBcatid;
}
foreach ($url as $u)
{
$u = substr($u,37);
$t=$u;
$t=str_replace("-"," ",$t);
$t=str_replace("/"," > ",$t);
$t=ereg_replace("[0-9]"," ",$t);
echo "<a href=\"$base_url/$u\" class=\"list4\">Home > $t</a><br>";
}