计算和整理html表值

时间:2012-01-31 10:58:11

标签: php mysql html

我有一个表格,可以在提交时使用PHP脚本生成HTML表格。

如果表格如此......

Qty | Colour | Size  | Name
----------------------------------
10  | Red    | Small | Name 1
5   | Red    | Small | Name 2
25  | White  | Large | Name 3

...我希望提供这样的信息:

15 Red Small
25 White Large

这是否可能,因为它使用一些我尚未学习的聪明的PHP方法?如果没有,我将使用数据库,是否可能?我想尽可能避免使用数据库,因为我不需要在站点上部署数据库功能,但如果需要可以将其放在另一个域上。

对于这方面缺乏研究工作的道歉,我不知道从哪里开始。

- 编辑以添加代码 -

HTML

<form action="processform.php" method="post" id="monogramming-builder">
  <fieldset>
    <table>
      <tr>
        <th>Qty</th>
        <th>Colour</th>
        <th>Size</th>
        <th>Name</th>
      </tr>
      <tr>
        <td><input type="text" name="qty[]" class="qty" /></td>
        <td>
          <select name="colour[]">
            <option value="">[Please Select]</option>
            <option value="Red">Red</option>
            <option value="Black">Black</option>
            <option value="White">White</option>
          </select>
        </td>
        <td>
          <select name="size[]">
            <option value="">[Please Select]</option>
            <option value="Small">Small</option>
            <option value="Medium">Medium</option>
            <option value="Large">Large</option>
            <option value="XL">XL</option>
            <option value="2XL">2XL</option>
          </select>
        </td>
        <td><input type="text" name="name[]" maxlength="18" /></td>
      </tr>
    </table>
    <p><a href="javascript:;" class="addrow">Add a row</a></p>    
    <input type="submit" value="Send Request" />
  </fieldset>
</form>

PHP:

$qtys = $_POST['qty'];
$colours = $_POST['colour'];
$sizes = $_POST['size'];
$names = $_POST['name'];

$output = '<h2>You have requested:</h2>' . "\n";
$output .= '<table><tr><th>Qty</th><th>Colour</th><th>Size</th><th>Name</th></tr>' . "\n";
for($i=0, $count = count($qtys); $i < $count; $i++) {
  $output .= '<tr>';
  $output .= '<td>' . $qtys[$i] . '</td>' . "\n"; 
  $output .= '<td>' . $colours[$i] . '</td>' . "\n";
  $output .= '<td>' . $sizes[$i] . '</td>' . "\n";
  $output .= '<td>' . $names[$i] . '</td>' . "\n";
  $output .= '</tr>' . "\n";   
}
$output .= '</table>';  
echo $output;

JS

$('.addrow').click(function(){
    var tablerow = '<tr><td><input type="text" name="qty[]" class="qty" /></td><td><select name="colour[]"><option value="">[Please Select]</option><option value="Red">Red</option><option value="Black">Black</option><option value="White">White</option></select></td><td><select name="size[]"><option value="">[Please Select]</option><option value="Small">Small</option><option value="Medium">Medium</option><option value="Large">Large</option><option value="XL">XL</option><option value="2XL">2XL</option></select></td><td><input type="text" name="name[]" maxlength="18" /></td></tr>';
    $('tr:last-child').after(tablerow);
  });

2 个答案:

答案 0 :(得分:1)

我认为这段代码或非常相似的代码会做你想要的:

$qtys = $_POST['qty'];
$colours = $_POST['colour'];
$sizes = $_POST['size'];
$names = $_POST['name'];

$totals = Array();

$output = '<h2>You have requested:</h2>' . "\n";
$output .= '<table><tr><th>Qty</th><th>Colour</th><th>Size</th><th>Name</th></tr>' . "\n";
for($i=0, $count = count($qtys); $i < $count; $i++) {
  // if we've not seen this colour before, set up an empty array
  if(!array_key_exists($colours[$i], $totals)) {
    $totals[$colours[$i]] = Array();
  }
  // if we've not seen this size for this colour before, set it to zero
  if(!array_key_exists($sizes[$i], $totals[$colours[$i]])) {
    $totals[$colours[$i]][$sizes[$i]] = 0;
  }
  // Add to the quantity for this colour/size
  $totals[$colours[$i]][$sizes[$i]] += $qtys[$i];

  $output .= '<tr>';
  $output .= '<td>' . $qtys[$i] . '</td>' . "\n"; 
  $output .= '<td>' . $colours[$i] . '</td>' . "\n";
  $output .= '<td>' . $sizes[$i] . '</td>' . "\n";
  $output .= '<td>' . $names[$i] . '</td>' . "\n";
  $output .= '</tr>' . "\n";   
}
$output .= '</table>';  
echo $output;


$output = "<table>";
foreach($totals as $color=>$totals_by_size) {
  foreach($totals_by_size as $size=>$quantity) {
    $output .= '<tr>';
    $output .= '<td>'.$color.'</td>';
    $output .= '<td>'.$size.'</td>';
    $output .= '<td>'.$quantity.'</td>';
    $output .= '</tr>';
  }
}
echo $output.'</table>';

我们所做的是当您循环遍历帖子数据以构建第一个表时,我们正在构建一个如下所示的多维数组:

array(2) {
  ["red"]=>
  array(1) {
    ["Small"]=>
    int(15)
  }
  ["white"]=>
  array(1) {
    ["large"]=>
    int(25)
  }
}

然后我们遍历该数组并构建另一个表来显示它。

答案 1 :(得分:1)

已发布答案。无论如何我已经尝试过了。你可以尝试这个:

$qtys = $_POST['qty'];
$colours = $_POST['colour'];
$sizes = $_POST['size'];
$names = $_POST['name'];
$product = array();


for($i=0, $count = count($qtys); $i < $count; $i++) {
    if (!isset($product[$colours[$i]][$sizes[$i]][$names[$i]])) {
        $product[$colours[$i]][$sizes[$i]][$names[$i]] = $qtys[$i];
    } else {
        $product[$colours[$i]][$sizes[$i]][$names[$i]] = $product[$colours[$i]][$sizes[$i]][$names[$i]] + $qtys[$i];
    }
}

$output = '';
$output .= '<h2>You have requested:</h2>' . "\n";
$output .= '<table><tr><th>Qty</th><th>Colour</th><th>Size</th><th>Name</th></tr>' . "\n";
foreach ($product as $product_keys => $product_values) {
    foreach ($product_values as $product_key => $product_value) {
        foreach ($product_value as $new_product_key => $new_product_value) {
              $output .= '<tr>';
                $output .= '<td>' . $new_product_value . '</td>' . "\n"; 
                $output .= '<td>' . $product_keys . '</td>' . "\n";
                $output .= '<td>' . $product_key . '</td>' . "\n";
                $output .= '<td>' . $new_product_key . '</td>' . "\n";
              $output .= '</tr>' . "\n"; 
        }

    }
}
$output .= '</table>';  
echo $output;

希望这可以帮助你:)