Problem with foreach loop and $_GET
I have a very simple foreach loop
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>';
}
With url query, with every click the current class repeated. How can avoid this? Thanks alot.
$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>";
}
The problem you had was that you never change $class
after it was assigned value 'currentt'
.
精彩评论