TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-07-2005, 05:03 PM   #1 (permalink)
The Wanderer
 
Join Date: May 2005
Location: Maine|USA
Posts: 17
Thanks: 0
Ogden2k is on a distinguished road
Default Sort order problem

I have a little game database, I am trying to sort the data (per a user click) by title, or by systme or by whatever the user specifies. Here's the code that I have, it's not sorting:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <title>Games Database</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
    <link rel="stylesheet" type="text/css" href="http://misc.techiehq.net/stuff/css/games.css" /> 
</head> 
<body> 
<div class="body_fore"> 

<div class="title">
Games:
</div> 

<table class="table_head"> 
<tr> 
     <td width="85">System <a href="index.php?order=system">^</a> v</td> 
     <td width="200">Title <a href="index.php?order=title">^</a> v</td> 
     <td width="110">Developer ^ v</td> 
     <td width="140">Publisher ^ v</td> 
     <td width="140">Year ^ v</td> 
     <td width="140">Purchased ^ v</td> 
     <td width="88">Genre ^ v</td> 
     <td width="120">Serial ^ v</td> 
</tr> 
</table>
 
<?php
// Connect to the DB server 
$dbcnx = @mysql_connect('localhost''x''x'); 
    if (!
$dbcnx) { 
    exit(
'<p>Unable to connect to the ' 
    
'database server at this time.</p>'); 

// Select the binarydr_stuff DB 
    
if (!@mysql_select_db('binarydr_stuff')) { 
    exit(
'<p>Unable to locate the stuff ' 
    
'database at this time.</p>'); 
}
// Request the text of all the info 
$result = @mysql_query('SELECT * FROM games'); 

if (!
$result) { 
    exit(
'<p>Error performing query: ' mysql_error() . '</p>'); 
}
// Display each game
while ($row mysql_fetch_array($result)) { 
echo 

<table class="cells"> 
<tr> 
     <td width="85">' 
$row['system'] . '</td> 
     <td width="200">' 
$row['title'] . '</td> 
     <td width="110">' 
$row['developer'] . '</td> 
     <td width="140">' 
$row['publisher'] . '</td> 
     <td width="140">' 
$row['year'] . '</td> 
     <td width="140">' 
$row['purchased'] . '</td> 
     <td width="88">' 
$row['genre'] .'</td> 
     <td width="120">' 
$row['serial'] .'</td> 
</tr> 
</table> 
'

}
$order = isset($_GET['order']) ? mysql_escape_string($_GET['order']) : 'system'// Request the text of all the info
    
$result1 = @mysql_query(
    
"SELECT system,title,developer,publisher, year,purchased,genre,serial FROM games 
    ORDER BY 
$order DESC"
);
// When clicked, this link will load this page with the hook sub form displayed 
echo '<div class="button_area"> 
<p>[ <a href="add.php">Add a Game</a> | <a href="../search/index.php">Search</a> | <a href="../">Home</a> | <a href="' 
$_SERVER['PHP_SELF'] . '">Refresh</a> ]</p></div> 
</div> 
</div>'
;
?> 
</body> 
</html>
__________________
TechieHQ
Send a message via AIM to Ogden2k
Ogden2k is offline  
Reply With Quote
Old 06-07-2005, 05:10 PM   #2 (permalink)
The Acquainted
 
Join Date: May 2005
Posts: 106
Thanks: 0
jaswinder_rana is on a distinguished road
Default

TRY changing this line (which is i think line number 42)
PHP Code:
$result = @mysql_query('SELECT * FROM games'); 
with (which are 64-68)
PHP Code:
$order = isset($_GET['order']) ? mysql_escape_string($_GET['order']) : 'system'// Request the text of all the info
    
$result1 = @mysql_query(
    
"SELECT system,title,developer,publisher, year,purchased,genre,serial FROM games
    ORDER BY 
$order DESC"
); 

Line numbers can be wrong. so, take care

i think all you did is put the query at wrong place. just need to exchange it


Suggestion
don't order by the input from url. what if i changed the url manually and changed to Something_wrong. this will give error and your query won't execute

try, using switch statement as
PHP Code:
  $order '';
  switch(
$_GET['order'])
  {
    case 
'order':
     
$order 'order';
     break;
    case 
'system':
     
$order 'system';
     break;
    default:
     
$order 'order';
     break;
  } 
this will take the default one if user changes the link manually. people (like me) try to do it just to see how the code handles it??
__________________
---------------------------
Errors = Improved Programming.
Portfolio
Send a message via MSN to jaswinder_rana
jaswinder_rana is offline  
Reply With Quote
Old 06-07-2005, 05:34 PM   #3 (permalink)
The Wanderer
 
Join Date: May 2005
Location: Maine|USA
Posts: 17
Thanks: 0
Ogden2k is on a distinguished road
Default

I took your suggestion, that's a better way of going about it. I also fixed the default view / sorting too. Thank you for the help.
__________________
TechieHQ
Send a message via AIM to Ogden2k
Ogden2k is offline  
Reply With Quote
Old 06-07-2005, 05:35 PM   #4 (permalink)
The Acquainted
 
Join Date: May 2005
Posts: 106
Thanks: 0
jaswinder_rana is on a distinguished road
Default

glad to help you
__________________
---------------------------
Errors = Improved Programming.
Portfolio
Send a message via MSN to jaswinder_rana
jaswinder_rana is offline  
Reply With Quote
Old 06-07-2005, 06:52 PM   #5 (permalink)
The Wanderer
 
Join Date: May 2005
Location: Maine|USA
Posts: 17
Thanks: 0
Ogden2k is on a distinguished road
Default

Hmf, I have been trying to get this resolved but it doesn't seem to work... I have created a link for them to be sorted ASC which works, but DESC will not work.

Tried setting up another case and result array but it did not work. Any pointers?
__________________
TechieHQ
Send a message via AIM to Ogden2k
Ogden2k is offline  
Reply With Quote
Old 06-07-2005, 07:08 PM   #6 (permalink)
The Acquainted
 
Join Date: May 2005
Posts: 106
Thanks: 0
jaswinder_rana is on a distinguished road
Default

Using 2 different techniques (ASC & DESC) is not impossible, but its going to take a lot of tihinking

for example

page.php?order=name&method=asc
page.php?order=phone&method=asc
page.php?order=postal&method=asc

say, i have above 3 links.so, for sorting you already got it and ASC is the default. now, if you want to do desc too, then when you choose the order you also need to change the method to DESC for next time.
PHP Code:
switch($_GET['order'])
{
 case 
name:
  
$order 'name';
  
$method = ($_GET['method'] == 'ASC')?'DESC:'ASC';
}

$query = "select * from table order by $order $method"; 
So, for next time now you have to change THAT PARTICULAR link to DESC, so when next time you click it, it also orders but this time it orders by DESC


i know its not clear. but, i couldn't find an example to support it
__________________
---------------------------
Errors = Improved Programming.
Portfolio
Send a message via MSN to jaswinder_rana
jaswinder_rana is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL problem Ogden2k Absolute Beginners 3 06-27-2005 10:06 PM
Welcome to talkPHP.com, please be sure to register in order to make full use of the hostingonline Feedback 3 04-15-2005 01:04 AM


All times are GMT. The time now is 04:07 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design