wooowooo Posted December 28, 2007 Share Posted December 28, 2007 Hi Below is the code I have developed to pull recorts from my customer database and sort them assendingly by the user clicking onto the column heading however I would like to make it so they can choose weather to view them assendingly or decendingly - I know that I should add a link and use the GET method so the title and weather its assending or desending but I dont know how to implement it into my code, could anyone show me? Many thanks, im new to php and not all that good! <?php $default_sort = 'CustomerID'; $allowed_order = array ('CustomerID','Name', 'Address','Postcode','Telephone','Email'); if (!isset ($_GET['order']) || !in_array ($_GET['order'], $allowed_order)) { $order = $default_sort; } else { $order = $_GET['order']; } // connect to db $query = "SELECT * FROM table ORDER BY $order"; $result = mysql_query ($query); $numrows = mysql_num_rows($result); if ($numrows == 0) { echo "No data to display!"; exit; } $row = mysql_fetch_assoc ($result); echo "<TABLE border=1>Click on a column heading to sort the column assendingly <p> \n"; echo "<TR>\n"; foreach ($row as $heading=>$column) { echo "<TD><b>"; if (in_array ($heading, $allowed_order)) { echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>"; } else { echo $heading; } echo "</b></TD>\n"; } echo "</TR>\n"; mysql_data_seek ($result, 0); while ($row = mysql_fetch_assoc ($result)) { echo "<TR>\n"; foreach ($row as $column) { echo "<TD>$column</TD>\n"; } echo "</TR>\n"; } echo "</TABLE>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83429-sorting-columns/ Share on other sites More sharing options...
teng84 Posted December 28, 2007 Share Posted December 28, 2007 <?php // url format index.php?sort=1&by=Name $default_sort = 'CustomerID'; $allowed_order = array ('CustomerID','Name', 'Address','Postcode','Telephone','Email'); if (in_array($_GET['by'], $allowed_order)){ $order =($_GET['sort']==1)?'asc':'desc'; $val = $_GET['by'].' '.$order; } else{ $val = $default_sort.' desc'; } $query = "SELECT * FROM table ORDER BY $val"; $result = mysql_query ($query); $numrows = mysql_num_rows($result); if ($numrows == 0) { echo "No data to display!"; exit; } $row = mysql_fetch_assoc ($result); echo "<TABLE border=1>Click on a column heading to sort the column assendingly <p> \n"; echo "<TR>\n"; foreach ($row as $heading=>$column) { echo "<TD><b>"; if (in_array ($heading, $allowed_order)) { echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>"; } else { echo $heading; } echo "</b></TD>\n"; } echo "</TR>\n"; mysql_data_seek ($result, 0); while ($row = mysql_fetch_assoc ($result)) { echo "<TR>\n"; foreach ($row as $column) { echo "<TD>$column</TD>\n"; } echo "</TR>\n"; } echo "</TABLE>\n"; ?> maybe Quote Link to comment https://forums.phpfreaks.com/topic/83429-sorting-columns/#findComment-424462 Share on other sites More sharing options...
corbin Posted December 28, 2007 Share Posted December 28, 2007 I was going to ask you if my post the other day works (http://www.phpfreaks.com/forums/index.php/topic,173931.msg769601.htm), but then I realized that when I posted that the other day, I had been working in one file for multiple scripts with exit statements, and I managed to paste the entire thing x.x. Try that script above the first exit. Ehhh just to clarifiy, I'll repost it... <?php $default_sort = 'CustomerID'; $allowed_order = array ('CustomerID','Name', 'Address','Postcode','Telephone','Email'); if (!isset ($_GET['order']) || !in_array ($_GET['order'], $allowed_order)) { $order = $default_sort; } else { $order = $_GET['order']; } mysql_connect ('localhost','user','pass'); mysql_select_db ('database'); $ad = (isset($_GET['ad']) && $_GET['ad'] == 'desc') 'desc' : 'asc'; $query = "SELECT * FROM table ORDER BY $order $ad"; $result = mysql_query ($query); $numrows = mysql_num_rows($result); if ($numrows == 0) { echo "No data to display!"; exit; } $row = mysql_fetch_assoc ($result); echo "<TABLE border=1>Click on a column heading to sort the column assendingly <p> \n"; echo "<TR>\n"; foreach ($row as $heading=>$column) { echo "<TD>"; if (in_array ($heading, $allowed_order)) { $tad = ($ad == 'asc' && $order == $heading) ? 'desc' : 'asc'; echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&ad=$tad\">$heading[/url]"; } else { echo $heading; } echo "</TD>\n"; } echo "</TR>\n"; mysql_data_seek ($result, 0); while ($row = mysql_fetch_assoc ($result)) { echo "<TR>\n"; foreach ($row as $column) { echo "<TD>$column</TD>\n"; } echo "</TR>\n"; } echo "</TABLE>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/83429-sorting-columns/#findComment-424471 Share on other sites More sharing options...
wooowooo Posted December 28, 2007 Author Share Posted December 28, 2007 corbin's code comes up with a T_CONSTANT_ENCAPSED_STRING on line 10 I cannt find the problem for toffee though! Any help? Quote Link to comment https://forums.phpfreaks.com/topic/83429-sorting-columns/#findComment-424641 Share on other sites More sharing options...
wooowooo Posted December 28, 2007 Author Share Posted December 28, 2007 Ment line 17 :s Quote Link to comment https://forums.phpfreaks.com/topic/83429-sorting-columns/#findComment-424642 Share on other sites More sharing options...
wooowooo Posted December 28, 2007 Author Share Posted December 28, 2007 Sorted that error Any one know why theres only the first letters of the column headings displaying though now? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/83429-sorting-columns/#findComment-424659 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.