Gossamer Forum
Home : Products : Links 2.0 : Customization :

Problem with review mod.

Quote Reply
Problem with review mod.
I'm making one and I am trying to show an x number of reviews on the detailed pages for each link. I want to show the latest ones though. I can easily do it showing the first 2 but I used the Lastlink mod and I can't get it to work. I can see why but don't know how to fix it.

In sub detailed_view I have

$review = &opinions($rec{'ID'});
so that each time nph-build.cgi calls it for each Id it will call sub opinions, which looks like this


Code:
sub opinions {
my $id = shift;
my $x = 2;
my (@lines, $link, @tmp, %tmp);
open (D, "<$db_opinions_name") or &cgierr("unable to open database:$db_opinions_name.\nReason: $!");
@lines = <D>;
close D;
A: for ($i = $#lines; $i >= $#lines - $x + 1; $i--) {
chomp $lines[$i];
@tmp = &split_op_decode ($lines[$i]);
%tmp = &array_to_op_hash (0, @tmp);
$link .= &site_html_opinions_link (%tmp) if ($tmp[$db_op_pid] == $id);
}
return $link;
}
see so I have it set to show 2 reviews per ID. In my review db, I have the id to the link that the review is for stored as $db_op_pid. I check it against $id. That part works but when $x is set at 2 it will only print two for the first detailed page. When I set it to 4 (there are only three reviews for the first and second links), it prints 3 for the first and 1 for the second. So it's obviously not resetting or anything. Does anyone have a suggestion for better code? I was thinking of just starting the loop over.
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Problem with review mod. In reply to
A while() loop would be quicker than slurping into an array then looping.

Using undef may work to reset the count?

Last edited by:

RedRum: Jan 9, 2002, 2:49 PM
Quote Reply
Re: [RedRum] Problem with review mod. In reply to
I don't think I need to reset the count though. It's weird. I tried a while loop before:

Code:
sub opinions {
my $bid = shift;
my $q = 2;
my (@opinion, @values, %tmp, $i);
local ($link);
open (D, "<$db_opinions_name") or &cgierr("unable to open database:$db_opinions_name.\nReason: $!");
LINE: while (<D>) {
/^#/ and next LINE;
/^\s*$/ and next LINE;
chomp;
@values = &split_op_decode($_);
push (@opinion, @values) if ($values[$db_op_pid] == $bid);
}
close D;for ($i = 0; $i <= $q - 1; $i++) {
%tmp = &array_to_op_hash ($i, @opinion);
last if ($tmp{$db_op_key} eq "");
$link .= &site_html_opinions_link (%tmp) if (@opinion);
}
return $link;
}


This works fine and I get the results for each link printed, but it prints the first 2 reviews instead of the last 2. I tried using reverse sorts on the arrays and all that. When I try to edit the for loop to start at the total reviews using $#opinion, and go down to 0, I get this error in build:

Modification of non-creatable array ... subscript -1. So it prints a negative integer.
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Problem with review mod. In reply to
you should be able to combine the string to array to hash stuff all into one line...
Code:
sub opinions {
my ($id, $i, $x, $link) = (shift, 0, 2, '');
open (D, "<$db_opinions_name") or &cgierr("unable to open database:$db_opinions_name.\nReason: $!");
while (reverse<D>) {
($i < $x && /^$id\|/) and $i++ or next;
$link .= site_html_opinions_link (array_to_op_hash(0, split_op_decode(chomp())));
}
close (D);
return $link;
}
don't know if that'll compile, but I've done that sort of thing before...

hm... that regexp is assuming that pid is the first field... otherwise you can change it...


--Philip
Links 2.0 moderator

Last edited by:

ThatPerson1024: Jan 9, 2002, 3:20 PM
Quote Reply
Re: [ThatPerson1024] Problem with review mod. In reply to
Looks like better code than mine, but I don't think it compiles. Nph-build.cgi freezes on the first page and the perl process doesn't close until I restart my server. I don't know why, I can't see any problems with it.
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Problem with review mod. In reply to
when I take off reverse from the loop, it builds but I get no data passed to the site_html_opinions_link, I do get 2 of them however but they are blank.
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Problem with review mod. In reply to
darn ;-) told you I wasn't sure it would compile but it does look sexy doesn't it?

--Philip
Links 2.0 moderator
Quote Reply
Re: [ThatPerson1024] Problem with review mod. In reply to
Hey thats my saying! Cool


Quote Reply
Re: [ThatPerson1024] Problem with review mod. In reply to
sure does. I'll try playing around with it some more.
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Problem with review mod. In reply to
well, not as *sexy* as the other code, but a simple while loop got it.

Code:
sub opinions {
my ($id, $i, $x, $link, $count) = (shift, 0, 3, '', 0);
my (@opinion, @values, %tmp);open (D, "<$db_opinions_name") or &cgierr("unable to open database:$db_opinions_name.\nReason: $!");
LINE: while (<D>) {
/^#/ and next LINE;
/^\s*$/ and next LINE;
chomp;
@values = &split_op_decode($_);
push (@opinion, @values) and $count++ if ($values[$db_op_pid] == $id);
}
close D;for ($i = $count-1; $i >= $count/$x - 1; $i--) {
%tmp = &array_to_op_hash ($i, @opinion);
last if ($tmp{$db_op_key} eq "");
$link .= &site_html_opinions_link (%tmp) if (@opinion);
}
return $link;
}

Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Problem with review mod. In reply to
Out of interest why are you using a different split_decode routine?
Quote Reply
Re: [RedRum] Problem with review mod. In reply to
Well, since I am using two different def files in the same opinions.cgi, I would need to change pretty much everything in my opinions.def from the fields to db_cols. Fields so I could use the Link fields in the template as well as the reviews. So in the opinions.def instead of ID, I have opID. And db_cols changed to db_op_cols b/c it would interfere with the links.def one. So that's why I have all those subroutines. If you look, the originals in db_utils are setup for db_cols and db_sort, but I had to change mine to db_op_cols, so it wouldn't work anyway. So I just made the new subs. Hope I explained well. If you can see a way that I can require two files with the same names for variables, but yet still have everything work, i'd love to know.
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Problem with review mod. In reply to
I attempted a very hopeful change to not have different subs, but it didn't work. So, for now, it stands that to add this mod, you'll need to have about 3-4 subs in db_utils.pl that are already there, but designated for the opinions database. I finished the admin side. Works great. It's 2 seperate required files, but tied together with the admin by admin.cgi. It currently allows searching for a review before modifying/deleting/validating it. Emails will get sent to the person who reviewed it, notifying them of validation or rejection, but there will be a checkbox there to turn this off. Um, I've tied it in with the detailed pages. Do you think I should allow searching for a review? I don't really see the point of this. Lets see, the user can sort by rating, or date in ascending or descending order. This is all dynamic, except for the part built on the detailed pages. You can show links that are positive, or Links that are negative or all. I'll work on an easy way to change images from dots to stars, or whatever. I'll have a demo up soon.
Lavon Russell
LookHard Mods
lavon@lh.links247.net