我有一个"网站列表"在我希望在网页上显示的SQL数据库中。我需要为每个站点创建一个div ID。暂时我这样做(将表格取回$ _SESSION数组,获取并显示循环中站点的名称并进行回声):
<div id='leftnavigation1' class="leftnavigation">
<ul>
<?php
$maxKeys = max(array_keys($_SESSION['usersmeter'])); //check how many sites are in the usersmeter table
for ($i = 0; $i <= $maxKeys; $i++) { //loop every key in the array
$siteName = $_SESSION['usersmeter'][$i]['siteName'];
echo "<li id='siteID$i'><a href='#'><span>" . $siteName . "</span></a></li>"; //display the name of the sites
}
?>
</ul>
</div>
它运行良好,但我认为将PHP代码包含在View中是一种不好的做法。
我怎么办? Javascript中的AJAX?
谢谢,
答案 0 :(得分:0)
Like that you will br try:
<?php
$my_array = array(
'GSU4300' => 'Lili Markina',
'GSU4301' => 'John Kokina',
'GSU4304' => 'Bill Clinong',
'GSU4305' => 'Obamark Chiko'
);
echo '<ul id="somethig">';
foreach ($my_array as $k => $v) {
echo '<li id="' . $k . '">' . $v . '</li>';
}
echo '</ul>';
?>