Обсуждение: Sorting after a search

Поиск
Список
Период
Сортировка

Sorting after a search

От
rickf
Дата:
Many thanks to all who responded & helped with my last query.
Does anyone have any suggestions on a good online resource (with examples)
where I can find out how to process a query then sort the output a key (which is the field used for the search)
Currently I have the script below which works fine as far as it goes.

I need to be able to sort on surname (later on other fields based on user input).

 I tried to work in some of the sort functions like asort but couldn't get any output.
I suspect the problem lies in passing the values from the first search to the sort.

I tried used pg_fetch_array to do the initial search then pass it on to asort but without luck.

Ultimately I will be having three search fields, a user pick list for fields outputted and sorted on.
As you can see I'm a long way from there :-)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 //EN">
<?PHP
if ( strlen($NAME) >0):
        $NAME = addslashes($NAME);
        $NAME = strtolower($NAME);
        $NAME = ucfirst($NAME);
$conn = pg_Connect("dbname=test_db port=5432");
        if(!$conn) { echo "Error in connecting to DB\n"; exit;  }
                $result = pg_Exec($conn, "SELECT * FROM table1 WHERE surname LIKE '$NAME%' ");  
        if(!$result) { echo "Error in output\n"; exit;  }
$num = pg_NumRows($result);
        echo "<br><table border=\"2\">There are $num records that start with  $NAME in the surname field        <br>_____________________________________________________________________________________________________________________________________</table>";             

$i=0;
while ($i < $num)
{
        $cto_from = pg_Result($result, $i, "to_from");
        $ctitle = pg_Result($result, $i, "title");
        $crank = pg_Result($result, $i, "rank");
        $cother = pg_Result($result, $i, "other");
        $cname = pg_Result($result, $i, "surname");
        $cdate = pg_Result($result, $i, "date");
        $cact_date = pg_Result($result, $i, "act_date");
        $cws_add = pg_Result($result, $i, "ws_add");
        $csend_add = pg_Result($result, $i, "send_add");
        $crecp_add = pg_Result($result, $i, "recp_add");
        $csalutn = pg_Result($result, $i, "salutn");
        $copen =  pg_Result($result, $i, "open_wds");
        $cpr_loc = $cid  =  pg_Result($result, $i, "pr_loc");
        $cdoc_type = pg_Result($result, $i, "doc_type");
        $cms_loc =  pg_Result($result, $i, "ms_loc");
        $cms_numb = pg_Result($result, $i, "ms_numb");
        $cms_folio = pg_Result($result, $i, "ms_folio");
        $ctrans = pg_Result($result, $i, "trans");
        $cdatenote = pg_Result($result, $i, "datenote");
        $ctextnote = pg_Result($result, $i, "textnote");
        $cid  =  pg_Result($result, $i, "id");
        $cbio =  pg_Result($result, $i, "bio");

echo "<table border=2><tr><td>DB Number</td><td> $cid </td><td>$cto_from</td><td>$crank $ctitle  $cother $cname</td><td>Dated</td><td>$cact_date ($cdate)</td><tr><td>MS Location</td><td>$cms_loc  $cms_numb $cms_folio</td><td>Opening words</td><td>$csalutn $copen </td><tr>_____________________________________________________________________________________________________________________________________</tr></table>";

$i++;
}
echo "<p> <a href='index.shtml'>Home</a>";
exit;
endif;
?>
<html>
<head>
<title> Database access test</title>
</head>
<body>
<form method='PUT' action='db.php'>
<center>
<h1><i>Test Database.</i></h1>
<table>
<tr><td> <h3>Please enter the surname (or the beginning letters of the name) you wish to query </h3></font></td>
<tr><td> <input type="text" NAME="NAME" VALUE ="" SIZE=40 MAXLENGTH=40>
<BR>
 </td></tr>
 </table>
<input type=submit name=submit value="Press to Query">
</center>a
</body>
</html>

Rick Frank
(mailto:rickf@dufferinresearch.com
________________________________

Re: Sorting after a search

От
"Steve Werby"
Дата:
"rickf" <rickf@dufferinresearch.com> wrote:
> Currently I have the script below which works fine as far as it goes.
>
> I need to be able to sort on surname (later on other fields based on user
> input).
>
>   I tried to work in some of the sort functions like asort but couldn't
get
> any output.
> I suspect the problem lies in passing the values from the first search to
> the sort.
>
> I tried used pg_fetch_array to do the initial search then pass it on to
> asort but without luck.

Why not add an ORDER BY clause to the SQL statement?  It should be much
faster to sort the records within PostgreSQL than within PHP.

> Ultimately I will be having three search fields, a user pick list for
> fields outputted and sorted on.

I'd suggest building your SQL statement dynamically.

$sql = "SELECT * FROM table1 WHERE surname LIKE '$NAME%' ";
if ( $radio[1] == 1 ) { $sql .= 'ORDER BY last_name '; }
else if ( $radio[2] == 1 ) { $sql .= 'ORDER BY first_name '; }

Then if you're using checkboxes to allow the user to set the fields to
display, loop through the user inputted list and dynamically set the fields
to display.  Unless you're using PHP's serialize() and session functions (or
a similar method) you're not going to be able to pass the query results from
page to page to sort anyway - you'll have to requery Postgre every time you
want to change the output on the page based on some user input.

--
Steve Werby
President, Befriend Internet Services LLC
http://www.befriend.com/


Re: Sorting after a search

От
"Adam Lang"
Дата:
I agree. SQL DBs are made to do that stuff.

Adam Lang
Systems Engineer
Rutgers Casualty Insurance Company
http://www.rutgersinsurance.com
----- Original Message -----
From: "Steve Werby" <steve-lists@befriend.com>
To: <pgsql-php@postgresql.org>; "rickf" <rickf@dufferinresearch.com>
Sent: Friday, April 06, 2001 11:49 AM
Subject: Re: [PHP] Sorting after a search


> "rickf" <rickf@dufferinresearch.com> wrote:
> > Currently I have the script below which works fine as far as it goes.
> >
> > I need to be able to sort on surname (later on other fields based on
user
> > input).
> >
> >   I tried to work in some of the sort functions like asort but couldn't
> get
> > any output.
> > I suspect the problem lies in passing the values from the first search
to
> > the sort.
> >
> > I tried used pg_fetch_array to do the initial search then pass it on to
> > asort but without luck.
>
> Why not add an ORDER BY clause to the SQL statement?  It should be much
> faster to sort the records within PostgreSQL than within PHP.
>
> > Ultimately I will be having three search fields, a user pick list for
> > fields outputted and sorted on.
>
> I'd suggest building your SQL statement dynamically.
>
> $sql = "SELECT * FROM table1 WHERE surname LIKE '$NAME%' ";
> if ( $radio[1] == 1 ) { $sql .= 'ORDER BY last_name '; }
> else if ( $radio[2] == 1 ) { $sql .= 'ORDER BY first_name '; }
>
> Then if you're using checkboxes to allow the user to set the fields to
> display, loop through the user inputted list and dynamically set the
fields
> to display.  Unless you're using PHP's serialize() and session functions
(or
> a similar method) you're not going to be able to pass the query results
from
> page to page to sort anyway - you'll have to requery Postgre every time
you
> want to change the output on the page based on some user input.
>
> --
> Steve Werby
> President, Befriend Internet Services LLC
> http://www.befriend.com/
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)