Jump to content

Change the "ORDER BY" with forms


triphis

Recommended Posts

I was wondering how I can reorder the MySQL query \"order by\" with the use of forms. I have no clue.

 

My friend owns this site, but because she knows ASP, rather than PHP, she was unable to help me.

 

http://neoguide.dcwd.biz/Pet.asp

 

See all the buttons? By clicking on them, the list order is changed.

Link to comment
https://forums.phpfreaks.com/topic/1080-change-the-order-by-with-forms/
Share on other sites

you\'d want to pass a variable to the page and have that variable tell what query to run. Something like

 

[php:1:74ec0ffbbe]<?php

if ($_REQUEST[submitted] == \"true\")

{

 

$sort = $_POST[\'sort\'];

 

if ($sort == \"name\")

{

$query = mysql_query(\"select user_id, user_name, user_address, user_type from table ORDER BY user_name\");

} elseif ($sort == \"address\") {

$query = mysql_query(\"select user_id, user_name, user_address, user_type from table ORDER BY user_address\");

} elseif ($sort == \"type\") {

$query = mysql_query(\"select user_id, user_name, user_address, user_type from table ORDER BY user_type\");

} else {

$query = mysql_query(\"select user_id, user_name, user_address, user_type from table ORDER BY user_id\");

}

}

?>

<FORM ACTION=\"<?= $_SERVER[\"PHP_SELF\"] ?>\" METHOD=\"post\">

 

<SELECT NAME=\"sort\">

<OPTION SELECTED VALUE=\"name\">User Name

<OPTION VALUE=\"address\">User Address

<OPTION VALUE=\"type\">User Type

</SELECT>

</FORM>

[/php:1:74ec0ffbbe]

 

That\'s how I\'d do it anyway :)

Hey, if I wanted to shorten this:

 

[php:1:d805344b15]<?php

if ($sort == \"name\")

{

$query = mysql_query(\"select user_id, user_name, user_address, user_type from table ORDER BY user_name\");

} elseif ($sort == \"address\") {

$query = mysql_query(\"select user_id, user_name, user_address, user_type from table ORDER BY user_address\");

} elseif ($sort == \"type\") {

$query = mysql_query(\"select user_id, user_name, user_address, user_type from table ORDER BY user_type\");

} else {

$query = mysql_query(\"select user_id, user_name, user_address, user_type from table ORDER BY user_id\");

}

?>[/php:1:d805344b15]

 

I could go like this right? :

 

[php:1:d805344b15]

$query = mysql_query(\"select user_id, user_name, user_address, user_type from table \");

if ($sort == \"name\")

{

$query .= ORDER BY user_name;

} elseif ($sort == \"address\") {

$query .= ORDER BY user_address;

} elseif ($sort == \"type\") {

$query .= ORDER BY user_type;

} else {

$query .= ORDER BY user_id\");

}

[/php:1:d805344b15]

 

I don\'t know the correct use of \" .= \" but I know it adds to a previous variable.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.