I want to display a table (in a web page) containing the data from my database, but only want to display the table with fields selected by the user. For example: I have a total of 6 fields in my table(name, email, age, location, gender, occupation). Let's say that the user on my page selects name, email and age. I want a table to be displayed containing all the row entries, but with only the specified fields.
This is what I've written so far but it doesn't seem to be working.
include("init.php");
$fields = $_POST["fields"];
$sorting = trim($_POST["sorting"]);
$filter = trim($_POST["filter"]);
$sql_query = sprintf("select * from applicants where %s order by %s", $filter, $sorting);
$query = mysql_query($sql_query);
$num_rows = mysql_numrows($query);
echo "<table border='1'>";
echo "<tr>";
foreach($fields as $field) {
$field = ucfirst($field);
echo "<th>$field</th>";
}
echo "</tr>";
for($i = 0; $i < $num_rows; $i++) {
echo "<tr>";
while($field = mysql_fetch_field($query)) {
if(in_array($field->name, $fields)) {
$data = mysql_result($query, $i, $col);
echo "<td>$data</td>";
} else {
echo "<td>false</td>";
}
}
echo "</tr>";
}
echo "</table>";