duanhuan7750 2016-09-19 18:03
浏览 52
已采纳

数据以HTML格式复制到其他动态表

I am having trouble with the way my data is being displayed. Any data that is entered into the first and second tables will not be displayed, but anything entered into the 3rd table will be displayed in not only the 3rd table, but the first and second as well. How can I change this so the data can be displayed in each table separately.

Here is an example of what it looks like: Data Entered

Data displayed

Here is my HTML/PHP related code:

<p>
<div class="border1">
<div id="rebate_400p">
<strong>400P</strong><br>
</div>

<?php if(isset($_POST['rows'])): ?>
    <table cellspacing="20">
        <tr align="center" class="table_row">
            <td>Tier</td>
            <td>Purchase Minimum</td>
            <td>Multiplier</td>
            <td>UOM</td>
            <td>Retro</td>
            <td>Guaranteed</td>
            <td>Paid</td>
        </tr>
        <?php 
            $count = 1;
            foreach($_POST['rows'] as $row): 
        ?>
            <tr align="center">
                <td><?php echo $count; ?></td>
                <td><?php echo $row['purchase_minimum']; ?></td>
                <td><?php echo $row['multiplier']; ?></td>
                <td><?php echo $row['uom']; ?></td>
                <td><?php echo $row['retro']; ?></td>
                <td><?php echo $row['guaranteed']; ?></td>
                <td><?php echo $row['paid']; ?></td>
            </tr>
        <?php 
            $count++;
            endforeach; 
        ?>
    </table>
<?php endif; ?>
</div>
</p>

<!-- 400M -->
<p>
<div class="border1">
<div id="rebate_400m">
<strong>400M</strong><br>
</div>

<?php if(isset($_POST['rows'])): ?>
    <table cellspacing="20">
        <tr align="center" class="table_row">
            <td>Tier</td>
            <td>Purchase Minimum</td>
            <td>Multiplier</td>
            <td>UOM</td>
            <td>Retro</td>
            <td>Guaranteed</td>
            <td>Paid</td>
        </tr>
        <?php 
            $count = 1;
            foreach($_POST['rows'] as $row): 
        ?>
            <tr align="center">
                <td><?php echo $count; ?></td>
                <td><?php echo $row['purchase_minimum']; ?></td>
                <td><?php echo $row['multiplier']; ?></td>
                <td><?php echo $row['uom']; ?></td>
                <td><?php echo $row['retro']; ?></td>
                <td><?php echo $row['guaranteed']; ?></td>
                <td><?php echo $row['paid']; ?></td>
            </tr>
        <?php 
            $count++;
            endforeach; 
        ?>
    </table>
<?php endif; ?>
</div>
</p>

<!-- 400D -->
<p>
<div class="border1">
<div id="rebate_400d">
<strong>400D</strong><br>
</div>

<?php if(isset($_POST['rows'])): ?>
    <table cellspacing="20">
        <tr align="center" class="table_row">
            <td>Tier</td>
            <td>Purchase Minimum</td>
            <td>Multiplier</td>
            <td>UOM</td>
            <td>Retro</td>
            <td>Guaranteed</td>
            <td>Paid</td>
        </tr>
        <?php 
            $count = 1;
            foreach($_POST['rows'] as $row): 
        ?>
            <tr align="center">
                <td><?php echo $count; ?></td>
                <td><?php echo $row['purchase_minimum']; ?></td>
                <td><?php echo $row['multiplier']; ?></td>
                <td><?php echo $row['uom']; ?></td>
                <td><?php echo $row['retro']; ?></td>
                <td><?php echo $row['guaranteed']; ?></td>
                <td><?php echo $row['paid']; ?></td>
            </tr>
        <?php 
            $count++;
            endforeach; 
        ?>
    </table>
<?php endif; ?>
</div>
</p>

Here is some of my Javascript code:

// ----- Deletes row for 400p -----
function deleteRow(row)
{
    var i=row.parentNode.parentNode;
    i.parentNode.removeChild(i);
}

// ----- Adds row for 400p -----
function insRow()
{
  var x=document.getElementById('tables');
  var new_row = x.rows[1].cloneNode(true);
  var len = x.rows.length;
  new_row.cells[0].innerHTML = len;

  var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
  inp1.id += len;
  inp1.value = '';
  var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
  inp2.id += len;
  inp2.value = '';

  var inputs = new_row.querySelectorAll('input[type=text]');

  for(var i=0;i<inputs.length;i++)
  {
      inputs[i].value='';
      inputs[i].name='rows[' + len + '][' + inputs[i].dataset.name + ']';
  }

  x.appendChild(new_row)
}

