Page 1 of 1

Multiple sorting

Posted: Fri Nov 17, 2006 2:04 am
by twodogdog
JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Dear all,

I am fresh in writing PHP. I would like to make a multiple sorting function on my programme but no idea how to make it. I would grateful if someone can help in this forum.


Here is a code I wrote:


[b]In mysql[/b]

Code: Select all

// describe the variable and statment for sorting
$sort_string = "order_status=".$_GET['value_status']." AND order_action=".$_GET['value_action']." AND order_circuit=".$_GET['value_circuit']." AND order_bandwidth=".$_GET['value_bandwidth']." AND order_pm=".$_GET['value_pm'];

$sort = $_GET['value_status'] && $_GET['value_action'] && $_GET['value_circuit'] && $_GET['value_bandwidth'] && $_GET['value_pm'];


// get table content
if($sort) {
	$result = mysql_query("SELECT id,DATE_FORMAT(order_date,'%d %b %Y')AS'order_date',order_status,order_int_ref,order_ab,order_customer,order_action,order_desc,order_circuit,order_bandwidth,order_destination,order_arcstar,order_rfs,order_pm,DATE_FORMAT(order_comp_date,'%d %b %Y')AS'order_comp_date',mod_person,mod_time FROM Mytable WHERE ".$sort_string." ORDER BY (DATE_FORMAT(order_date,'%Y %m %d')) DESC LIMIT $offset,$rowsPerPage");
}
In display part:

Code: Select all

echo "<form action=\"order_list.php?value_status=$key_status&value_action=$key_action&value_circuit=$key_circuit&value_bandwidth=$key_bandwidth&value_pm=$key_pm\">";
// select box of Order Status
echo "<li>";
echo "Sort by Status<br>";
echo "<select name=\"value_status\">";
if (is_array($arrVal["order_status"])) {
foreach($arrVal["order_status"] as $key_status => $value){
echo "<option value=\"$key_status\">$value</option>";
} 
}
echo "</select>";
echo "</li>";

// select box of Order Action
echo "<li>";
echo "Sort by Action<br>";
echo "<select name=\"value_action\">";
if (is_array($arrVal["order_action"])) {
foreach($arrVal["order_action"] as $key_action => $value){
echo "<option value=\"$key_action\">$value</option>";
} 
}
echo "</select>";
echo "</li>";

// select box of Order Circuit 
echo "<li>";
echo "Sort by Circuit<br>";
echo "<select name=\"value_circuit\">";
if (is_array($arrVal["order_circuit"])) {
foreach($arrVal["order_circuit"] as $key_circuit => $value){
echo "<option value=\"$key_circuit\">$value</option>";
} 
}
echo "</select>";
echo "</li>";

// select box of Bandwidth 
echo "<li>";
echo "Sort by Bandwidth<br>";
echo "<select name=\"value_bandwidth\">";
if (is_array($arrVal["order_bandwidth"])) {
foreach($arrVal["order_bandwidth"] as $key_bandwidth => $value){
echo "<option value=\"$key_bandwidth\">$value</option>";
} 
}
echo "</select>";
echo "</li>";

// select box of PM 
echo "<li>";
echo "Sort by PM<br>";
echo "<select name=\"value_pm\">";
if (is_array($arrVal["order_pm"])) {
foreach($arrVal["order_pm"] as $key_pm => $value){
echo "<option value=\"$key_pm\">$value</option>";
} 
}
echo "</select>";
echo "</li>";


echo "<li><br><input type='submit' /></li>";
echo "</ul></form>;

Thanks a lots


JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Sorting

Posted: Fri Nov 17, 2006 9:07 am
by timclaason
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Usually, if I have multiple fields I can sort by, I'll have 1 querystring variable.
So for each field you want to sort by:

[syntax="html"]
<TABLE>
<TR>
<TD><a href='page.php?sortby=field1'>Field1</A></TD>
<TD><a href='page.php?sortby=field2'>Field2</A></TD>
<TD><a href='page.php?sortby=field3'>Field3</A></TD>
</TR>
<!--Finish Rest of HTML-->
Then, on page.php[/syntax]

Code: Select all

$sortby = $_GET['sortby'];
if($sortby == "field1") {
$query = "SELECT * ... ORDER BY fld_field1";
}
elseif($sortby == "field2") {
$query = "SELECT * ... ORDER BY fld_field2";
}
elseif($sortby == "field3") {
$query = "SELECT * ... ORDER BY fld_field3";
}
else{
$query = "SELECT *...";
}
$SQL = mysql_query($query, $db_link);
That's how I do it, anyway


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]