从SQL搜索表并在页面上显示其数据

时间:2017-06-07 08:55:30

标签: php arrays mysqli html-table

我一直试图找到一个简单的方法。 mysql中所有表的搜索(下拉菜单),当我单击要在页面上显示的表时显示其内容。而不是只显示页面上的每个表格,我认为它可以更容易?任何帮助,将不胜感激!
我的代码到目前为止:

<?php
$host    = "localhost";
$user    = "heijsdb_user";
$pass    = "maus";
$db_name = "heijsdb";

//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);

//test if connection failed
if(mysqli_connect_errno()){
    die("connection failed: "
        . mysqli_connect_error()
        . " (" . mysqli_connect_errno()
        . ")");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo "borsten HFP controle";
$result = mysqli_query($connection,"SELECT * FROM borstenHFPcontrole");
$all_property = array();  //declare an array for saving property

//showing property
echo '<table class="data-table w3-table-all" border="2px">
        <tr class="data-heading">';  //initialize table tag
while ($property = mysqli_fetch_field($result)) {
    echo '<td>' . $property->name . '</td>';  //get field name for header
    array_push($all_property, $property->name);  //save those to array
}
echo '</tr>'; //end tr tag

//showing all data
while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    foreach ($all_property as $item) {
        echo '<td>' . $row[$item] . '</td>'; //get items using property value
    }
    echo '</tr>';
}
echo "</table>";
////////////////////////////////////////////////////////////////////////////////////////////////////////////

1 个答案:

答案 0 :(得分:1)

这几乎就是这个想法,您可以从这里开始并根据您的解决方案进行调整。对不起,我用自己的方式,在嵌入HTML时我更喜欢PHP模板样式。 ;)

$host    = "localhost";
$user    = "heijsdb_user";
$pass    = "maus";
$db_name = "heijsdb";

//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);

//test if connection failed
if(mysqli_connect_errno()){
    die("connection failed: "
        . mysqli_connect_error()
        . " (" . mysqli_connect_errno()
        . ")");
}

//check if the form was submitted
$table = filter_input(INPUT_POST, 'table', FILTER_SANITIZE_STRING);

?>
<html>
<head>
    <title>showing table content on user action</title>
</head>
<body>
    <div>

        <form id="form-menu" method="post">
            <label for="select-menu">Choose a table</label>
            <select id="select-menu" name="table">
                <option></option>
                <?php
                $result = mysqli_query($connection,"SELECT table_name FROM information_schema.tables where table_schema='test'"); // <-- the table_schema field here is your database name, change 'test' for yours
                while ($row = mysqli_fetch_array($result)) : $selected = $row['table_name'] == $table ? 'selected' : ''; ?>
                    <option value="<?php echo $row['table_name'] ; ?>" <?php echo $selected; ?>><?php echo $row['table_name'] ; ?></option>
                <?php endwhile; ?>
            </select>
        </form>

        <hr>

        <div>
            <?php if (empty($table)) : ?>
                <h3>Please select a table to show its content</h3>
            <?php else : ?>
                <h3>Content for the table `<?php echo $table; ?>`</h3>
                <?php
                $result = mysqli_query($connection,"SELECT * FROM `{$table}`");
                $all_property = [];  //declare an array for saving property
                ?>
                <!-- showing property -->
                <table class="data-table w3-table-all" border="2px">
                    <tr class="data-heading"> <!-- initialize table tag -->
                        <?php while ($property = mysqli_fetch_field($result)) : ?>
                        <td><?php echo $property->name; ?></td> <!-- get field name for header -->
                        <?php $all_property[] = $property->name; //save those to array ?>
                        <?php endwhile; ?>
                    </tr> <!-- end tr tag -->

                    <!-- showing all data -->
                    <?php while ($row = mysqli_fetch_array($result)) : ?>
                    <tr>
                        <?php foreach ($all_property as $item) : ?>
                            <td><?php echo $row[$item]; ?></td> <!-- get items using property value -->
                        <?php endforeach; ?>
                    </tr>
                    <?php endwhile; ?>
                </table>
            <?php endif; ?>
        </div>

    </div>

    <script>
        document.getElementById('select-menu').addEventListener('change', function() {
            document.getElementById('form-menu').submit();
        });
    </script>
</body>
</html>

方便的链接:
- Get table names using SELECT statement in MySQL
- Examples of how to do query, style, dom, ajax, event etc like jQuery with plain javascript.

希望这会有所帮助:)