// ----- JS for 400M -----

// ----- Deletes row for 400m -----
function deleteRow1(row)
{
    var i=row.parentNode.parentNode;
    i.parentNode.removeChild(i);
}

// ----- Adds row for 400m -----
function insRow1()
{
  var x=document.getElementById('tables1');
  var new_row1 = x.rows[1].cloneNode(true);
  var len = x.rows.length;
  new_row1.cells[0].innerHTML = len;

  var inp1 = new_row1.cells[1].getElementsByTagName('input')[0];
  inp1.id += len;
  inp1.value = '';
  var inp2 = new_row1.cells[2].getElementsByTagName('input')[0];
  inp2.id += len;
  inp2.value = '';

  var inputs = new_row1.querySelectorAll('input[type=text]');

  for(var i=0;i<inputs.length;i++)
  {
      inputs[i].value='';
      inputs[i].name='rows[' + len + '][' + inputs[i].dataset.name + ']';
  }

  x.appendChild(new_row1)
}

// ----- JS for 400D -----

// ----- Deletes row for 400d -----
function deleteRow2(row)
{
    var i=row.parentNode.parentNode;
    i.parentNode.removeChild(i);
}

// ----- Adds row to 400d -----
function insRow2()
{
  var x=document.getElementById('tables2');
  var new_row2 = x.rows[1].cloneNode(true);
  var len = x.rows.length;
  new_row2.cells[0].innerHTML = len;

  var inp1 = new_row2.cells[1].getElementsByTagName('input')[0];
  inp1.id += len;
  inp1.value = '';
  var inp2 = new_row2.cells[2].getElementsByTagName('input')[0];
  inp2.id += len;
  inp2.value = '';

  var inputs = new_row2.querySelectorAll('input[type=text]');

  for(var i=0;i<inputs.length;i++)
  {
      inputs[i].value='';
      inputs[i].name='rows[' + len + '][' + inputs[i].dataset.name + ']';
  }

  x.appendChild(new_row2)
}
  • 写回答

3条回答 默认 最新

  • doushishi6513 2016-09-20 15:39
    关注

    Solved your issue by modifying your code in several different places. In your current code, you get chaotic results because you're overwriting some values by using same array index. In order to make sure it does not conflict, I would suggest to use multidimensional arrays. A few changes:

    1. First table contains multiple indexes:

    Added an additional [0] index. This should apply to all inputs, not just purchase_minimum

    <input type="text" id="rebate_tables" data-name="purchase_minimum" name="rows[0][0][purchase_minimum]">
    

    In second table we do the same but write [1] instead of [0] as in the first table (next index with increasing order):

    <input type="text" id="rebate_tables" data-name="purchase_minimum" name="rows[1][0][purchase_minimum]">
    

    And in the third table we will have [2]:

    <input type="text" id="rebate_tables" data-name="purchase_minimum" name="rows[2][0][purchase_minimum]">
    

    Also, it's important to make last indexes (words) without a number (instead of purchase_minimum1 like in your case, use purchase_minimum).

    2. In JavaScript we do the same thing + second index should be len - 1 instead of len because otherwise would go 0, 2, 3, 4, ...

    For 1st table:

    for (var i = 0; i < inputs.length; i++) {
        inputs[i].value = '';
        inputs[i].name = 'rows[0][' + (len - 1) + '][' + inputs[i].dataset.name + ']';
    }
    

    For 2nd table we will have:

    inputs[i].name = 'rows[1][' + (len - 1) + '][' + inputs[i].dataset.name + ']';
    

    And in the third:

    inputs[i].name = 'rows[2][' + (len - 1) + '][' + inputs[i].dataset.name + ']';
    

    3. In your confirmation page we change only foreach in all areas:

    Would be:

    foreach($_POST['rows'][0] as $row):
    

    Instead of:

    foreach($_POST['rows'] as $row):
    

    Also, in each foreach you'll have to remove those same additional numbers in (string) indexes, like:

    <td><?php echo $row['purchase_minimum']; ?></td>
    

    Let me know if I missed anything and it's still not working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?