Index.php
是根据domains.php
的数据构建的。如何使用domains.php
函数动态foreach
创建这些表格和选项?
domains.php
<?php
$price=array(
"cz"=> "20",
"com"=> "10",
"net"=> "8",
"org"=> "8",
"info"=> "8",
"biz"=> "8",
"name"=> "25",
"mobi"=> "25"
);
$renamed=array(
"cz"=> "Czech Republic",
"com"=> "international",
"mobi"=> "mobile"
);
?>
的index.php
<?php include("domains.php"); ?>
<div id="prices">
<table class="table">
<tr><td>.cz</td><td>20$</td></tr>
<tr><td>.com</td><td>10$</td></tr>
<tr><td>.net</td><td>8$</td></tr>
</table>
<table class="table">
<tr><td>.info</td><td>8$</td></tr>
<tr><td>.biz</td><td>8$</td></tr>
<tr><td>.name</td><td>25$</td></tr>
</table>
<table class="table">
<tr><td>.mobi</td><td>25$</td></tr>
<tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>
</table>
</div>
<select id="zone">
<option value="cz">Czech Republic</option>
<option value="com">international</option>
<option value="net">.net</option>
<option value="org">.org</option>
<option value="info">.info</option>
<option value="biz">.biz</option>
<option value="name">.name</option>
<option value="mobi">mobile</option>
</select>
答案 0 :(得分:1)
这应该给你一个开始。有关详细信息,请参阅php.net的大量文档。如果每个表只需要三行,则必须使用一些行计数器。
$row = 0;
foreach($price as $tld => $money) {
if ($row == 0)
echo "<table>\n";
echo "<tr><td>$tld</td><td>$money</td></tr>\n";
// close the table after three rows
$row++;
if ($row >= 3) {
echo "</table>\n";
$row = 0;
}
}
// close the final table, if needed
if ($row > 0)
echo "</table>\n";
答案 1 :(得分:1)
这将完成这项工作:
<div id="prices">
<table class="table">
<?php
// Iterate through the list of domains and prices and print a row for each.
foreach($price as $domain => $price) {
print '<tr><td>.'.$domain.'</td><td>'.$price.'$</td></tr> ';
}
?>
</table>
</div>
<select id="zone">
<?php
// Iterate through the list of domains and use the value of $renamed if exists.
foreach(array_keys($price) as $domain) {
print '<option value="'.$domain.'">'.isset($renamed[$domain]) ? $renamed[$domain] : $domain.'</option>';
}
?>
</select>