Php 如何将array_column()和array_filter()集成到sql结果的动态表生成中
我想从sql数据库中读取数据并在表中显示它们。这很有效。现在,我只想显示其中至少有一个值的列,而不是空列(包含NULL、0、空字符串)。这适用于以下示例:Php 如何将array_column()和array_filter()集成到sql结果的动态表生成中,php,mysql,dynamic,html-table,Php,Mysql,Dynamic,Html Table,我想从sql数据库中读取数据并在表中显示它们。这很有效。现在,我只想显示其中至少有一个值的列,而不是空列(包含NULL、0、空字符串)。这适用于以下示例: enter code here <TABLE width="500" border="1" cellpadding="1" cellspacing="1"> <?php $query = mysql_query("SELECT * FROM guestbook", $db); $resu
enter code here
<TABLE width="500" border="1" cellpadding="1" cellspacing="1">
<?php
$query = mysql_query("SELECT * FROM guestbook", $db);
$results = array();
while($line = mysql_fetch_assoc($query)){
$results[] = $line;
$Name = array_column($results, 'Name');
$Home = array_column($results, 'Home');
$Date = array_column($results, 'Date');
$Emptycolumn = array_column($results, 'Emptycolumn');
$Comment = array_column($results, 'Comment');
$City = array_column($results, 'City');
}
echo "<TR>";
if(array_filter($Name)) {echo "<TH>Name</TH>";}
if(array_filter($Home)){echo "<TH>Home</TH>";}
if(array_filter($Date)){echo "<TH>Date</TH>";}
if(array_filter($Emptycolumn)){echo "<TH>Emptycolumn</TH>";}
if(array_filter($Comment)){echo "<TH>Comment</TH>";}
if(array_filter($City)){echo "<TH>City</TH>";}
echo "</TR>";
$query = mysql_query("SELECT * FROM guestbook", $db);
while($line = mysql_fetch_assoc($query)){
echo "<TR>";
if(array_filter($Name)) {echo "<TD>".$line['Name']."</TD>";}
if(array_filter($Home)) {echo "<TD>".$line['Home']."</TD>";}
if(array_filter($Date)) {echo "<TD>".$line['Date']."</TD>";}
if(array_filter($Emptycolumn)) {echo "<TD>".$line['Emptycolumn']."</TD>";}
if(array_filter($Comment)) {echo "<TD>".$line['Comment']."</TD>";}
if(array_filter($City)) {echo "<TD>".$line['City']."</TD>";}
echo "</TR>";
}
?>
</TABLE>
在此处输入代码
由于“我的表”的列名是高度可变的(取决于查询),因此通过在结果数组中循环生成表,首先是列名,然后是行中的值:
enter code here
$sql = "SELECT DISTINCT $selection FROM $tabelle WHERE
$whereclause"; //will be changed to PDO
$result = mysqli_query($db, $sql) or die("<b>No result</b>"); //Running
the query and storing it in result
$numrows = mysqli_num_rows($result); // gets number of rows in result
table
$numcols = mysqli_num_fields($result); // gets number of columns in
result table
$field = mysqli_fetch_fields($result); // gets the column names from the
result table
if ($numrows > 0) {
echo "<table id='myTable' >";
echo "<thead>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th>";
for($x=0;$x<$numcols;$x++){
$key = array_search($field[$x]->name, $custom_column_arr);
if($key !== false){
echo "<th>" . $key . "</th>";
}else{
echo "<th>" . $field[$x]->name . "</th>";
}
}
echo "</tr></thead>";
echo "<tbody>";
$nr = 1;
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $nr . "</td>";
for ($k=0; $k<$numcols; $k++) { // goes around until there are no
columns left
echo "<td>" . $row[$field[$k]->name] . "</td>"; //Prints the data
}
echo "</tr>";
$nr = $nr + 1;
} // End of while-loop
echo "</tbody></table>";
}
}
mysqli_close($db);
在此处输入代码
$sql=“从$tabelle中选择不同的$selection,其中
$WHERE条款”//将更改为PDO
$result=mysqli_query($db,$sql)或die(“无结果”)//跑步
查询并将其存储在结果中
$numrows=mysqli_num_行($result);//获取结果中的行数
桌子
$numcols=mysqli_num_字段($result);//获取中的列数
结果表
$field=mysqli_fetch_fields($result);//从中获取列名
结果表
如果($numrows>0){
回声“;
回声“;
回声“;
回显“'Nr.”;
对于($x=0;$xname,$custom\u column\u arr);
如果($key!==false){
回显“$key.”;
}否则{
回显“.$field[$x]->name.”;
}
}
回声“;
回声“;
$nr=1;
while($row=mysqli\u fetch\u数组($result)){
回声“;
回声“$nr.”;
对于($k=0;$kname]。“”;//打印数据
}
回声“;
$nr=$nr+1;
}//while循环结束
回声“;
}
}
mysqli_close($db);
现在,我尝试将上面示例中的array_column()和array_filter()块集成到循环中,但不幸的是,它不起作用。我相信,这对专业人士来说很容易,如果有人能帮我解决这个问题,我将非常感激!
非常感谢您!!很抱歉,当我发现
mysql\uu
时,我停止了阅读。您使用此功能有什么特别的原因吗?array\u column()
是一个添加到PHP版本中的函数,它同时支持mysqli
和PDO
,所以这不应该是一个问题。或者..是因为您忽略了页面上的巨大红色块吗?它只存在了4.5年,所以您可能会错过它。为什么您要在项目中混合使用mysql
和mysqli
API?Poss非常感谢您的评论!@Xorifelse,我知道数据库查询只在PDO中是安全的。但是,我是PDO新手,首先必须熟悉这项工作。这将是解决当前问题后的下一步。@Rajdeep Paul,我不知道混合使用mysql和mysqli是一种不好的行为到目前为止,代码仍然有效。