Gossamer Forum
Home : Products : Gossamer Links : PHP Front End :

showing 'father' category in related@ links

Quote Reply
showing 'father' category in related@ links
Is there any way to [easily] show the top level (father) category in the Yahoo-style related@ category titles? The reason I ask is because I have several related categories under different main categories that have the same names. For example:

Clubs
--- US - West

Triathlon
--- Clubs
--- --- US - West

Makes sense within the context of each category, but when you add related categories to the list (which I very much like the addition of in v2.x), you get something like:

Clubs
--- US - West
--- --- US - West@

Triathlon
--- Clubs
--- --- US - West
--- --- --- US - West@

Which is a bit confusing, needless to say. What I would like to do is change that US - West@ to something like Clubs > US - West@ to signify it's part of another top level category. Listing all intermediate category levels in the title would not be necessary and would only clutter the display.

So, the question is how to accomplish it... function generate_category_page() in Page.inc.php obviously plays a role, as does function _print_cat() in Utils.inc.php (the short name portion) and subcategory.html's <?if ($Related) {?>@<?}?>, but I don't see any way off hand to conditionally alter the short name for related categories.

Any ideas?

Thanks,
Dan

Last edited by:

Dan Kaplan: Jul 3, 2002, 11:38 AM
Quote Reply
Re: [Dan Kaplan] showing 'father' category in related@ links In reply to
... on the other hand ... I just noticed that the CatRelations table contains a RelationName column for naming related categories, which I don't believe was an option in v1.1x.

That seems to do a good job at manually setting a more descriptive title, although something dynamic would be nice in the unlikelihood that I were to change my top level category names at some point...

The Category Browser seems to be the only way to get to the related category settings in the admin panel, correct? Seems a bit cumbersome to do much work in that way... Also, it appears you cannot edit existing relations, only delete or add new ones, correct? The only way I can see to edit them is to do so directly through a database interface, making sure to match up correct category ID's.

Dan
Quote Reply
Re: [Dan Kaplan] showing 'father' category in related@ links In reply to
Hmm, one down side to the RelationName naming approach is that it doesn't adjust the alphabetical sort order of the categories accordingly...

Dan
Quote Reply
Re: [Dan Kaplan] showing 'father' category in related@ links In reply to
I think this question is general enough to go into the regular Links SQL discussion and you'll probably get more responses that way. It's been a while since I worked on the front end, and I haven't played with related categories that much, so I'm not too familiar with related categories now.

Here's an idea: For each category you know it's father id, and you know whether or not that category is a related category. So if it's a related category, perform a lookup on it's father id to get its name. A global or just putting it in the template could work.
<?if ($Related) { [perform lookup]; echo "$father_name $Short_Name" ... } else {?>...

Adrian
Quote Reply
Re: [brewt] showing 'father' category in related@ links In reply to
Thanks for the suggestion. I was thinking adding another <?if ($Related) {?> condition ahead of the $Short_Name piece in subcategory.html might do the trick, but I was hoping to avoid extra database queries if at all possible (trying to be server friendly, especially if I go fully dynamic).

It also wouldn't addresss the aesthetical issue of the father leverl category name not getting sorted into the list alphabetically.

Is there any guideline in place for which topics should go into which forums, specifically referring to the PHP interface? The reason I asked in this forum is because the affected files are the xxx.inc.php and PHP templates, but the same question would apply to the corresponding Perl files...

Dan
Quote Reply
Re: [Dan Kaplan] showing 'father' category in related@ links In reply to
I came up with a solution to part 1 of 2 (I was inspired by qango in the Discussion forum looking into similar issues) -- getting the related titles to indicate what top level category they are in. In page.inc.php's generate_category_page(), in the Yahoo subcat section (I didn't bother with the non-integrated section) I changed:

Code:
$sth = $DB->query("SELECT RelatedID, RelationName FROM
${PREFIX}CatRelations WHERE CategoryID = '" . $category['ID'] . "'");

to:

Code:
$sth = $DB->query("SELECT r.RelatedID, r.RelationName, c.Full_Name
FROM ${PREFIX}CatRelations r, ${PREFIX}Category c WHERE r.CategoryID =
'" . $category['ID'] . "' AND r.RelatedID = c.ID");

and:

Code:
$rel[$relate[0]] = isset($relate[1]) ? $relate[1] : '';

to:

Code:
$split = explode("/", $relate[2]);
if (count($split) < 3) {
$rel[$relate[0]] = $relate[2]; // 2 level subdir, no need to abbreviate
} else {
// 3+ level subdir, show just first and last cat's
$rel[$relate[0]] = $split[0] ."/.../". $split[count($split)-1];
}

Here's an example page:

http://run-down.com/.../index.php?cat_id=18

The remaining problem is how to sort the related categories alphabetically by the Full_Name field (that example page looks correct, but it's coincidental to both the top level and related category names fitting in alphabetically). That looks to be a bit challenging, based on:

Code:
$order_by = 'ORDER BY ' . $CFG['build_category_sort'];

I'm thinking some post-query sorting will be necessary, as $CFG['build_category_sort'] looks to be used for both the regular subcategories (Name) and related subcategories (Full_Name).

Dan
Quote Reply
Re: [Dan Kaplan] showing 'father' category in related@ links In reply to
Ok, I think part 2 is now functional enough. Here's a good example page:

http://run-down.com/.../index.php?cat_id=11

It seems a bit slower as a result of the extra sorting, but not too bad.

In Page.inc.php's generate_category_page(), I changed:

Code:
while ($cat = $sth->fetchrow_hash()) {
if (isset($rel[$cat['ID']])) {
$cat['Related'] = 1;
$cat['RelationName'] = $rel[$cat['ID']];
}
$display['category_loop'][] = $cat;
}

to:

Code:
while ($cat = $sth->fetchrow_hash()) {
if (isset($rel[$cat['ID']])) {
$cat['Related'] = 1;
$cat['RelationName'] = $rel[$cat['ID']];
$this_sort = $cat['Full_Name'];
} else {
$this_sort = $cat['Name'];
}
$cat['sort_by'] = $this_sort;

$display['category_loop'][] = $cat;
}

and in Util.inc.php's _print_cat(), I added:

Code:
if (sizeof($sub_cat) > 1) {
$sort_array = array ();
foreach ($sub_cat as $this_cat) {
$sort_array[] = $this_cat['sort_by'];
}
array_multisort ($sort_array, $sub_cat, SORT_ASC, SORT_STRING);
}

after:

Code:
$parent_cat = array_shift($sub_cat);

Basically, what I did was to use the normal Name field for regular categories and Full_Name field for related categories, but combined into one sort for an overall alphabetical list.

The only way I could figure to then get the overall array of categories into order was to build a second multi-dimensional array ($sort_array) that contained just the combined sort titles with the same key associations, then run it through array_multisort() along with $sub_cat.

If anyone can think of a cleaner or more efficient way to do it, I'm all ears!

Dan