I have a form which allows the user to set what the sort the data by in the table and if it increase or decreasing order
http://damiensplace.co.uk/test/requestTableDisplay.htm
However I want the user to choose to exclude a field (Age Grade) form set the variable but been racking my brain on how to do this but only way I come up with is not neat as I basically need to write two lots of code am I missing a trick?
<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = '****';
$db_user = '***';
$db_pwd = '***';
$sortOn = $_POST['SortOn'];
$sortIn = $_POST['SortIn'];
$database = '****';
$table = '***';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table} order by {$sortOn} {$sortIn}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>
If someone can point me in the right direction I would be most grateful
Pretty simple. Instead of running this query:
SELECT * FROM {$table} order by {$sortOn} {$sortIn}run this one:
"SELECT hats, cats, margarine FROM {$table} order by {$sortOn} {$sortIn}…where
hats,catsandmargarineare to be replaced by fields in your table. You can specify any number of fields in this way, even down to grabbing only a single field.