Jump to content

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.

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.