sort by asc and desc

I want to be able to sort my query tabe by asc then when I click on the link I want it to sort by desc can someone help me with the code. Here is what I have.

<?php mysql_connect("localhost", "my_username", "my_password") or die(mysql_error()); mysql_select_db("websited_Contacts") or die(mysql_error()); if ($_REQUEST['action'] == "del") { $id = intval($_REQUEST['id']); mysql_query("DELETE FROM Contacts WHERE id='$id'"); echo " Deleted {$_REQUEST['acc']} From The contacts List

"; } if ($_REQUEST['action'] == "sort") { $col=$_REQUEST['col']; $result = mysql_query("SELECT * FROM `Contacts` ORDER BY `Contacts`.`$col` ASC") or die(mysql_error()); } else { $col=$_REQUEST['col']; $result = mysql_query("SELECT * FROM `Contacts` ORDER BY `Contacts`.`$col` DESC")or die(mysql_error()); } echo "

"; echo ""; while($row = mysql_fetch_array( $result )) { echo ""; } echo "
"; echo "Company Name"; echo " "; echo "Full Business Address"; echo " "; echo "Current Website Address"; echo " "; echo "Phone Humber"; echo " "; echo "Email Address"; echo " "; echo "Describe Your Business Needs"; echo " "; echo "Remove?"; echo "
"; echo $row['field_1']; echo " "; echo $row['field_2']; echo " "; echo $row['field_3']; echo " "; echo $row['field_4']; echo " "; echo $row['field_5']; echo " "; echo $row['field_6']; echo " "; echo "delete"; echo "
"; ?>

Looks like you just need to swap ASC and DESC in queries in your code:
[php]if ($_REQUEST[‘action’] == “sort”) {
$col=$_REQUEST[‘col’];
$result = mysql_query(“SELECT * FROM Contacts ORDER BY Contacts.$col DESC”) or die(mysql_error());
} else {
$col=$_REQUEST[‘col’];
$result = mysql_query(“SELECT * FROM Contacts ORDER BY Contacts.$col ASC”)or die(mysql_error());

}[/php]

phphelp I get the following error when I do that.

Unknown column ‘Contacts.’ in ‘order clause’

I have tried a lot of versions of this but I don’t think that I have ever seen that error before. What does that mean?

Try this query:

"SELECT * FROM Contacts ORDER BY $col ASC"

If this not work either, try to debug - echo sql query and see if $col is set correctly

I am still getting this error

Unknown column ‘Contacts.’ in ‘order clause’

I don’t understand it the aray worked before but maybe something is wrong with that? Maybe I deleted something that was supposed to be there.

ok I messed up somehow it was in there twice so I deleted one and now i get a new error

Parse error: syntax error, unexpected $end in /home/websited/public_html/tmp/persistent/form233222/admin/admin.php on line 58

I got nothing on that one.

Code now looks like this

<?php mysql_connect("localhost", "my_user", "my_password") or die(mysql_error()); mysql_select_db("websited_Contacts") or die(mysql_error()); if ($_REQUEST['action'] == "del") { $id = intval($_REQUEST['id']); mysql_query("DELETE FROM Contacts WHERE id='$id'"); echo " Deleted {$_REQUEST['acc']} From The contacts List

"; if ($_REQUEST['action'] == "sort") { $col=$_REQUEST['col']; $result = mysql_query("SELECT * FROM Contacts ORDER BY $col DESC") or die(mysql_error()); } else { $col=$_REQUEST['col']; $result = mysql_query("SELECT * FROM Contacts ORDER BY $col ASC")or die(mysql_error()); } echo "

"; echo ""; while($row = mysql_fetch_array( $result )) { echo ""; } echo "
"; echo "Company Name"; echo " "; echo "Full Business Address"; echo " "; echo "Current Website Address"; echo " "; echo "Phone Humber"; echo " "; echo "Email Address"; echo " "; echo "Describe Your Business Needs"; echo " "; echo "Remove?"; echo "
"; echo $row['field_1']; echo " "; echo $row['field_2']; echo " "; echo $row['field_3']; echo " "; echo $row['field_4']; echo " "; echo $row['field_5']; echo " "; echo $row['field_6']; echo " "; echo "delete"; echo "
"; ?>

You’re missing closing } immediately after this piece of code:
[php]if ($_REQUEST[‘action’] == “del”) {
$id = intval($_REQUEST[‘id’]);
mysql_query(“DELETE FROM Contacts WHERE id=’$id’”);
echo " Deleted {$_REQUEST[‘acc’]} From The contacts List

";[/php]

yup just found that now getting a strange error. I took out the if statement that we are working on and everything else works. However even if I isolate this if statement I still get this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘ASC’ at line 1

never seen that before. no idea what it means

Ok I got rid of that crazy error and now there are no errors. However it still dosen’t switch back and forth. When I click on the link it only sorts by asc and if I change the query then it goes back to the other way so maybe it checks the code and says it’s true so it only looks at the if statement and not the else. I think that it has to have more in the if statement than it does but not sure what or how to write it. This is current version of the code.

<?php mysql_connect("localhost", "My_username", "My_password") or die(mysql_error()); mysql_select_db("websited_Contacts") or die(mysql_error()); if ($_REQUEST['action'] == "del") { $id = intval($_REQUEST['id']); mysql_query("DELETE FROM Contacts WHERE id='$id'"); echo " Deleted {$_REQUEST['acc']} From The contacts List

"; } if ($_REQUEST['action'] == "sort") { $col=$_REQUEST['col']; $result = mysql_query("SELECT * FROM `Contacts` ORDER BY `Contacts`.`field_1` DESC") or die(mysql_error()); } else { $col=$_REQUEST['col']; $result = mysql_query("SELECT * FROM `Contacts` ORDER BY `Contacts`.`field_1` ASC")or die(mysql_error()); } echo "

"; echo ""; while($row = mysql_fetch_array( $result )) { echo ""; } echo "
"; echo "Company Name"; echo " "; echo "Full Business Address"; echo " "; echo "Current Website Address"; echo " "; echo "Phone Humber"; echo " "; echo "Email Address"; echo " "; echo "Describe Your Business Needs"; echo " "; echo "Remove?"; echo "
"; echo $row['field_1']; echo " "; echo $row['field_2']; echo " "; echo $row['field_3']; echo " "; echo $row['field_4']; echo " "; echo $row['field_5']; echo " "; echo $row['field_6']; echo " "; echo "delete"; echo "
"; ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service