Gossamer Forum
Home : General : Perl Programming :

problem with arrays in sub query

Quote Reply
problem with arrays in sub query
Hi all,
using DBman flatfile......
I'm using the "sort by multiple fields" mod sorting on 3 fields (with great success, I should say). But I do have one problem. This mod only works if $values[$in{'sb1'}] contains 1 single value. In my case I have multiple values in this field (db position is 1). I know, I know, but I wanted to try it anyway.....

Now, I figured out to add the following in sub query just after:


open (DB, "<$db_file_name") or &cgierr("error in search. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and next LINE; # Skip comment Lines.
(/^\s*$/) and next LINE; # Skip blank lines.
$line = $_; chomp ($line); # Remove trailing new line.
@values = &split_decode($line);


# my new code
# $values[1] being the sort field in question
if ($in{'view_select_records'}) {
@tmp_value = split(/\|/, $values[1]); #$values[1] in this case eq "value1|value2|value3" etc.
foreach $tmp_value (@tmp_value) {
$values[1] = $tmp_value;
}
push (@values, $values[1]);
}
# end

this does not interfere with any of my other search routines and prints out the record with multiple field values no problemo, but uses only 1 of the available field values (not necessarily the first one?). In other words, it does not make a "temporary copy" (for want of a better word) of the record (or should I say $line). How can I stack all the newly assigned $values[1] in @values? I need to "push" @values so that I don't have to worry about the rest of sub query or any other subsequent sorting functions. This also lets me call $rec{'fieldname'} as usual.

I know I'm missing something rudimentary....I've tried everything I can think of and I'm not one to give up easily....been on this thing for hours.

Thanks for any help.

Nora
Nora @
www.baytides.ca
Quote Reply
Re: (cont.) problem with arrays in sub query In reply to
ok,

I've now changed my code as follows:

open (DB, "<$db_file_name") or &cgierr("error in search. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and next LINE; # Skip comment Lines.
(/^\s*$/) and next LINE; # Skip blank lines.
$line = $_; chomp ($line); # Remove trailing new line.
if ($in{'view_select_records'}) {
@values = &split_decode($line);
@tmp_value = split(/\|/, $values[1]); #$values[1] in this case eq "value1|value2|value3" etc.
for ($i = 1; $i <= $#tmp_value; $i++) {
$values[1] = $tmp_value[$i];
}
push (@values);
}
else {
@values = &split_decode($line);
}

It obviously does not want to display those records more than once....duh....

How can I tell DBman to display a record '$i' # of times based on $tmp_value[$i} found? I don't know how else to ask?

Again here's what I'm trying to achieve:
- in sub html_record I'd like to call $rec{'Type'} (while this is the field with multiple values), but it will only return 1 value, while printing the associated record # (# of multiple values) of times. In essence I want to add the record # of times to the %rec hash but with different values in $rec{'Type'}. Does that make sense to anybody else but me?

Does anybody have any ideas?

At this point I'm ready to try just about anything.

Thanks

Nora
Nora @
www.baytides.ca
Quote Reply
Re: [baytides] (cont.) category headings and records. In reply to
ok, I changed my approach and gave up on the change to sub query. I actually got this work, but I still have one small problem.

I've now got the following in my version of html_success:

Code:
my (@tmp_category) = split (/,/, $db_select_fields{'Type'});foreach $tmp_category (@tmp_category) {

print qq|
#-- all my category heading stuff ---#
|;
for (0 .. $numhits - 1) {
&select_property_display (&array_to_hash($_, @hits));
}
}
then in my version of html_record I have the following:
Code:
my (@types) = split(/\|/, $rec{'Type'}); # This takes care of multiple values in the category field
foreach $type (@types) { # and should a value contain a space (ie. 'Cottage Resort')

$new_type = $type . '_'; # it will make it a unique entry, so that 'Cottage Resort'
# does not get mixed in with 'Cottage'

if ($new_type =~ /$tmp_category\_/) {
print qq|#-- here goes all my record specific stuff --#|;}}
Now my problem is that should a Category NOT contain any records, it will still print the category heading.
That in itself is not a problem but I would like to add a little note (like "no listings in this category")
should this occur.

I've tried to place my little note in several places in html_record and html_view_success with no real success, mainly
because I'm probably not approaching this problem the correct way.How can check that there's indeed a record in a category? Do I have to do a new query?I would appreciate any help.Thanks
Nora @
www.baytides.ca
Quote Reply
Re: [baytides] (cont.) category headings and records. In reply to
If you set your Category field not_null to 1 in your Database Definition %db_def located in your .cfg, then sub validate_record in your .cgi will catch that and return an error.
To check your fields from any sub you can use -
Code:

$status = &validate_record;
if ($status) {
print "$status";
}

This is the code snip from DBMan v1 from sub validate_record which will catch that.
Code:
foreach $col (@db_cols) {
if ($in{$col} =~ /^\s*$/) { # entry is null or only whitespace
($db_not_null{$col}) and # entry is not allowed to be null.
push(@input_err, "$col (Can not be left blank)"); # so let's add it as an error
}

Bob
http://totallyfreeads.com.au
Quote Reply
Re: [lanerj] (cont.) category headings and records. In reply to
Thanks, but I tried that already.

See, first I'm asking the program to print all records for each category and then I do my matching in html_record to get rid of the repetitions.

With your suggestion (and what I've tried) "$status" will be printed for each record and each category.

I guess I need to do the matching before I get to html_view_success, I just don't know how because of the multiple values in the category field.Pirate
Nora @
www.baytides.ca
Quote Reply
Re: Solution, I think:) In reply to
For some unexplained reason, after i changed html_view_sucess to:

Code:
my (@tmp_category) = split (/,/, $db_select_fields{'Type'}); foreach $tmp_category (@tmp_category) {
if (grep $_ =~ $tmp_category, @hits) {

&tmp_category(&array_to_hash($_, @hits)); for (0 .. $numhits - 1) {
&select_property_display (&array_to_hash($_, @hits));
}
}
}


it works:))) at least for now:))
Nora @
www.baytides.ca