Gossamer Forum
Home : Products : DBMan : Customization :

Default to sorting results by certain field

Quote Reply
Default to sorting results by certain field
I have yet another problem that seems so simple, but... Crazy

I would like to be able to have the list of fields to sort by always show a certain one unless changed. That is, instead of showing ---, I would like it to show a field I specify. I'd still like the user to be able to change it; I just want it to show up that way *unless* changed, like I have now with "Ascending" showing up and sorting that way unless it is changed to "Descending."

I've spent some time trying to do things with the script as well as try some HTML solutions and nothing seems to work. It SHOULD be obvious, I'm sure, but I'm just not seeing it.

Can anyone shed some light on this problem?
Quote Reply
Re: [emyers] Default to sorting results by certain field In reply to
In your html.pl sub html_view_search {

You can add these fields to the form:

<input type="hidden" name="sb" value="$sort_field">
<input type="hidden" name="so" value="$sortorder">

and then in your .cfg file define the variables for example:

$sort_field ='4'; ### sort by date
$sortorder = 'descend';

Hope that helps

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Default to sorting results by certain field In reply to
When I try this solution I get the following error message:

Undefined sort subroutine "main::alpha_ascend~~ascend" called at /spare/apache/cgi-bin/db.cgi line 473

Does anyone have any idea why?
Quote Reply
Re: [emyers] Default to sorting results by certain field In reply to
I don't know why you would be getting an error as I use this in numerous datbases without a problem.

In the $sort_field ='4'; example have you changed this to the field number you want to search on? What type of field is the one you are sorting by: date, numer, alpha?

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Default to sorting results by certain field In reply to
Lois, I don't know either :/

I want to sort by "Last" which is field 1 and is an alpha field.

Maybe this information from my files will help you:

Here is the info from my cfg file:

%db_def = (
ID => [0, 'numer', -2, 255, 1, '', ''],
Last => [1, 'alpha', 15, 255, 1, '', ''],
First => [2, 'alpha', 15, 255, 1, '', ''],
Department => [3, 'alpha', 0, 255, 0, '', ''],
Division => [4, 'alpha', 0, 255, 0, '', ''],
Location => [5, 'alpha', 0, 255, 0, '', ''],
Title => [6, 'alpha', 0, 255, 0, '', ''],
'Contact_For' => [7, 'alpha', '40x5', 999, 0, '', ''],
Phone => [8, 'numer', 15, 255, 0, '', ''],
'E-mail' => [9, 'alpha', 25, 255, 0, '', ''],
Notes => [10, 'alpha', '40x3', 255, 0, '', '']
);

and

Build up some variables from your definitions. Internal use only.
foreach (sort { $db_def{$a}[0] <=> $db_def{$b}[0] } keys %db_def) {
push (@db_cols, $_);
$db_sort{$_} = $db_def{$_}[1];
$db_form_len{$_} = $db_def{$_}[2];
$db_lengths{$_} = $db_def{$_}[3];
$db_not_null{$_} = $db_def{$_}[4];
$db_defaults{$_} = $db_def{$_}[5];
$db_valid_types{$_} = $db_def{$_}[6];
($_ eq $db_key) and $db_key_pos = $db_def{$_}[0];

$sort_field ='1'; ### sort by last name
$sortorder = 'ascend';

}

and from html.pl

< -- End page text -->

print qq|
<form action="$db_script_url" method="GET" name="form1">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">

<input type="hidden" name="sb" value="$sort_field">
<input type="hidden" name="so" value="$sortorder">

|;
Quote Reply
Re: [emyers] Default to sorting results by certain field In reply to
Perhaps the error is due to having the variables within a sub. Move these lines further up in your .cfg file so they are not within any other sub routine:

$sort_field ='1'; ### sort by last name
$sortorder = 'ascend';

Hope that helps

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Default to sorting results by certain field In reply to
Lois, I figured it out ("sort" of) -- pun not originally intended...but it sure is appropriate

I kept staring at that line in which the debugger was showing the error and I finally saw:

($sort_order = "ascend")

I was defining that variable, again, I think, with

$sortorder = 'ascend'

which is what was causing the "ascend_ascend."

I removed that hidden field from both the html.pl and the definition from my cfg file and now it works. I left the $sortorder = 'ascend' both defined in the cfg and as a hidden in my form in html.pl.

I also took your suggestion about moving the variables out of the subroutine and that may have been part of the problem, but until I removed my extra defiintion it wasn't working.

I knew if I stared at it long enough something would appear. <g>

Thanks for all your time spent helping me and others on this forum!

Elizabeth

Last edited by:

emyers: Nov 17, 2003, 11:13 AM
Quote Reply
Re: [emyers] Default to sorting results by certain field In reply to
Well, I thought I had it figured out...

What I have now is better than nothing, but it forces a sort by that field no matter what field is chosen. What I wanted to do is sort by "Last" as the default, but allow others to change it if they wanted.

I'll keep on trudging... :)

Elizabeth