0

我想从包含的文件中插入数据并自动创建 HTML 元素。

data.php我有:

$vps_de_dc1_s1 = array(
"name"=>"Germany, DC1, First",
"price"=>"10€",
);

$vps_de_dc1_s2 = array(
"name"=>"Germany, DC1, Second",
"price"=>"10€",
);

$vps_de_dc2_s1 ...、$vps_de_dc2_s2 ...、$vps_cz_dc1_s1 ...、$vps_cz_dc12_s1 ...等。

page.php中应该有:

include('data.php');

<tr class="vps_de_dc1">
    <td class="column-left">
        name
    </td>
    <td class="column-right">
        $vps_de_dc1_s1["name"]
    </td>
    <td class="column-right">
        $vps_de_dc1_s2["name"]
    </td>
    <td class="column-right">
        $vps_de_dc1_s3["name"]
    </td>
</tr>

<tr class="vps_de_dc2">
    <td class="column-left">
        name
    </td>
    <td class="column-right">
        $vps_de_dc2_s1["name"]
    </td>
    <td class="column-right">
        $vps_de_dc2_s2["name"]
    </td>
    <td class="column-right">
        $vps_de_dc2_s3["name"]
    </td>
</tr>

...

我想知道,是否可以在这里以某种方式自动创建表格元素?


更新:事实上,echo我必须手动创建所有表实例。但我想找到一种方法来<tr class="vps_*_dc*">...</tr>为每个$vps_*来自 data.php 的人自动创建所有这些。

4

2 回答 2

2

首先从 data.php 开始。在底部,添加所有数组的主数组:

$masterArray = array(
    'vps_de' => array ('dc' => 2, 's' => 4), // The key is the root name.
    'vps_cz' => array ('dc' => 2, 's' => 4), // The value is information about how many dc and s exist per location.
    // This example means that this has 2 dc and 4 s for a total of 8 possible arrays that you've defined.
    ...
);

在 page.php 中:

foreach ($masterArray as $varName => $infoArray) // Iterating through the master list.
{
    for ($dc = 1; $dc <= $infoArray['dc']; $dc++) // Iterating through however many numbers up to the dc limit.
    {
        $className = $varName . '_dc' . $dc; // Creating the class name, which is the location key root concatenated with the dc number.
        echo '<tr class="' . $className . '">'; // Echo the table row.
        for ($s = 1; $s <= $infoArray['s']; $s++) // Iterating through however many numbers up to the s limit.
        {
            $arrayName = $className . '_s' . $s; // Creating the name of the variable, reference to the array.
            if (isset($$arrayName)) // Checking to see if the variable by the name we just created exists.
            {
                $tmpArray = $$arrayName; // Using a variable variable to reference the array using its name.
                echo '<td class="column-left">';
                echo $tmpArray['name']; // I think you can use $$arrayName['name'] here, but it's been loaded to $tmpArray just to be safe.
                echo '</td>';
            }
        }
        echo '</tr>';
    }
}

编辑:在阅读了您的问题之后,您的要求似乎比我最初想象的要复杂。

希望这是您正在寻找的。

祝你好运!

于 2012-07-10T05:34:56.200 回答
0
<?php

include('data.php');

echo "
<tr class=\"vps_de_dc1\">
    <td class=\"column-left\">
       name
    </td>
    <td class=\"column-right\">".
       $vps_de_dc1_s1["name"].
    "</td>
    <td class="column-right">".
       $vps_de_dc1_s2["name"].
    "</td>
    <td class=\"column-right\">".
       $vps_de_dc1_s3["name"].
    "</td>
</tr>

<tr class=\"vps_de_dc2\">
    <td class="column-left">
       name
    </td>
    <td class=\"column-right\">".
       $vps_de_dc2_s1["name"].
    "</td>
    <td class=\"column-right\">".
       $vps_de_dc2_s2["name"].
    "</td>
    <td class=\"column-right\">".
       $vps_de_dc2_s3["name"].
    " </td>
</tr>"


?>
于 2012-07-10T05:30:21.307 回答