PHP печатает имена полей SQL как массив с соответствующими значениями, отображаемыми один раз в строке

Есть таблица SQL, как это:

Titles Device1 Device2 Device3 Title1 inputA inputA inputB Title2 inputA inputB inputC Title3 inputB inputB inputB 

Я хочу, чтобы значения inputA, inputB, inputC каждый отображались один раз в каждой строке со своими соответствующими полями Device1, Device2, Device3, отображались в виде массива, следующих за значениями «ввода» следующим образом:

 Titles header1 header2 header3 Title1 inputA: inputB: Device1 Device3 Device2 Title2 inputA: inputB: inputC: Device1 Device2 Device3 Title3 inputB: Device1 Device2 Device3 

Это то, что у меня есть до сих пор после пользовательского заголовка и инструкции 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>"); } 

Но не знаете, как получить все поля «Устройство» для отображения со своими соответствующими значениями. Выглядит так:

 Titles header1 header2 header3 Title1 inputA: inputB: Device1 Device3 Title2 inputA: inputB: inputC: Device1 Device2 Device3 Title3 inputB: Device1 

Единственный раз, когда в ячейке появлялось больше одного поля устройства, когда в строке с заголовком не было значений. Отсутствует что-то перед break ? Заявление?

Прежде всего, проще сначала преобразовать данные в формат, который вы можете легко выводить. Следуя небольшому примеру:

 $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 (включая пустые конечные ячейки):

 $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>'); в $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>');