Gossamer Forum
Home : Products : Links 2.0 : Customization :

Random links with Priority always first

Quote Reply
Random links with Priority always first
I have spent two days reading the forums because I know that the answers are here, it's just putting them all together for what I want that is the problem.

I would like to have the links rotate randomly with each build but yet keep links that have been labeled "Priority" at the top of the list.

Here is what I've done so far:

db_utils.pl - sub build_sorthit

my (@unsorted) = @_;
my ($num) = ($#unsorted+1) / ($#db_cols+1);
my (%sortby, %priority, %isnew, %iscool, $hit, $i, @sorted);

for ($i = 0; $i < $num; $i++) {
$sortby{$i} = $unsorted[$db_sort_links + ($i * ($#db_cols+1))];
($unsorted[$db_isnew + ($i * ($#db_cols+1))] eq "Yes") and ($isnew{$i} = 1);
($unsorted[$db_priority + ($i * ($#db_cols+1))] eq "Yes") and ($priority{$i} = 1);
}
foreach $hit (sort {
($priority{$b} and !$priority{$a}) and return 1;
($priority{$a} and !$priority{$b}) and return -1;
($isnew{$b} and !$isnew{$a}) and return 1;
($isnew{$a} and !$isnew{$b}) and return -1;
($priority{$a} and $priority{$b}) and return lc($sortby{$a}) cmp lc($sortby{$b});
($isnew{$a} and $isnew{$b}) and return lc($sortby{$a}) cmp lc($sortby{$b});

return (rand > 0.5) ? 1 : -1;
} (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
return @sorted;
}

And I also changed this

my (%sortby, %priority, %isnew, %iscool, $hit, $i, @sorted);

Then I added the following in links.def

Priority => [14, 'alpha', 0, 5, 0, 'No', '']

$db_priority = 14;

The first problem I have is that when I go to the admin and try to add a link there's a field for Priority, but it's only a box with with "No" in it. What am I missing?

I haven't added any links yet because I'm concerned that I don't have this right because the Priority Field doesn't have the drop down menu with the options like the "isNew" or "isPopular" Fields.

I've worked with Links2 for awhile and have done a few modifications, but this one really has me stumped.

One other question - will the "Priority" links have that next to them like "New" and "Pop" or will it just be a field that can be for admin?

Thank you to anyone who can offer some help. I really have tried to go through the past posts and cobble this together.
Quote Reply
Re: [SandraJ] Random links with Priority always first In reply to
see also http://www.gossamer-threads.com/...;;page=unread#unread


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [SandraJ] Random links with Priority always first In reply to
use this as your model for Priority

Code:
ReceiveMail => [13, 'alpha', 10, 10, 1, 'Yes', 'No|Yes'],


further down see

Code:
# System defaults. When adding new links or modifying links, these fields
# can not be overwritten by a user.
%add_system_fields = (
isNew => 'No',
isPopular => 'No',
ReceiveMail => 'Yes',


Still further down see

Code:
# Hash of column names to possible options. If you want to use a select form
# field, you can use &build_select_field in your HTML page. This routine will
# make a <SELECT> input tag using the following values:
%db_select_fields = (
isNew => 'Yes,No',
isPopular => 'Yes,No',
ReceiveMail => 'Yes,No',


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] Random links with Priority always first In reply to
Thank you. I now have the Priority showing up at the top of the list. I now need to get the links to rotate. I've done that before now I just need to figure out how to apply it to this installation.

If I can't figure it out I'll be back - just wanted to let you know that the information you gave worked. The only thing I changed was to make the default "No" for Priority because there's not going to be that many links with that status and I didn't want to forget to change it. :-)
Quote Reply
Re: [SandraJ] Random links with Priority always first In reply to
Okay, here's what I changed in the db_utls.pl:

sub build_sorthit {
# --------------------------------------------------------
# This function sorts a list of links. It has been modified to sort
# new links first, then cool links, then the rest alphabetically. By modifying
# the sort function below, you can sort the links however you like (by date,
# or random, etc.).
#
my (@unsorted) = @_;
my ($num) = ($#unsorted+1) / ($#db_cols+1);
my (%sortby, %priority, %isnew, %iscool, $hit, $i, @sorted);

for ($i = 0; $i < $num; $i++) {
$sortby{$i} = $unsorted[$db_sort_links + ($i * ($#db_cols+1))];
if ($unsorted[$db_isnew + ($i * ($#db_cols+1))] eq "Yes") { $isnew{$i} = 1; }
if ($unsorted[$db_ispop + ($i * ($#db_cols+1))] eq "Yes") { $iscool{$i} = 1; }
}
srand;
foreach $hit (sort { return int(rand(1) + 0.5) ? 1 : -1 } (keys
%sortby)) {

$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
return @sorted;
}

This does a random sort - but it sorts the Priority with the rest.

So what do I need to do to make it rotate by with Priority first and then the rest. Is it also possible to sort randomly within Priority and within the rest? I don't really care if it sorts the pop and new before the rest. I'd like to randomly sort the priority followed by all the other links randomly sorted. That would be the perfect configuration for what I want to do.

Help is much appreciated.
Quote Reply
Re: [SandraJ] Random links with Priority always first In reply to
well, my perl skills are very poor...Blush

I can usually do it but not right off the top of my head....I'd have to research the forums and my notes on the build_sorthit routine.

I'm a little short of time so it would take me 2-3 days, even if I could offer a solution.

Maybe someone else could help...


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] Random links with Priority always first In reply to
Thank you for your time and I really appreciate the effort. I'll spend some time this weekend searching the forums. Maybe I can find an answer and then post the solution here so there will be one thread with all the answers - atleast for this problem. :-)
Quote Reply
Re: [SandraJ] Random links with Priority always first In reply to
I don't know if anyone will answer, but I've read so many threads that this all has become muddled to me. Is there anyone who can put together all the steps to get Links to list all the "priority" links first and then rotate the rest of them?

I think there would be a great many people who would appreciate this.