call "ORDER BY" command from php

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

sryuuza
Forum Newbie
Posts: 14
Joined: Mon Aug 16, 2004 1:18 am
Location: Olympia, WA
Contact:

Post by sryuuza »

hmmm, i guess i dont understand how to make the variable work, so that it changes... would use of rand() help maybe?

##EDIT##

Code: Select all

//get field names
$kv = rand(0,1);
print "<tr>\n";
print "
<th><a href='address2.php?id=$kv'>ID</a></th>
<th><a href='address2.php?fname=0'>First Name</a></th>
<th><a href='address2.php?lname=0'>Last Name</a></th>
<th><a href='address2.php?email=0'>Email</a></th>
<th><a href='address2.php?pnum=0'>Phone Number</a></th>\n";
print "</tr>\n\n";
the above code works, but not consistenly... is there a way to tell it to toggle between 0 and 1, and not randomly generate?
##EDIT##
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

not especially...

Code: Select all

echo $_SERVER['PHP_SELF'] . '?your_recognized_key_here=' . ((int)!(isset($_GET['your_recognized_key_here']) ? $_GET['your_recognized_key_here'] : 1));
you can make that into a function or something...
sryuuza
Forum Newbie
Posts: 14
Joined: Mon Aug 16, 2004 1:18 am
Location: Olympia, WA
Contact:

Post by sryuuza »

so, i would just make up a recognized key of my choice...
like a variable...

and then where would i put that code, and how would i call it up?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you'd use that whereever you wanted to write out a url for your sorting.. depending on if you made that a function or switched it around to store the variable you can save off the url to echo out later..
sryuuza
Forum Newbie
Posts: 14
Joined: Mon Aug 16, 2004 1:18 am
Location: Olympia, WA
Contact:

Post by sryuuza »

everything is working great....

http://therandomjoe.com/DJM/idea4/address2.php

how do i specify certain fields that i want to show up, right now i just query the data base, and get all the fields... but i dont want to display the ID... how do i make it do i get everything but that field?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Instead of using SELECT * FROM table, you just specify the columns. SELECT col1, col2, col3 FROM table
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

reduce code:

index.php?o=lname

Code: Select all

if(isset($_GET['o'])){
  $o = $_GET['o'];
}
else {
  $o = "default_column";
}
$sql = "SELECT * FROM table ORDER BY $o ASC";
sryuuza
Forum Newbie
Posts: 14
Joined: Mon Aug 16, 2004 1:18 am
Location: Olympia, WA
Contact:

Post by sryuuza »

thank you, it ended up working great....

http://djmdevelopment.com/address2.php

but then i changed the fields for numbers... and ran into a whole new problem...

http://djmdevelopment.com/propertieslist.php

i have made a new post regarding this issue here...

http://www.devnetwork.net/forums/viewto ... 249#128249

any help is greatly appreciated :D
Post Reply