开发者

printing breadcrumb trail from two SQL tables

Problem solved.. is probably an easier way of doing this, but the FOR loop below works fine.

hey, I'm trying to print the breadcrumb trail for a classified ad. I have two tables (classifieds & categories). Classifieds contains all the detailed records, and categories contains the category structure for the site.

They look like this:

classifieds: | id | name | cat | descrip | contact | email | price | picture | password |   
categories: | id | name | sub | parent |
开发者_JAVA百科

example of a categories record:

| 1 | Cars | 0 | 0 |  
| 2 | Muscle Cars | 1 | 1 |  
| 3 | Ford | 2 | 1 |  

My logic is take the cat # from the CLASSIFIEDS table and locate that record in the CATEGORIES table. Then count how many other rows in the CATEGORIES table have the same parent #. Then create a loop to pull the subcategories for as many times as there were parents and store their names in an array. Once done, loop through and print the array.

$row['cat'] is equal to the 'cat' column of classifieds  

// if there is at least 1 record in classifieds, process it  
if ($rows > 0) { 

$cat = $row['cat'];  
$querya = "SELECT * FROM categories WHERE id = '$cat' limit 1";  
$resulta = mysql_query($querya);  
    while ($rowa = mysql_fetch_array($resulta, MYSQL_ASSOC)) {  
        $catname = $rowa['name'];  
        $catsub = $rowa['sub'];  
        $catparent = $rowa['parent'];  
    }

// count how many other rows have the same parent  
$queryparent = "SELECT * FROM categories WHERE parent = '$catparent'";  
$pullparent = mysql_query($queryparent);  
$parents = mysql_num_rows($pullparent);  

OLD FOR LOOP

// pull subparent records

if ($catsub > 1) {

    for ($i = 0; $i < $parents; $i++){

        $queryc = "SELECT * FROM categories WHERE id = '$catsub'";

        $subcat2 = mysql_query($queryc);

             while {$row3 = mysql_fetch_array($subcat2, MYSQL_ASSOC)) {

                $breadc[] = $row3['name'];

                $breadsub = $row3['sub'];

                $catsub = $breadsub;

             }

}  
else { } 

CORRECT FOR LOOP

if ($catsub > 0) {

    for ($i=0; $i < $parents; $i++) {

        $queryc = mysql_query("SELECT * FROM categories WHERE id = '$catsub' ORDER BY id ASC");

        $row3[$i] = mysql_fetch_array($queryc, MYSQL_ASSOC);

        $catsub = $row3[$i]['sub'];

    }

}

else { }

foreach ($breadc as $crumb) {  
    echo $crumb . " > ";  
}

The foreach echo above prints the trail--backwards but--properly as Ford > Muscle Cars > Cars, but it only does it for the first record in the CLASSIFIEDS table. So if there's 15 records in that table, it prints the same bread crumb structure for each one.

The for loop under the '// pull subparent records' comment iterates for as many times as there are records, but it doesn't reload itself with the new data from the CLASSIFIEDS table. I've been playing with this for a while and haven't figured it out. I can restructure the CATEGORIES table if someone has better idea for that.

Thanks for any advice.


I think your trouble is here

        $catsub = $breadsub; 

You never change $breadsub in the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