2012-06-01 64 views
1

問題:使用數組值與PHP生成自定義HTML表單

我試圖產生與使用PHP的數組值的幫助下自定義HTML表單。

PHP代碼$行[ '鍵']將包含W,B和R):

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 
       ) 
     ) 
) 

:用PHP產生

$numbers[$row['Key']] = array (
          'C' => 
           array (
            'BO' => $row['BO'], 
            'BT' => $row['BT']), 
          'D' => 
           array (
            'MF' => $row['MF'], 
            'MT' => $row['MT']) 
         ); 

陣列結果應該如下所示。注意W/B/R和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> 
+0

[你嘗試過什麼( http://mattgemmell.com/2008/12/08/what-have-you-tried/)? – ghoti

+0

老實說,我還沒有嘗試編寫一個例子,雖然我知道如何使用foreach - 我被困在需要將W/B/R與BO/BT/MF/MT結合的部分。 – kexxcream

回答

2

不使用3 foreach循環

$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 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; 
} 

呼應

<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> 
1

呢?

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

現貨!我有一個想法做同樣的解決方案,但我仍然想知道是否沒有使用3個foreach循環的「更好看」的解決方案。 – kexxcream

+0

我認爲這裏的所有解決方案「涉及」3個foreach循環:),說實話,我不認爲這個問題可以解決沒有循環;你可以在沒有循環的情況下硬編碼它,但是如果你的數組長度是1000個對象會怎樣?另外,請注意我的方法中的(數組)?它也可以防止空的子陣列。 – 2012-06-01 12:52:20

1

應該很容易地通過這個數組與foreach()

[[email protected] ~]$ 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"); 

和輸出:

[[email protected] ~]$ ./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> 
[[email protected] ~]$ 
1

檢查這個代碼的傢伙。

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