Jump to content


Photo

Change the "ORDER BY" with forms


  • Please log in to reply
3 replies to this topic

#1 triphis

triphis
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 29 September 2003 - 02:18 AM

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.
Xac Attack

#2 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 29 September 2003 - 02:27 AM

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

#3 triphis

triphis
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 29 September 2003 - 08:37 PM

thank you :P that makes a whole lotta sense >.> *is daft*
Xac Attack

#4 triphis

triphis
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 29 September 2003 - 11:28 PM

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.
Xac Attack




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users