foreach循环和$ _GET的问题

时间:2010-06-09 15:40:56

标签: php

我有一个非常简单的foreach循环

foreach($tv as $id => $channel) {
$ID = $_GET['ID'];
if($ID == $id){$class = "currentt";}
echo '<a href="http://www.mysite.com/tst.php?ID='.$id.'"     class="'.$class.'">'.$channel.'</a><br>';
 }

使用url查询,每次单击都会重复当前的类。怎么能避免这个? 非常感谢。

1 个答案:

答案 0 :(得分:4)

$ID = $_GET['ID'];
foreach($tv as $id => $channel) {
    $class = $ID == $id ? "currentt": '';
    echo "<a href='http://www.mysite.com/tst.php?ID=$id' class='$class'>$channel</a><br>";
}

您遇到的问题是,在分配值$class后,您永远不会更改'currentt'