1

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>
2
  • What have you tried? Commented Jun 1, 2012 at 11:06
  • To be honest I haven't tried to code an example, although I know how to work with foreach - I am stuck at the part where I need to combine W/B/R with BO/BT/MF/MT. Commented Jun 1, 2012 at 11:08

5 Answers 5

5

http://php.net/manual/en/control-structures.foreach.php

Sign up to request clarification or add additional context in comments.

Comments

2

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>

Comments

1

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 ~]$ 

Comments

1

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>";

2 Comments

Spot on! I had an idea of doing the same solution but I am still wondering if there isn't a "better looking" solution without using 3 foreach loops.
i think all solutions here "involve" 3 foreach loops :) and, honestly, i dont think this problem can be solved without loops; You can hard code it without loop, sure, but what if your array is 1000 objects long? Also, notice the (array) in my approach? it protects against empty sub-arrays as well.
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.