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


How to insert data from the included file and create HTML-elements automatically?

我想从包含的文件中插入数据并自动创建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中为每个$vps_*自动创建所有这些<tr class="vps_*_dc*">...</tr>

首先从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>';
    }
}

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

希望这就是你想要的。

祝你好运!

<?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>"

?>