hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : mySQL - ORDER BY
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

mySQL - ORDER BY

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 09-15-2002, 04:47 PM
NickMahon NickMahon is offline
WHT Addict
 
Join Date: Apr 2002
Posts: 112

mySQL - ORDER BY


PHP Code:
if($alpha==""){
    
mysql_query("SELECT name FROM album_collection ORDER BY name");
} elseif(
$alpha=="name"){
    
mysql_query("SELECT name FROM album_collection ORDER BY name");
} elseif(
$alpha=="artist"){
    
mysql_query("SELECT artist FROM album_collection ORDER BY artist");
} elseif(
$alpha=="rating"){
    
mysql_query("SELECT rating FROM album_collection ORDER BY rating");

Is there something wrong with this? It has no effect whatsoever.

__________________
- Nick Mahon
Vereor.com
Professional Web Development

Reply With Quote


Sponsored Links
  #2  
Old 09-15-2002, 04:50 PM
Alturus Alturus is offline
Newbie
 
Join Date: Aug 2002
Location: Canada
Posts: 22
Add some conditions at the end

SELECT blah FROM blah ORDER BY blah DESC

or

SELECT blah FROM blah ORDER BY blah ASC

Reply With Quote
  #3  
Old 09-15-2002, 04:53 PM
NickMahon NickMahon is offline
WHT Addict
 
Join Date: Apr 2002
Posts: 112
Still no luck.

Here is the full code:

PHP Code:
<?
include("dbconnect.inc");
$result mysql_query("SELECT name, artist, comments, rating FROM album_collection"$link_id);
if(
$alpha==""){
    
mysql_query("SELECT name FROM album_collection ORDER BY name ASC");
} elseif(
$alpha=="name"){
    
mysql_query("SELECT name FROM album_collection ORDER BY name ASC");
} elseif(
$alpha=="artist"){
    
mysql_query("SELECT artist FROM album_collection ORDER BY artist ASC");
} elseif(
$alpha=="rating"){
    
mysql_query("SELECT rating FROM album_collection ORDER BY rating ASC");
}
print(
"<table align=\"center\" border=\"0\" cellspacing=\"1\" cellpadding=\"4\">");
print(
"<tr align=\"center\">");
print(
"<td class=\"headerrow\" nowrap><strong><a href=\"?alpha=name\">Album Name</a></strong></td>");
print(
"<td class=\"headerrow\" nowrap><strong><a href=\"?alpha=artist\">Artist</a></strong></td>");
print(
"<td class=\"headerrow\" nowrap><strong><a href=\"?alpha=rating\">Rating</a></strong></td>");
print(
"</tr>");
$counter 0;
while(
$query_data mysql_fetch_row($result)) {
    
$counter++;
    if(
$counter%2==0) {
        
$rowclass row2;
    } else {
        
$rowclass row1;
    }
    print(
"<tr align=\"center\">");
    print(
"<td class=\"$rowclass\" nowrap>$query_data[0]</td>");
    print(
"<td class=\"$rowclass\" nowrap>$query_data[1]</td>");
    print(
"<td class=\"$rowclass\" nowrap><img src=\"$query_data[3].gif\"></td>");
    print(
"</tr>");
    if(
$query_data[2]!="N/A") {
        print(
"<tr align=\"center\">");
        print(
"<td class=\"$rowclass\" colspan=\"3\"><b>Comments:</b> $query_data[2]</td>");
        print(
"</tr>");
    }
}
print(
"</table>");
?>

__________________
- Nick Mahon
Vereor.com
Professional Web Development

Reply With Quote
Sponsored Links
  #4  
Old 09-15-2002, 04:56 PM
Alturus Alturus is offline
Newbie
 
Join Date: Aug 2002
Location: Canada
Posts: 22
Well, try using SELECT * FROM

Because as it stands you seem to be only selecting one bit of info:

SELECT whatever FROM...

Only selects 'whatever' not all the data.

__________________
http://www.alturus.ca
Professional Web-Design/Programming

Your CSS2.0/XHTML table-less guru. Pushing for properly coded webpages.

Reply With Quote
  #5  
Old 09-15-2002, 04:58 PM
Alturus Alturus is offline
Newbie
 
Join Date: Aug 2002
Location: Canada
Posts: 22
Oh oh, try this:

PHP Code:
<?
include("dbconnect.inc");

if(
$alpha==""){
   
$sort "ORDER BY name ASC";
} elseif(
$alpha=="name"){
   
$sort "ORDER BY name DESC";
} elseif(
$alpha=="artist"){
   
$sort "ORDER BY artist ASC";
} elseif(
$alpha=="rating"){
   
$sort "ORDER BY rating ASC";
}

$result mysql_query("SELECT name, artist, comments, rating FROM album_collection $sort"$link_id);

print(
"<table align=\"center\" border=\"0\" cellspacing=\"1\" cellpadding=\"4\">");
print(
"<tr align=\"center\">");
print(
"<td class=\"headerrow\" nowrap><strong><a href=\"?alpha=name\">Album Name</a></strong></td>");
print(
"<td class=\"headerrow\" nowrap><strong><a href=\"?alpha=artist\">Artist</a></strong></td>");
print(
"<td class=\"headerrow\" nowrap><strong><a href=\"?alpha=rating\">Rating</a></strong></td>");
print(
"</tr>");
$counter 0;
while(
$query_data mysql_fetch_row($result)) {
    
$counter++;
    if(
$counter%2==0) {
        
$rowclass row2;
    } else {
        
$rowclass row1;
    }
    print(
"<tr align=\"center\">");
    print(
"<td class=\"$rowclass\" nowrap>$query_data[0]</td>");
    print(
"<td class=\"$rowclass\" nowrap>$query_data[1]</td>");
    print(
"<td class=\"$rowclass\" nowrap><img src=\"$query_data[3].gif\"></td>");
    print(
"</tr>");
    if(
$query_data[2]!="N/A") {
        print(
"<tr align=\"center\">");
        print(
"<td class=\"$rowclass\" colspan=\"3\"><b>Comments:</b> $query_data[2]</td>");
        print(
"</tr>");
    }
}
print(
"</table>");
?>
I use basically the same sort process at http://supratest.ue3.net

PHP Code:
if($sort == 'user_desc'){
$sql_sort 'ORDER BY username DESC';
}
elseif(
$sort == 'user_asc'){
$sql_sort 'ORDER BY username ASC';
}
elseif(
$sort == 'date_asc'){
$sql_sort 'ORDER BY id ASC';
}
elseif(
$sort == 'date_desc'){
$sql_sort 'ORDER BY id DESC';
}
elseif(
$sort == 'car_asc'){
$sql_sort 'ORDER BY supra ASC';
}
elseif(
$sort == 'car_desc'){
$sql_sort 'ORDER BY supra DESC';
}
elseif(
$sort == 'votes_asc'){
$sql_sort 'ORDER BY votes ASC';
}
elseif(
$sort == 'votes_desc'){
$sql_sort 'ORDER BY votes DESC';
}
else{
$sql_sort 'ORDER BY supra DESC';
}

then further past a bunch of crap:

$sql "SELECT * FROM accounts $sql_sort LIMIT 100";
$result mysql_query($sql) or die ("Invalid Query");
while(
$myrow mysql_fetch_array($result)){


etc etc etc 

__________________
http://www.alturus.ca
Professional Web-Design/Programming

Your CSS2.0/XHTML table-less guru. Pushing for properly coded webpages.

Reply With Quote
  #6  
Old 09-15-2002, 04:59 PM
NickMahon NickMahon is offline
WHT Addict
 
Join Date: Apr 2002
Posts: 112
Well, I want the user to be able to click on a link on view.php such as "Album Name" - this will take them to view.php?alpha=name, thus executing

PHP Code:
} elseif($alpha=="name"){
    
mysql_query("SELECT name FROM album_collection ORDER BY name ASC");

and ordering the field alphabetically.

__________________
- Nick Mahon
Vereor.com
Professional Web Development

Reply With Quote
  #7  
Old 09-15-2002, 05:01 PM
NickMahon NickMahon is offline
WHT Addict
 
Join Date: Apr 2002
Posts: 112
Thanks

__________________
- Nick Mahon
Vereor.com
Professional Web Development

Reply With Quote
  #8  
Old 09-15-2002, 05:01 PM
RackNine RackNine is offline
Web Hosting Master
 
Join Date: May 2002
Location: Edmonton, Canada
Posts: 978
mysql_query("SELECT name FROM album_collection ORDER BY name ASC");

Shouldn't the above be stored into a result object? ie:

$result = mysql_query("SELECT name FROM album_collection ORDER BY name ASC");

__________________
Matt Meier, RackNine Inc.
email: mmeier@racknine.com
web: http://www.racknine.com

Reply With Quote
  #9  
Old 09-15-2002, 05:02 PM
Alturus Alturus is offline
Newbie
 
Join Date: Aug 2002
Location: Canada
Posts: 22
PHP Code:
$result mysql_query("SELECT name, artist, comments, rating FROM album_collection"$link_id); 
You've already selected your info here, no need to do it again, it won't work. What you need to do is append a sorting condition on this statement, like:

PHP Code:
$result mysql_query("SELECT name, artist, comments, rating FROM album_collection ORDER BY name DESC"$link_id); 

__________________
http://www.alturus.ca
Professional Web-Design/Programming

Your CSS2.0/XHTML table-less guru. Pushing for properly coded webpages.

Reply With Quote
  #10  
Old 09-15-2002, 05:04 PM
NickMahon NickMahon is offline
WHT Addict
 
Join Date: Apr 2002
Posts: 112
Yup, worked great.

__________________
- Nick Mahon
Vereor.com
Professional Web Development

Reply With Quote
  #11  
Old 09-15-2002, 09:13 PM
drsenay drsenay is offline
New Member
 
Join Date: Sep 2002
Posts: 2
Quote:
Originally posted by NickMahon
PHP Code:
if($alpha==""){
    
mysql_query("SELECT name FROM album_collection ORDER BY name");
} elseif(
$alpha=="name"){
    
mysql_query("SELECT name FROM album_collection ORDER BY name");
} elseif(
$alpha=="artist"){
    
mysql_query("SELECT artist FROM album_collection ORDER BY artist");
} elseif(
$alpha=="rating"){
    
mysql_query("SELECT rating FROM album_collection ORDER BY rating");

Is there something wrong with this? It has no effect whatsoever.

It will work faster and simpler if you do this:

PHP Code:
switch ($alpha){
    case 
"":
    case 
"name":
        
$sql "SELECT name FROM album_collection ORDER BY name";
    break;
    case 
"artist":
        
$sql "SELECT artist FROM album_collection ORDER BY artist";
    break;
    case 
"rating":
        
$sql "SELECT artist FROM album_collection ORDER BY rating";
    break;
}

mysql_query($sql); 

and so on... I think you'll find this works better....

Reply With Quote
  #12  
Old 09-15-2002, 09:47 PM
Chr1s Chr1s is offline
Junior Guru Wannabe
 
Join Date: Sep 2002
Location: Canada
Posts: 35
If your running PHP4 you also might have to declare your $alpha variable:

PHP Code:
$alpha $HTTP_GET_VARS['alpha']; 

__________________
EZScripts - Quality PHP Scripts, Low Price (includes Custom Scripting on request.)
http://www.EZScripts.net/

Reply With Quote
  #13  
Old 09-15-2002, 09:48 PM
NickMahon NickMahon is offline
WHT Addict
 
Join Date: Apr 2002
Posts: 112
Thanks everyone, but this is already solved

__________________
- Nick Mahon
Vereor.com
Professional Web Development

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Web Host Rackspace Launches Private Beta for MySQL Cloud Database Web Hosting News 2011-12-01 21:09:51
Parallels Now Offering Licenses for CloudLinux OS Web Hosting News 2011-10-26 17:29:25
Web Hosting Software Firm CloudLinux Releases CloudLinux OS Version 6.1 Web Hosting News 2011-09-13 14:08:49
Web Host FireHost Partners with Cloud Security Firm Gazzang for Data Encryption Web Hosting News 2011-08-16 20:33:43
Gazzang Data Encrytion Solution Adds Support for CloudLinux Platform Web Hosting News 2011-06-30 18:54:10


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?