Gossamer Forum
Home : Products : DBMan : Customization :

Sorting records from a search result

Quote Reply
Sorting records from a search result
Dear All,

I would like dbman to implement this like Yahoo classified does.
When users do a search and a list of records are returned and displayed in short display style within a table. The table head row will display selective field name, like Item, Post date, City... I hope those field names are hyperlinked. When users click on one of those field names, dbman will sort the returned records in the order of the clicked field name. Make it even fancier, when the field name is clicked again, dbman will sort the field in opposite order.

Thanks in advance.

Long

Quote Reply
Re: Sorting records from a search result In reply to
Smile I always wondered about that possibility.
Guess this is the question I was afraid to ask.

Close Watch
LyricZ http://www.lyricz.12inter.net
NL
Quote Reply
Re: Sorting records from a search result In reply to
I did this for someone a while back.

In sub html_view_success, change

Code:

for (0 .. $numhits - 1) {
print "<P>";
&html_record (&array_to_hash($_, @hits));
}
to

Code:

$new_url = $ENV{'QUERY_STRING'};
$new_url =~ s/\&sb=\d+//;
$new_url =~ s/\&so=\w+//;
for ($col=0; $col <=$#db_cols; ++$col) {
if ($in{'sb'} == $col) {
if ($in{'so'} eq 'descend') { $sb[$col] = 'ascend'; }
else { $so[$col] = 'descend'; }
}
else { $so[$col] = 'ascend'; }
}
print qq|
<TABLE><TR><TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD></TR>\n|;
for (0 .. $numhits - 1) {
print "<TR>";
&html_record (&array_to_hash($_, @hits));
print "</TR>\n";
}
print "</TABLE>";
Be sure to change FieldName to the name of your field you want to display and the matching #s to the number of that field. You can add as many fields as you want, just by following the pattern above.

Replace sub html_record with the following:

Code:

