【发布时间】:2017-06-07 08:55:30
【问题描述】:
我一直在尝试为此找到一种简单的方法。 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>";
////////////////////////////////////////////////////////////////////////////////////////////////////////////
【问题讨论】:
-
我想你的意思是
array_push($all_property, $property); //save those to array而不是array_push($all_property, $property->name); //save those to array,对吧? -
@timo 如果我这样做就不行了
-
哦,对了!对不起穆阿斯。是的,我已经测试了您的解决方案,它工作正常。那么,您需要使用表格名称填充下拉菜单,然后当用户选择表格时,在页面中显示该表格的内容?
-
@timo 就是这样:)
-
有两种方法可以做到这一点:当用户在下拉菜单中选择表格时刷新页面或向服务器发出 AJAX 请求以从表格中获取信息并填充表格。您更愿意实施哪种解决方案?
标签: php arrays mysqli html-table