wooowooo Posted December 23, 2007 Share Posted December 23, 2007 Anyone know how I can modify the code below so you can choice weather to sort assendlingly or dessendingly? I have tried a few things such as adding "up" and "down" arrows but cannt seem to do it Im not all that good at php as you may have guessed! <code> <?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'); $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"; ?> </code> Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/82949-and-desending/ Share on other sites More sharing options...
corbin Posted December 23, 2007 Share Posted December 23, 2007 $query = "SELECT * FROM table ORDER BY $order"; To make it srt ASC it would simply be: $query = "SELECT * FROM table ORDER BY $order ASC"; And descending would simply be $query = "SELECT * FROM table ORDER BY $order DESC"; .... So... you could do something like: $ad = (isset($_GET['ad']) && $_GET['ad'] == 'desc') 'desc' : 'asc'; $query = "SELECT * FROM table ORDER BY $order $ad"; Quote Link to comment https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421881 Share on other sites More sharing options...
wooowooo Posted December 23, 2007 Author Share Posted December 23, 2007 Where abouts would that need to be added? Quote Link to comment https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421884 Share on other sites More sharing options...
wooowooo Posted December 23, 2007 Author Share Posted December 23, 2007 I have added this code in but I cnt make it work do I need to add "up" and "down" arrows? Could you let me know how/what to do? Thank u Quote Link to comment https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421894 Share on other sites More sharing options...
corbin Posted December 23, 2007 Share Posted December 23, 2007 You need to add ad=asc or ad=desc to the links.... It will default to asc if nothing is specified by the way... Quote Link to comment https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421898 Share on other sites More sharing options...
wooowooo Posted December 23, 2007 Author Share Posted December 23, 2007 How do I do this? Quote Link to comment https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421903 Share on other sites More sharing options...
corbin Posted December 23, 2007 Share Posted December 23, 2007 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"; } Should work. Quote Link to comment https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421905 Share on other sites More sharing options...
wooowooo Posted December 23, 2007 Author Share Posted December 23, 2007 mysql_data_seek ($result, 0); while ($row = mysql_fetch_assoc ($result)) { 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"; } foreach ($row as $column) { echo "<TD>$column</TD>\n"; } echo "</TR>\n"; } echo "</TABLE>\n"; ?> Is that correct because it still wont work Quote Link to comment https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421915 Share on other sites More sharing options...
corbin Posted December 23, 2007 Share Posted December 23, 2007 <?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"; exit; $orig_array = array('corbin', 'corbin', 'john', 'mark', 'mark', 'paul', 'paul', 'mary', 'mary', 'mary'); $count_array = array(); foreach($orig_array as $v) { (isset($count_array[$v])) ? $count_array[$v]++ : $count_array[$v] = 1; } print_r($count_array); exit; if (eregi('http:', $question)) { die ("Please enter question"); } if(!$email == "" && (!strstr($email,"@") || !strstr($email,"."))) { echo "<h2>Please enter valid email</h2>\n"; $badinput = "<h2>Feedback was NOT submitted</h2>\n"; echo $badinput; die ("Go back! ! "); } if(empty($firstname) || empty($email) || empty($question )) { echo "<h2>Please fill in all fields</h2>\n"; die ("Use back! ! "); } $showform = true; $error = ''; if($_POST) { //pretend $question, $email and $firstname are defined if(eregi('http:', $question)) { $error = 'Please enter question'; } elseif(empty($email) || empty($firstname) || empty($question)) { $error = 'Please fill in all fields.'; } elseif(!strstr($email, '@') || !strstr($email, '.')) { $error = 'Please enter a valid email.'; } else { //success!!!! $showform = false; } } else { $question = $email = $firstname = ''; } if($showform != false) { if(!empty($error)) { echo $error; } echo <<<HERE <!-- form stuff here --> HERE; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421932 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.