this has me stumped.
I've got this page http://saywhatagain.co.uk/customers.php
What its currently doing is listing a table of results twice, and in the first table listing them in ascending order, and in the second listing them by descending, by whichever column heading is selected.
What i want it to do is have ONE table of results, with two headings for each column (ascending/descending), and then whichever you select it sorts the results etc.
Here's the code I have at the moment (just the two blocks of php for each table):
Anyone got any ideas?
I've got this page http://saywhatagain.co.uk/customers.php
What its currently doing is listing a table of results twice, and in the first table listing them in ascending order, and in the second listing them by descending, by whichever column heading is selected.
What i want it to do is have ONE table of results, with two headings for each column (ascending/descending), and then whichever you select it sorts the results etc.
Here's the code I have at the moment (just the two blocks of php for each table):
Code:
<?php
$default_sort = 'CustomerID'; // default sort order is by the first column - customerID
$allowed_order = array ('CustomerID', 'Name','Address','Postcode','Telephone','Email'); //array of all column headings
if (!isset ($_GET['order']) ||
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
$query = "SELECT * FROM customers ORDER BY $order"; // query which selects all data from the table and orders it by the selected column
$result = mysql_query($query);
// gets the first row and starts the table
$row = mysql_fetch_assoc ($result);
echo "<table cellpadding='5' cellspacing='5'>\n";
echo "<tr>\n";
foreach ($row as $heading=>$column) {
//if the heading is in our allowed_order array, its hyperlinked so that the table can be ordered by this 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";
//display the data
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";
?>
</table>
<table cellpadding="5" cellspacing="5">
<tr><td>
<b>Descending Order</b></td></tr>
<tr><td>
<?php
$default_sort1 = 'CustomerID'; // default sort order is by the first column - customerID
$allowed_order1 = array ('CustomerID', 'Name','Address','Postcode','Telephone','Email'); //array of all column headings
if (!isset ($_GET['order']) ||
!in_array ($_GET['order'], $allowed_order1)) {
$order1 = $default_sort1;
} else {
$order1 = $_GET['order'];
}
$query1 = "SELECT * FROM customers ORDER BY $order1 DESC"; // query which selects all data from the table and orders it by the selected column
$result1 = mysql_query($query1);
// gets the first row and starts the table
$row = mysql_fetch_assoc ($result1);
echo "<table cellpadding='5' cellspacing='5'>\n";
echo "<tr>\n";
foreach ($row as $heading=>$column) {
//if the heading is in our allowed_order array, its hyperlinked so that the table can be ordered by this column
echo "<td><b>";
if (in_array ($heading, $allowed_order1)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
} else {
echo $heading;
}
echo "</b></td>\n";
}
echo "</tr>\n";
//display the data
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result1)) {
echo "<tr>\n";
foreach ($row as $column) {
echo "<td>$column</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
Anyone got any ideas?