如何从包含的文件中插入数据并自动创建HTML元素?

时间:2012-07-10 05:21:33

标签: php

我想从包含文件中插入数据并自动创建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,我必须手动创建所有表实例。但我希望找到一种方法来自data.php为每个<tr class="vps_*_dc*">...</tr>自动创建所有这些$vps_*

2 个答案:

答案 0 :(得分: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>';
    }
}

编辑:在仔细阅读了您的问题后,您的要求似乎比我原先想象的要复杂得多。

希望这就是你要找的东西。

祝你好运!

答案 1 :(得分: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>"


?>