|
-
Aug 3rd, 2004, 09:15 PM
#1
Thread Starter
Fanatic Member
[solved] arranging sorted result
i don't know if my subject meets my need here. hehe. i'm quiet new to this thing and done a 'hello world" so far. i wanted to sort a result by asc/desc when sort button is clicked. this is what i've got so far.
PHP Code:
<style>
body,table,input{
font:9pt arial;
}
td,th{
padding:3px;
}
th{
color:white;
}
</style>
<?php
mssql_connect("x","sa","password");
mssql_select_db("northwind");
echo"<input type=submit value='sort'><br><br>";
echo"<table cellspacing=1 bgcolor=#666699 width=100%>";
echo"<tr><th>ID</th><th>Description</th><th>Region</th></tr>";
load("select * from territories");
echo"</table><br>";
function load($s){
$result=mssql_query($s);
$i=0;
while($row=mssql_fetch_array($result,MSSQL_NUM)){
if($i%2==0) echo"<tr bgcolor=white>";
else echo"<tr bgcolor=#f4f4f4>";
foreach($row as $r) echo "<td>$r</td>";
echo"</tr>";
$i++;
}
}
?>
i don't know how to have the page knowing wether it's asc/desc sort of the result. any help would be greatly appreciated.
Last edited by brown monkey; Aug 4th, 2004 at 04:57 AM.
-
Aug 4th, 2004, 03:54 AM
#2
Re: arranging sorted result
The best way to do this is to use a link which reverses the sorty order. You can append a query string to a link refering back to your PHP script and pick up the value from the $_GET array.
You can see a similar method used by these forums. The following link:
Code:
http://www.vbforums.com/showthread.php?s=&threadid=299757
The bit after the question mark is the query string. You can then pick up the values of these variables in your PHP script as follows:
PHP Code:
$thread = $_GET['threadid'];
I have made a slight modification to your code to enable you to sort.
PHP Code:
<?php
mssql_connect("x","sa","password");
mssql_select_db("northwind");
/* get the search order if specified
1 - is for ascending
0 - is for descending
*/
$order = isset ($_GET['order'])?int_val($_GET['order']):1
if ($order == 0)
$orderby = 'ORDER BY [i]ColName[/i] DESC';
else if ($order == 1)
$orderby = 'ORDER BY [i]ColName[/i] ASC';
else
$orderby = '';
?>
<table cellspacing="1" bgcolor="#666699" width="100%">
<tr>
<th>ID</th>
<th>Description</th>
<th>Region</th>
</tr>
<?php load("select * from territories $orderby"); ?>
</table>
<a href="<?php echo ($_SERVER['PHP_SELF'] . 'order=' . !$order) ?>">Sort</a>
<?php
function load($s){
$result=mssql_query($s);
$i=0;
while($row=mssql_fetch_array($result,MSSQL_NUM)) {
if($i%2==0)
echo"<tr bgcolor=white>";
else
echo"<tr bgcolor=#f4f4f4>";
foreach($row as $r) echo "<td>$r</td>";
echo"</tr>";
$i++;
}
}
?>
-
Aug 4th, 2004, 04:10 AM
#3
Thread Starter
Fanatic Member
thanks for the reply mate. i'll try this when i get home. thanks very much.
-
Aug 4th, 2004, 04:57 AM
#4
Thread Starter
Fanatic Member
hehehe. thanks mate. sooo much. this is my code now.
PHP Code:
<?php
mssql_connect("x","sa","password");
mssql_select_db("northwind");
$order=1;
$order=($_GET['order']==1)?0:1;
$orderby=($order==0)?'order by territoryid desc':'order by territoryid asc';
?>
<table cellspacing="1" bgcolor="#666699" width="100%">
<tr><th>ID</th>
<th>Description</th>
<th>Region</th>
</tr>
<?php load("select * from territories $orderby"); ?>
</table>
<a href="<?php echo ($_SERVER['PHP_SELF'] . '?order=' . $order)?>">Sort</a>
<?php
function load($s){
$result=mssql_query($s);
$i=0;
while($row=mssql_fetch_array($result,MSSQL_NUM)) {
if($i%2==0) echo"<tr bgcolor=white>";
else echo"<tr bgcolor=#f4f4f4>";
foreach($row as $r) echo "<td>$r</td>";
echo"</tr>";
$i++;
}
}
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|