Reputation: 5933
Problem:
I am trying to generate a custom HTML form with help of values in an array using PHP.
The PHP code ($row['Key'] will contain W, B and R):
$numbers[$row['Key']] = array (
'C' =>
array (
'BO' => $row['BO'],
'BT' => $row['BT']),
'D' =>
array (
'MF' => $row['MF'],
'MT' => $row['MT'])
);
Array produced with PHP:
Array
(
[W] => Array
(
[C] => Array
(
[BO] => 36
[BT] => 63
)
[D] => Array
(
[MF] => 54
[MT] => 63
)
)
[B] => Array
(
[C] => Array
(
[BO] => 60
[BT] => 105
)
[D] => Array
(
[MF] => 90
[MT] => 105
)
)
[R] => Array
(
[C] => Array
(
[BO] => 12
[BT] => 21
)
[D] => Array
(
[MF] => 18
[MT] => 24
)
)
)
The outcome should look like the following. Notice the combination of W/B/R and BO/BT/MF/MT.
<table>
<tbody>
<tr>
<td>W</td>
<td><input type="text" name="WBO" id="WBO" value="36"></td>
<td><input type="text" name="WBT" id="WBT" value="63"></td>
<td><input type="text" name="WMF" id="WMF" value="54"></td>
<td><input type="text" name="WMT" id="WMT" value="63"></td>
</tr>
<tr>
<td>B</td>
<td><input type="text" name="BBO" id="BBO" value="60"></td>
<td><input type="text" name="BBT" id="BBT" value="105"></td>
<td><input type="text" name="BMF" id="BMF" value="90"></td>
<td><input type="text" name="BMT" id="BMT" value="105"></td>
</tr>
<tr>
<td>R</td>
<td><input type="text" name="RBO" id="RBO" value="12"></td>
<td><input type="text" name="RBT" id="RBT" value="21"></td>
<td><input type="text" name="RMF" id="RMF" value="18"></td>
<td><input type="text" name="RMT" id="RMT" value="24"></td>
</tr>
</tbody>
</table>
Upvotes: 1
Views: 4061
Reputation: 1058
without using 3 foreach loops
$var = print_r($numbers, true);
$table = preg_replace_callback(
"{.*?[^ ][ ]{4}\[(\S+)\].*?[^ ][ ]{8}\)}s",
"func",
$var);
$table= preg_replace('{\n[^<].*}','',$table);
echo "<table>{$table}</table>";
function: (count space to find array level)
function func($matches)
{
$table='';
$k=$matches[1];
$t=$matches[0];
$tr = preg_replace('{.*?[^ ][ ]{20}\[(\S+)\].*?=>.*?(\S+)}s',PHP_EOL."<td><input type='text' id='{$k}$1' name='{$k}$1' value='$2'></td>",$t);
$table .= "<tr>".PHP_EOL;
$table .= "<td>{$k}<td>{$tr}".PHP_EOL;
$table .= "</tr>".PHP_EOL;
return $table;
}
echo
<table><tr>
<td>W<td>
<td><input type='text' id='WBO' name='WBO' value='36'></td>
<td><input type='text' id='WBT' name='WBT' value='63'></td>
<td><input type='text' id='WMF' name='WMF' value='54'></td>
<td><input type='text' id='WMT' name='WMT' value='63'></td>
</tr>
<tr>
<td>B<td>
<td><input type='text' id='BBO' name='BBO' value='60'></td>
<td><input type='text' id='BBT' name='BBT' value='105'></td>
<td><input type='text' id='BMF' name='BMF' value='90'></td>
<td><input type='text' id='BMT' name='BMT' value='105'></td>
</tr>
</table>
Upvotes: 2
Reputation: 6147
Check this code dude.
<table>
<tbody>
<?php
foreach($numbers as $key=>$sarray){
echo "<tr>";
echo "<td>$key</td>";
foreach($sarray as $key1=>$sarray1){
foreach($sarray1 as $fname=>$fvalue){
echo '<td><input type="text" name="'.$fname.'" id="'.$fname.'" value="'.$fvalue.'"></td>';
}
}
echo "</tr>";
}
?>
</tbody>
</table>
Upvotes: 1
Reputation:
this?
echo "<table><tbody>";
foreach((array)$numbers as $key=>$val) {
echo "<tr><td>".$key."</td>";
foreach((array)$val as $key2=>$val2) {
foreach((array)$val2 as $key3=>$val3) {
echo '<td><input type="text" name="'.$key.$key3.'" id="'.$key.$key3.'" value="'.$val3.'"></td>';
}
}
echo "</tr>";
}
echo "</tbody></table>";
Upvotes: 1
Reputation: 46846
Should be fairly easy to step through this array with foreach()
.
[ghoti@pc ~]$ cat doit.php
#!/usr/local/bin/php
<?php
printf("<table>\n <tbody>\n");
$numbers=array(
'W' => array(
'C' => array( 'BO' => 36, 'BT' => 63),
'D' => array( 'MF' => 54, 'MT' => 63),
),
'B' => array(
'C' => array( 'BO' => 60, 'BT' => 105),
'D' => array( 'MF' => 90, 'MT' => 105),
),
);
$fmt1 = "\t<tr>\n"
. "\t\t<td>%s</td>\n"
. "%s"
. "\t</tr>\n";
$fmt2 = "\t\t<td><input type='text' name='%s%s' id='%s%s' value='%s'></td>\n";
foreach ($numbers as $index1 => $line1) {
foreach ($line1 as $index2 => $line2) {
foreach ($line2 as $index3 => $value) {
$output .= sprintf($fmt2, $index1, $index3, $index1, $index3, $value);
}
}
printf($fmt1, $index1, $output);
$output = "";
}
printf(" </tbody>\n</table>\n");
And the output:
[ghoti@pc ~]$ ./doit.php
<table>
<tbody>
<tr>
<td>W</td>
<td><input type='text' name='WBO' id='WBO' value='36'></td>
<td><input type='text' name='WBT' id='WBT' value='63'></td>
<td><input type='text' name='WMF' id='WMF' value='54'></td>
<td><input type='text' name='WMT' id='WMT' value='63'></td>
</tr>
<tr>
<td>B</td>
<td><input type='text' name='BBO' id='BBO' value='60'></td>
<td><input type='text' name='BBT' id='BBT' value='105'></td>
<td><input type='text' name='BMF' id='BMF' value='90'></td>
<td><input type='text' name='BMT' id='BMT' value='105'></td>
</tr>
</tbody>
</table>
[ghoti@pc ~]$
Upvotes: 1