homepage Welcome to WebmasterWorld Guest from 23.22.173.58
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Looping Problem
woldie

10+ Year Member



 
Msg#: 10937 posted 8:05 pm on Nov 27, 2005 (gmt 0)

Hi,

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.

 

tomda

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 10937 posted 11:17 am on Nov 28, 2005 (gmt 0)

I am ready to help you Woldie, nonetheless the best would be to trim a bit your code for better understanding.
Otherwise, I can already say that you've got too many queries and arrays, SIMPLIFY your code!

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

woldie

10+ Year Member



 
Msg#: 10937 posted 1:14 pm on Nov 28, 2005 (gmt 0)

Hi Tomda,

I acknowledge your response, I'm working on it now. I'll post the code, this time I'll trim it down for sure!

Thanks.

W

woldie

10+ Year Member



 
Msg#: 10937 posted 1:24 pm on Nov 28, 2005 (gmt 0)

Okay, I've trimmed down the code, but I get the following:

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.

woldie

10+ Year Member



 
Msg#: 10937 posted 4:15 pm on Nov 28, 2005 (gmt 0)

Okay, got it to work....

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>";
}

tomda

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 10937 posted 5:38 am on Nov 29, 2005 (gmt 0)

Nice! You made it!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved