Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

CategoryAlternates additional fields?

Quote Reply
CategoryAlternates additional fields?
Origianl Post, added to:
In Reply To:
Has anyone tried to add fields to the CategoryAlternates table and make those fields available to the link.html template?
pugdog thanks for your help on this one too, your thoughts were insightful and it was your questions that brought the solution to me...
Code:
# Generate the links listing.
for my $i (0 .. $#{$links_r}) {
last if (ref $links_r->[$i] ne 'ARRAY');
$tmp = $LINKDB->array_to_hash ($links_r->[$i]);
$OUT{links} .= &site_html_link ($tmp, $in);
}
for my $i (0 .. $#{$alt_r}) {
last if (ref $alt_r->[$i] ne 'ARRAY');
$tmp = $FAKEDB->array_to_hash ($alt_r->[$i]);
$OUT{links} .= &site_html_slink ($tmp, $in);
}
With this in page.cgi and nph-build.cgi as well as adding a fake .def file to read in the the extra fields from CategoryAlternates, I was able to get my Link extension fields into the templates. Now each link can basically have as many additional field values as I choose to put in the alternates table. I just hope it works in the long haul...

If anyone is interested in doing something like this send me a private message and I can give you more details. PS: pugdogs comment about this not being the best solution was indeed accurate if you only plan on having a couple few 'stores'. But for the purposes of my site this was my best option where the BGII game has prolly close to 45-50 stores that will need populating and having an extra 45-50 fields in the links table not to mention 45-50 <%if%> statements in my templates for just one field wasn't the way to go, then add in a 'store' for each major encounter and that number is closer to 200/300. Sorry pugdog I hadn't really made it clear as to why I was going about it the way I was.
Quote Reply
Re: CategoryAlternates additional fields? In reply to
I'm hoping someone might be able to offer some suggestions on this. Basically what I'm doing is creating one main area where an "Item" instead of a "Link" has it's main properties...then by assigning it to an Alternate Category I can assign it additional values, such as Qty, Size, Color, etc...

Actually getting the additional fields data into the database and the HTML changes to have the input fields available in the Alternate Category link is pretty easy...and the join involved to place the new data into $alt_r of nph-build.cgi/page.cgi is also quite easy, but because the $alt_r is then merged with $links_r (which is the same as the value of <%links%> in the category.html template) to show both links and alternates together, I'm trying to join two arrays that do not have the same number of fields, I tried adding the same fields to database.Links hoping the values of $alt_r from database.CategoryAlternates would overwrite the empty values in Links; to no effect.
In Reply To:
Code:
# This is the join, I'm working in page.cgi but
# it should be the same as in nph-build.cgi
# it's just an easier/faster way to test modifications.
# PS: page.cgi is the best way to try out changes that
# usually involve a rebuild of the site, without rebuilding!


