Javascript 若查询中并没有返回数据,则从html表中隐藏列
我有这样一个数组:Javascript 若查询中并没有返回数据,则从html表中隐藏列,javascript,php,jquery,Javascript,Php,Jquery,我有这样一个数组: $values = array(); 连同MySQL查询: $query = "SELECT $columns FROM Prices WHERE `key` LIKE '$rows' AND rate LIKE '$rate'"; if($results = $db->query($query)) { if($results->num_rows) { while($row = $results->fetch_object()
$values = array();
连同MySQL查询:
$query = "SELECT $columns FROM Prices WHERE `key` LIKE '$rows' AND rate LIKE '$rate'";
if($results = $db->query($query)) {
if($results->num_rows) {
while($row = $results->fetch_object()) {
$values[] = $row;
}
$results->free();
}
我使用以下代码打印出了表:我删除了一些表列
<?php
if(!count($values)) {
echo '<p>No results found for your current search. Use the inputs to the left to change the filters.</p>';
} else {
?>
<table>
<thead>
<tr>
<th>Location</th>
<th>City</th>
</tr>
</thead>
<tbody>
<?php
foreach ($values as $v) {
?>
<tr>
<td> <?php echo $v->Location; ?> </td>
<td> <?php echo $v->City; ?> </td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
而且会变成
| col 3 |
=========
| 33 |
| 32 |
或
将成为
| col 4 |
=========
| 65 |
| 25 |
我的问题是:如何隐藏空列
注:表格数据由用户通过输入表单填充
我很乐意通过css、php或JS实现这一点,假设返回的对象具有键的列名,首先循环第一行值以获得列名并写入TH标记,然后循环每行并写出值
// mocking up values for example
$values = array( (object) array("col_1" => 72, "col_3" => 44, "col_4" => 16), (object) array("col_1" => 51, "col_3" => 87, "col_4" => 34));
// error message if no results
if(!count($values)) {
echo '<p>No results found for your current search. Use the inputs to the left to change the filters.</p>';
// display results
} else {
?>
<table border="1">
<thead>
<tr>
<?php foreach ($values[0] as $k => $c) { /* loop first row to get column names */ ?>
<th> <?php echo $k; ?> </th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach ($values as $v) { /* loop rows */ ?>
<tr>
<?php foreach ($v as $k => $c) { /* loop columns */ ?>
<td> <?php echo $c; ?> </td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
答案可能重复:那么在第一种情况下,您只希望表包含第3列?第二个,只有可乐?我想如果你能举个例子说明你在寻找什么,这会有帮助。这里的答案对你有帮助吗?看看这些答案,再仔细想想,似乎最好的选择是将表头添加到数据库中,从查询返回这些表头,即仅搜索列。唯一的问题是,我不确定如何使顶行具有th标记而不是tr。有什么想法吗?-此外,我还试图实现@islanddave链接,但我没有luckand是的@bloodyKnucklesThanks,工作起来像个梦。我还使用了javascript版本。问题是哪一种实施方式更好。使用php版本,我必须将MySQL中的表值从小int更改为varchars以存储表列名,对于JS版本,用户必须启用javascript/capabilites。你有什么意见?需要考虑的因素很多。我想问自己的问题是:网站的其他部分需要Javascript吗?我的最终用户可能没有可用的JS吗?将此处理卸载到浏览器是否对我有利?哪一个更易于维护、可扩展。。。马上想到。感谢您的接受。我确实注意到您的代码有一个问题,因为我必须在数据库中生成第一行作为列标题,然后在表体中重复这一点。我假设这是因为首先使用foreach$values[0]作为$k=>c,然后使用foreach$values作为$v。我假设$values需要从$values[1]开始运行?将$values[n]设置为$v并从1到n运行for循环可以实现这一点吗?当然可以从第二行开始循环,但是……将列名称作为数据库中的第一行有什么好处?您可以在查询响应中获取列名作为键,而不必这样做。如果您想修改列名,比如用空格替换uu,我更喜欢在代码中这样做。
| col 4 |
=========
| 65 |
| 25 |
// mocking up values for example
$values = array( (object) array("col_1" => 72, "col_3" => 44, "col_4" => 16), (object) array("col_1" => 51, "col_3" => 87, "col_4" => 34));
// error message if no results
if(!count($values)) {
echo '<p>No results found for your current search. Use the inputs to the left to change the filters.</p>';
// display results
} else {
?>
<table border="1">
<thead>
<tr>
<?php foreach ($values[0] as $k => $c) { /* loop first row to get column names */ ?>
<th> <?php echo $k; ?> </th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach ($values as $v) { /* loop rows */ ?>
<tr>
<?php foreach ($v as $k => $c) { /* loop columns */ ?>
<td> <?php echo $c; ?> </td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
| col_1 | col_3 | col_4 |
==========================
| 72 | 44 | 16 |
| 51 | 87 | 34 |