sub html_record {
my (%rec) = @_;
print qq|
<TD>$rec{FieldName'}</TD>
<TD>$rec{'FieldName'}</TD>
<TD>$rec{'FieldName'}</TD>
<TD>$rec{'FieldName'}</TD>
|;
}
If you're using one of the short/long display mods, the instructions are within the mod.

You'll also need to change sub html_delete_form and sub html_modify_form.

In sub html_delete_form, change

Code:

for (0 .. $numhits - 1) {
%tmp = &array_to_hash($_, @hits);
print qq|<TABLE BORDER=0><TR><TD><INPUT TYPE=CHECKBOX NAME="$tmp{$db_key}" VALUE="delete">
</TD><TD>|;
&html_record (%tmp);
print qq|</TD></TR></TABLE>\n|;
}
to

Code:

$new_url = $ENV{'QUERY_STRING'};
$new_url =~ s/\&sb=\d+//;
$new_url =~ s/\&so=\w+//;
for ($col=0; $col <=$#db_cols; ++$col) {
if ($in{'sb'} == $col) {
if ($in{'so'} eq 'descend') { $sb[$col] = 'ascend'; }
else { $so[$col] = 'descend'; }
}
else { $so[$col] = 'ascend'; }
}
print qq|
<TABLE><TR><TD> </TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD></TR>\n|;
for (0 .. $numhits - 1) {
print "<TR>";
%tmp = &array_to_hash($_, @hits);
print qq|<TD><INPUT TYPE=CHECKBOX NAME="$tmp{$db_key}" VALUE="delete"></TD>|;
&html_record (%tmp);
print "</TR>\n";
}
print "</TABLE>";
In sub html_modify_form, change

Code:

for (0 .. $numhits - 1) {
%tmp = &array_to_hash($_, @hits);
print qq|<TABLE BORDER=0><TR><TD><INPUT TYPE=RADIO NAME="modify" VALUE="$tmp{$db_key}">
</TD><TD>|;
&html_record (%tmp);
print qq|</TD></TR></TABLE>\n|;
}
to

Code:

$new_url = $ENV{'QUERY_STRING'};
$new_url =~ s/\&sb=\d+//;
$new_url =~ s/\&so=\w+//;
for ($col=0; $col <=$#db_cols; ++$col) {
if ($in{'sb'} == $col) {
if ($in{'so'} eq 'descend') { $sb[$col] = 'ascend'; }
else { $so[$col] = 'descend'; }
}
else { $so[$col] = 'ascend'; }
}
print qq|
<TABLE><TR><TD> </TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD>
<TD><a href="$db_script_url?$new_url&sb=#&so=$so[#]">FieldName</a></TD></TR>\n|;
for (0 .. $numhits - 1) {
print "<TR>";
%tmp = &array_to_hash($_, @hits);
print qq|<TD><INPUT TYPE=RADIO NAME="modify" VALUE="$tmp{$db_key}"></TD>|;
&html_record (%tmp);
print "</TR>\n";
}
print "</TABLE>";
If you're not using one of the short/long display mods, you'll also need to change sub html_add_success and sub html_modify_success.

In both subroutines, change

&html_record(&get_record($in{$db_key}));

to

Code:

print qq|
<TABLE><TR><TD>FieldName</TD>
<TD>FieldName</TD>
<TD>FieldName</TD>
<TD>FieldName</TD></TR>\n
<TR>|;
&html_record(&get_record($in{$db_key}));
print "</TR></TABLE>";
JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Sorting records from a search result In reply to
Hi, JPD

Your script works great except for some little problems. If records are displayed using "list all items" command (not through search), and the linked field name is clicked the first time, records will be sorted in ascending order, when I click the second time, the url genereated is not correct. It looks like this "http://long/cgi-bin/relation/db.cgi?db=forrent&uid=long.96330729684357&view_records=1&ItemID=*scend&sb=3&so=descend".
Please note there is a word "scend" between the "temID=*" and "&sb=3&so=descend" in the url. If I take "scend" out, the url is correct. I guess some thing wrong with the pattern matching lines,
$new_url = $ENV{'QUERY_STRING'};
$new_url =~ s/\&sb=\d+//;
$new_url =~ s/\&so=\w//;

Another problem is when the records are returned by a search, the sorting seems not working, it generates the following url:
http://long/cgi-bin/relation/db.cgi?db=forrent&uid=long.96332056481293&sb=&Validated=Yesescend&view_records=1&AreaCode=415&Zipcode=&Type=---&Bedroom=---&Kitchen=---&Bathroom=---&Price-gt=&Price-lt=&view_records=search&view_records.x=14&view_records.y=11scend&sb=9&so=ascend

Thank you very much!

Long

Quote Reply
Re: Sorting records from a search result In reply to
Try adding &so=ascend to your "List All" link in sub html_footer.

I think I may need to see your database in action to know what's going on.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Sorting records from a search result In reply to
Hi, JPDeni

I am currently running DBman on my own computer, so I could not let you try it. I found an error in this line
$new_url=~ s/\&so=\w//;

I changed it to this
$new_url=~ s/\&so=\w+scend//;

That's why there is an extra "scend" in the sorting url. By changing this, now if records are listed by "list all items" commend, sorting order will change alternatively when the field name is clicked.

I tried many changes to solve the second problem (ie if records are listed by searching commend, the sorting order doesn't change when click the field names) without success.
A typical url generated by a area search looks like this:
http://long/cgi-bin/relation/db.cgi?db=forrent&uid=long.96339424792319&sb=&Validated=Yes&so=descend&view_records=1&Area=California&ItemCity=Brisbane&view_records=Search&view_records.x=13&view_records.y=14

While a sorting url generated by the sorting script looks like this:
http://long/cgi-bin/relation/db.cgi?db=forrent&uid=long.96339424792319&sb=&Validated=Yes&so=descend&view_records=1&Area=California&ItemCity=Brisbane&view_records=Search&view_records.x=13&view_records.y=14&sb=2&so=ascend

Clicking field names again could not change "&so=ascend" to "&so=descend".
I think the problem may lie within this codes:
for ($col=0; $col <=$#db_cols; ++$col) {
if ($in{'sb'}== $col) {
if ($in{'so'} eq 'ascend') { $so[$col] ='descend'; }
else { $so[$col] = 'ascend'; }
}
else { $so[$col] = 'descend'; }
}

Probably the Query_strings sent by list all item and search are different, so the above codes only fit the list all item varibles. Any clue?

Long


Quote Reply
Re: Sorting records from a search result In reply to
In Reply To:
$new_url=~ s/\&so=\w//;
I realize now what I did wrong. It should be

$new_url=~ s/\&so=\w+//;

I fixed it in the code above.

In Reply To:
Probably the Query_strings sent by list all item and search are different, so the above codes only fit the list all item varibles.
No, that shouldn't be the case.

I notice that while you have &so=descend, you don't have a &sb= in your URL. Without a field to sort by, the sort order is meaningless. Try adding a default field to sort by and see if that helps.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Sorting records from a search result In reply to
Hi, JPDeni

Thank you for pointing out the error in the url. Since some customized search form may produce url containing "&sb=" or "&so=" (no value after the equal sign), in this case they will not be recognized by the pattern matching statements. So I made a little change to those lines and now everything works fine.

$new_url = $ENV{'QUERY_STRING'};
$new_url =~ s/\&sb=\d?\d?//;
$new_url =~ s/\&so=\w+scend//;
$new_url =~ s/\&so=//;
for ($col=0; $col <=$#db_cols; ++$col) {
if ($in{'sb'} == $col) {
if ($in{'so'} eq 'descend') { $sb[$col] = 'ascend'; }
else { $so[$col] = 'descend'; }
}
else { $so[$col] = 'ascend'; }
}

Long

Quote Reply
Re: Sorting records from a search result In reply to
I get it now. Thank you.

You can make it a little more "elegant" by using


$new_url =~ s/\&sb=\d*//;
$new_url =~ s/\&so=\w*//;


The * means "none or more," while the + means "one or more." For this, we need the *. I just wasn't thinking that there would be any time when there wouldn't be something after &sb= or &so=.

Of course, you don't need to change your code. Whatever works!! Smile

It's a slow process, this learning stuff. But I now have a mod written. Thank you for your help in this.

JPD
http://www.jpdeni.com/dbman/