PHP 打印 SQL 字段名称作为数组,对应的值每行显示一次
PHP print SQL field names as array with corresponding values showing once per row
有一个像这样的SQLtable:
Titles Device1 Device2 Device3
Title1 inputA inputA inputB
Title2 inputA inputB inputC
Title3 inputB inputB inputB
我希望值 inputA、inputB、inputC 在每一行中都出现一次,并带有相应的字段 Device1、Device2、Device3,以数组形式出现在 "input" 值之后,如下所示:
Titles header1 header2 header3
Title1 inputA: inputB:
Device1 Device3
Device2
Title2 inputA: inputB: inputC:
Device1 Device2 Device3
Title3 inputB:
Device1
Device2
Device3
这是我在自定义 header 和 SELECT 声明后得到的:
$myquery = $mysqli->query ($sqlSelect);
if ($myquery = $mysqli->query ($sqlSelect)) {
while($row = mysqli_fetch_assoc($myquery)){
$format=array();
foreach ($row as $key => $val) {
switch($key) {
case "Device1":
case "Device2":
case "Device3":
$format[$val] = "<br>".$key;
break;
}
}
printf ("<tr><td>%s</td>", $row["Titles"]);
foreach ($format as $key => $val) {
printf ("<td>$key:<br/>$val</td>");
}
printf ("</tr>");
}
但不知道如何让所有 "Device" 字段显示相应的值。看起来像这样:
Titles header1 header2 header3
Title1 inputA: inputB:
Device1 Device3
Title2 inputA: inputB: inputC:
Device1 Device2 Device3
Title3 inputB:
Device1
单元格中出现多个设备字段的唯一一次是在带有标题的行中没有值的情况下。 break
之前遗漏了什么? while
语句?
首先,首先将数据转换为更容易输出的格式会更容易。以下是一个小例子:
$result = array();
while($row = mysqli_fetch_assoc($myquery)){
$tmp = array();
foreach (array('Device1', 'Device2', 'Device3') as $key) {
if (!isset($tmp[$row[$key]])) $tmp[$row[$key]] = array();
$tmp[$row[$key]][] = $key;
}
$result[$row['Titles']] = $tmp;
}
现在您可以输出正确的 html table(包括空尾随单元格):
$max = 0;
foreach ($result as $title => $inputs) {
$max = max($max, count($inputs));
}
print('<table>');
print('<tr><td>Titles</td><td>header1</td><td>header2</td><td>header3</td></tr>');
foreach ($result as $title => $inputs) {
print('<tr><td>' . $title . '</td>');
foreach ($inputs as $input => $devices) {
print('<td>' . $input . '<br/>' . implode('<br/>', $devices) . '</td>');
}
// fill with empty cells
for ($i = 0; $i < $max - count($inputs); ++$i) {
print('<td></td>');
}
print('</tr>');
}
print('</table>');
有一个像这样的SQLtable:
Titles Device1 Device2 Device3
Title1 inputA inputA inputB
Title2 inputA inputB inputC
Title3 inputB inputB inputB
我希望值 inputA、inputB、inputC 在每一行中都出现一次,并带有相应的字段 Device1、Device2、Device3,以数组形式出现在 "input" 值之后,如下所示:
Titles header1 header2 header3
Title1 inputA: inputB:
Device1 Device3
Device2
Title2 inputA: inputB: inputC:
Device1 Device2 Device3
Title3 inputB:
Device1
Device2
Device3
这是我在自定义 header 和 SELECT 声明后得到的:
$myquery = $mysqli->query ($sqlSelect);
if ($myquery = $mysqli->query ($sqlSelect)) {
while($row = mysqli_fetch_assoc($myquery)){
$format=array();
foreach ($row as $key => $val) {
switch($key) {
case "Device1":
case "Device2":
case "Device3":
$format[$val] = "<br>".$key;
break;
}
}
printf ("<tr><td>%s</td>", $row["Titles"]);
foreach ($format as $key => $val) {
printf ("<td>$key:<br/>$val</td>");
}
printf ("</tr>");
}
但不知道如何让所有 "Device" 字段显示相应的值。看起来像这样:
Titles header1 header2 header3
Title1 inputA: inputB:
Device1 Device3
Title2 inputA: inputB: inputC:
Device1 Device2 Device3
Title3 inputB:
Device1
单元格中出现多个设备字段的唯一一次是在带有标题的行中没有值的情况下。 break
之前遗漏了什么? while
语句?
首先,首先将数据转换为更容易输出的格式会更容易。以下是一个小例子:
$result = array();
while($row = mysqli_fetch_assoc($myquery)){
$tmp = array();
foreach (array('Device1', 'Device2', 'Device3') as $key) {
if (!isset($tmp[$row[$key]])) $tmp[$row[$key]] = array();
$tmp[$row[$key]][] = $key;
}
$result[$row['Titles']] = $tmp;
}
现在您可以输出正确的 html table(包括空尾随单元格):
$max = 0;
foreach ($result as $title => $inputs) {
$max = max($max, count($inputs));
}
print('<table>');
print('<tr><td>Titles</td><td>header1</td><td>header2</td><td>header3</td></tr>');
foreach ($result as $title => $inputs) {
print('<tr><td>' . $title . '</td>');
foreach ($inputs as $input => $devices) {
print('<td>' . $input . '<br/>' . implode('<br/>', $devices) . '</td>');
}
// fill with empty cells
for ($i = 0; $i < $max - count($inputs); ++$i) {
print('<td></td>');
}
print('</tr>');
}
print('</table>');