0

I want to insert data from included file and create HTML-elements automatically.

In data.php I have:

$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 ..., etc.

And in page.php there should be:

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>

...

I want to know, is it possible to automate somehow creation of table elements here?


Update: In fact, for echo I have to manually create all table instances. But I want to find a way to create all these <tr class="vps_*_dc*">...</tr> automatically for every $vps_* from 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 に答える