【问题标题】:How to insert dynamic row in different html table position如何在不同的html表格位置插入动态行
【发布时间】:2016-02-04 05:41:30
【问题描述】:

我想使用 php 在 html 表中回显以下 xml 数据,

<type>Debit</type>
<item>Item-1</item>
<price>150</price>
<type>Debit</type>
<item>Item-2</item>
<price>250</price>
<type>Debit</type>
<item>Item-3</item>
<price>100</price>
type>Debit</type>
<item>Item-4</item>
<price>200</price>
............
...so on
<type>Credit</type>
<item>Item-50</item>
<price>200</price>
<type>Credit</type>
<item>Item-51</item>
<price>300</price>
<type>Credit</type>
<item>Item-60</item>
<price>100</price>
............
...so on

这是我插入数据之前的 Html 表结构

现在,如果您看到我的 xml 数据,我想动态插入借项列表下的借项和贷项列表中的贷项,如下例所示:

我知道如何使用 insertRow()、getElementById("id") 动态添加行。但在这种情况下,我需要维护两个不同的位置 [1 用于借方,1 用于贷方],以便我可以正确插入它们位置。此外,行是动态的,所以我猜具有固定行数的固定 id 将不起作用。我该如何在 php 中执行此操作。请分享您的想法以各种简单的方式解决它。请让我知道以获取更多信息.谢谢

【问题讨论】:

  • 你不知道如何将值从 php 回显到 html?
  • 你好@ujwal dhakal。感谢您的评论。我知道从 php 到 html 的回显。但是我需要在 2 个不同的地方将这些标记值作为表数据 td 回显[所有借方都将归入借方,所有贷方将归入 html 表中的贷方]。这就是问题所在。帖子中提供了更多详细信息。谢谢

标签: php html


【解决方案1】:

试试这个解决方案。如果您使用 javascript,则使用两个表并在 tbody 中使用 append() 元素行。

<!DOCTYPE html>
<html>
<body>

<?php
$myXMLData =
"
<data>
<row>
    <type>Debit</type>
    <item>Item-1</item>
    <price>150</price>
</row>
<row>
    <type>Debit</type>
    <item>Item-2</item>
    <price>250</price>
</row>
<row>
    <type>Debit</type>
    <item>Item-3</item>
    <price>100</price>
</row>
<row>
    <type>Debit</type>
    <item>Item-4</item>
    <price>200</price>
</row>
<row>
    <type>Credit</type>
    <item>Item-50</item>
    <price>200</price>
</row>
<row>
    <type>Credit</type>
    <item>Item-51</item>
    <price>300</price>
</row>
<row>
    <type>Credit</type>
    <item>Item-60</item>
    <price>100</price>
</row>
</data>";

$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");

$debit = array();
$credit = array();
$debit_total = $credit_total = 0;
foreach ($xml as $row) {
    if($row->type == 'Debit') {
        $debit[] = array('item'=> (string)$row->item, 'price'=> (float)$row->price);    
        $debit_total += (float)$row->price;
    } else {
        $credit[] = array('item'=> (string)$row->item, 'price'=> (float)$row->price);   
        $credit_total += (float)$row->price;
    }

}

?>

<table>
<tr>
    <td colspan="2">Debit list<td>
</tr>
<?php
foreach ($debit as $key => $value) {
    echo "<tr><td>{$value['item']}</td><td>{$value['price']}</td></tr>";
}
echo "<tr><td>Total</td><td>{$debit_total}</td></tr>";
echo "<tr><td colspan='2'></td></tr>";

echo "<tr><td colspan='2'>Credit list</td></tr>";
foreach ($credit as $key => $value) {
    echo "<tr><td>{$value['item']}</td><td>{$value['price']}</td></tr>";
}
echo "<tr><td>Total</td><td>{$credit_total}</td></tr>";
echo "<tr><td colspan='2'></td></tr>";
?>

</table>

</body>
</html>

【讨论】:

  • 只能回显变量时使用回显的方式。 &lt;?php foreach($credit as $key=&gt;$value): ?&gt;&lt;tr&gt;&lt;td&gt;&lt;?php echo $value['item']?&gt;&lt;/td&gt;&lt;/tr&gt; 在代码之后和&lt;?php endforeach; ?&gt; 之后
  • 如果您的商品描述具有“商品编号”格式,您可以使用explode("-") 描述并在foreach 之后使用该数字作为借/贷数组和ksort 数组中的键,对于对表格中的元素进行排序。
猜你喜欢
  • 1970-01-01
  • 2017-05-15
  • 2018-10-14
  • 2021-04-03
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 2018-07-17
  • 2020-09-15
相关资源
最近更新 更多