我有一个基本的select *来自我的页面上显示我的表格。我希望该选项只显示某人可从页面中选择的列。这可能吗?
它是一个包含16列的基本表。不是每次都有人想要显示每一列。假设他们只想要显示的列a-e。什么是让他们选择这些列并让它构建查询的最佳方式。
要么过滤掉表头中的dropwons,要么过滤掉。就像你在Excel中添加了一个过滤器一样。
答案 0 :(得分:3)
另一种方法是将你的'数据表'输出到由JAVASCRIPT修改的东西,使用像jQuery这样的库,你可以使用一个'table plugin'来隐藏/显示列,并重新排序为你请。否则,您将通过后端SQL查询更改进行此排序。
JAVASCRIPT方法让用户可以通过1种格式大量提取隐藏/显示/排序数据。
以下是一些实现此JS方法的插件示例:
享受!
答案 1 :(得分:1)
在不了解您的代码或数据库的任何内容的情况下,一个简单的方法是:
让用户指定列(例如,从列表中选择它们),然后在代码中构造SQL语句,将所选列连接到SELECT列表而不是指定*,然后执行SQL语句。
答案 2 :(得分:0)
这完全取决于你对show的意思。
您可以在select中添加列而不是*,这样只会在返回的列表中提供所选列。
如果这不是您的意思,您需要添加更多详细信息,如问题评论中所述。
答案 3 :(得分:0)
你去(根据需要进行调整):
function show_column_selectors($available, $chosen, $labels){
echo <<<HTML
<form method="POST" action="#">
HTML;
foreach($available as $column){
$id = 'show_'.$column;
$checked = in_array($column, $chosen) ? 'checked="checked"' : '';
// See how I write the input elements with the same name,
// ending said name with []?
// This will make PHP join all their values in an array.
// (see main() function below)
echo <<<HTML
<label for="{$id}">
<input id="{$id}" name="columns_to_show[]"
type="checkbox" {$checked} value="{$column}">
{$labels[$column]}
</label>
HTML;
}
echo <<<HTML
<input type="submit" value="Go!">
</form>
HTML;
}
function do_query($cols){
// This is boilerplate I hacked just to test this out. Ignore it
$mysqli = mysqli_connect('server', 'username', 'password', 'database');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '.$mysqli->connect_error);
}
// Here you build your query from the array $cols,
// which contains the columns chosen by the user.
// See below, in the main() function, how it is obtained.
$query = "SELECT `".implode("`, `", $cols)."` FROM `table`;";
// More boilerplate follows
$result = $mysqli->query($query, MYSQLI_USE_RESULT);
$rows = array();
if($result){
while(($row = $result->fetch_assoc())){
$rows []= $row;
}
}
mysqli_free_result($result);
$mysqli->close();
return $rows;
}
function show_result($columns, $rows){
echo "<table><tr>\n";
foreach($columns as $column){
echo "<th>{$column}</th>\n";
}
echo "<tr>\n";
foreach($rows as $row){
echo "<tr>\n";
foreach($columns as $column){
echo "<td>{$row[$column]}</td>";
}
echo "</tr>\n";
}
echo "</table>";
}
function main(){
// This are the column names as seen by the database
$AVAILABLE_COLUMNS = array(
'col1', 'col2', 'col3', 'col4'
);
// And these map between the above
// and what you want the user to see
$LABELS = array(
'col1' => 'Column 1',
'col2' => 'Column 2',
'col3' => 'Column 3',
'col4' => 'Column 4',
);
// This is the important part, where you obtain the array
// with user's chosen columns.
// NOTE: same name as the input elements, but with no []
if(empty($_POST['columns_to_show'])){
$chosen_columns = $AVAILABLE_COLUMNS;
}
else{
$chosen_columns = array_intersect($_POST['columns_to_show'], $AVAILABLE_COLUMNS);
}
show_column_selectors($AVAILABLE_COLUMNS, $chosen_columns, $LABELS);
$rows = do_query($chosen_columns);
show_result($chosen_columns, $rows);
}
main();
?><html>
<head><title>Test</title></head>
<body>
</body>
</html>
function show_column_selectors($available, $chosen, $labels){
echo <<<HTML
<form method="POST" action="#">
HTML;
foreach($available as $column){
$id = 'show_'.$column;
$checked = in_array($column, $chosen) ? 'checked="checked"' : '';
// See how I write the input elements with the same name,
// ending said name with []?
// This will make PHP join all their values in an array.
// (see main() function below)
echo <<<HTML
<label for="{$id}">
<input id="{$id}" name="columns_to_show[]"
type="checkbox" {$checked} value="{$column}">
{$labels[$column]}
</label>
HTML;
}
echo <<<HTML
<input type="submit" value="Go!">
</form>
HTML;
}
function do_query($cols){
// This is boilerplate I hacked just to test this out. Ignore it
$mysqli = mysqli_connect('server', 'username', 'password', 'database');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '.$mysqli->connect_error);
}
// Here you build your query from the array $cols,
// which contains the columns chosen by the user.
// See below, in the main() function, how it is obtained.
$query = "SELECT `".implode("`, `", $cols)."` FROM `table`;";
// More boilerplate follows
$result = $mysqli->query($query, MYSQLI_USE_RESULT);
$rows = array();
if($result){
while(($row = $result->fetch_assoc())){
$rows []= $row;
}
}
mysqli_free_result($result);
$mysqli->close();
return $rows;
}
function show_result($columns, $rows){
echo "<table><tr>\n";
foreach($columns as $column){
echo "<th>{$column}</th>\n";
}
echo "<tr>\n";
foreach($rows as $row){
echo "<tr>\n";
foreach($columns as $column){
echo "<td>{$row[$column]}</td>";
}
echo "</tr>\n";
}
echo "</table>";
}
function main(){
// This are the column names as seen by the database
$AVAILABLE_COLUMNS = array(
'col1', 'col2', 'col3', 'col4'
);
// And these map between the above
// and what you want the user to see
$LABELS = array(
'col1' => 'Column 1',
'col2' => 'Column 2',
'col3' => 'Column 3',
'col4' => 'Column 4',
);
// This is the important part, where you obtain the array
// with user's chosen columns.
// NOTE: same name as the input elements, but with no []
if(empty($_POST['columns_to_show'])){
$chosen_columns = $AVAILABLE_COLUMNS;
}
else{
$chosen_columns = array_intersect($_POST['columns_to_show'], $AVAILABLE_COLUMNS);
}
show_column_selectors($AVAILABLE_COLUMNS, $chosen_columns, $LABELS);
$rows = do_query($chosen_columns);
show_result($chosen_columns, $rows);
}
main();
?><html>
<head><title>Test</title></head>
<body>
</body>
</html>
答案 4 :(得分:0)
我强烈建议您使用YUI Datatable。我经常将它用于类似的应用程序。您可以查询服务器以明确显示哪些列,或者仅返回整个集合(如果不总是需要数据,则效率低下。)