Jump to content

Change the "ORDER BY" with forms


triphis

Recommended Posts

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.