# Get any alternates.
$get_alt = $LINKDB->prepare (" SELECT l.*, c.Qty, c.Stacked
FROM Links as l, CategoryAlternates as c
WHERE l.ID = c.LinkID AND c.CategoryID = ?
ORDER BY $LINKS{build_sort_order_category}
LIMIT 1000 ");
$get_alt->execute ($category_r->{'ID'});
$alt_r = $get_alt->fetchall_arrayref || [];

# Sort them together.
$links_r = &build_sort_links ($links_r, $alt_r) if (@{$alt_r});

# Which leades to the sub in the next quote:
And here is my problem code that I can't get working together:
In Reply To:
Code:
sub build_sort_links {
# --------------------------------------------------------
# Merges two array refs into one link list.
#
my ($arr_a, $arr_b) = @_;
my @names = split /,/, $LINKS{build_sort_order};
my @fields = map { $LINKDB->position($_) - 1 } @names;
my @c = sort { lc join ("", @{$a}[@fields]) cmp lc join ("", @{$b}[@fields]) } @{$arr_b}, @{$arr_a};
return \@c;
}
The text in red is my join additions to get my new fields into $links_r but I think that somehow they are getting tossed during the merging of $links_r & $alt_r.

Is it possible to merge 2 arrays of different length? Even a bypass that isn't so pretty is fine with me, if it can be done...I was hoping I could fake $links_r into thinking it has 2 additional fields that are empty, in order to make this happen. It's really the only way I can think of that would make this work.

Quote Reply
Re: CategoryAlternates additional fields? In reply to
That is my least favorite sub routine in links.

BTW... if you try to do it that way, it won't work in the next version, since there is _NO_ distinction of an 'alt' or 'regular' link.

The links are in categories in a CatLinks table:

CategoryID|LinkID


You can get all links in a category, or all categories a link is in by aksing that table.

You are then returned a set of LinkID's that are used to look up the link.

You might be better off adding three fields to your Links record:

isItem
Qty
Stacked

Then, if isItem is yes, the template will check the QTY and Stacked fields.
If isItem is no, then the template will ignore those fields, even if there is a value in them.

It would eliminate the need for joins, unless you are keeping significant amounts of information in the extra tables, and only want to have those fields used for links that are isItems.




PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://postcards.com/FAQ


Quote Reply
Re: CategoryAlternates additional fields? In reply to
I thought of doing it that way, but the thing is that I'm using these extra fields to show the same items in multiple categories but the values of Qty etc... are not the same in each category. The benefit I have on my side is that there really is no need to merge $links_r & $alt_r because neither plain links or the altered links will ever be in the same category, but I have to somehow bring the values of database.Links and database.CategoryAlternates together which means using this routine.

As an example I've got my main items, under the category:
- Item List

Then the altered links would be placed in, for example:
- Athkatla/Store 1
- Athkatla/Store 2

The reason for this is that all the items will be the same in each store, only the quantities each store holds is different...you see what I mean?



Quote Reply
Re: CategoryAlternates additional fields? In reply to
Well, then you might be better off adding those fields to the links records, such as Qty_store1, Qty_store2, etc.

Then, in the template check your category: <%if category_id= nn%><%qty_store1%><%endif%>

Or, in the nph-build routines, assigning the value to <%QTY%> based on what category you are in. I don't think table joins are the best way for this sort of situation.

PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://postcards.com/FAQ


Quote Reply
Re: CategoryAlternates additional fields? In reply to
Well, I think I just succeeded...it may be ugly, and it definatley works off of improvisation and foolin' the subs but It produces the desired effect:

In Reply To:
Code:
# Generate the links listing.
if ($alt_r) {
for my $i (0 .. $#{$alt_r}) {
last if (ref $alt_r->[$i] ne 'ARRAY');
# Here I created a bogus .def file with the appropriate
# fields and run it through for the join. ie using:
# $FAKEDB = new Links::DBSQL $LINKS{admin_root_path} . "/defs/bogus.def";
$tmp = $FAKEDB->array_to_hash ($alt_r->[$i]);
$OUT{links} .= &site_html_slink ($tmp, $in);
}
} else {
# If it's a regular link then do what it usually does...
for my $i (0 .. $#{$links_r}) {
last if (ref $links_r->[$i] ne 'ARRAY');
$tmp = $LINKDB->array_to_hash ($links_r->[$i]);
$OUT{links} .= &site_html_link ($tmp, $in);
}
}
If you wanna see why I'm doing it this way, take a look at
http://www.mvsource.com/cgi-bin/bg2soa/page.cgi
It's for a Baldur's Gate II game site that makes for a searchable walkthrough/item database.

Quote Reply
Re: CategoryAlternates additional fields? In reply to
I get so excited about getting one side of it to work, that I didn't realize I broke the other half...so I got the join to work!

But now I've got to make it so the non-join works too...a bit more to do for now!

Quote Reply
Re: CategoryAlternates additional fields? In reply to
Take a look at the first posting it's content has changed from question to answer. If you are interested in doing something like this feel free to contact me I can give you the basics.