尝试将作为GET传递的数组中的值与数据库记录集do/while循环中的值进行比较时运气不佳。如果GET数组中的任何值与记录集id匹配,我将尝试选中一个复选框。如果您在URL中只使用一个不带逗号的兴趣id,则该复选框有效。非常感谢。
我的URL是:interestSearch.php?兴趣=3(有效)interestSearch.php?兴趣=3.8(无效)
<? require('db.php');
mysql_select_db($database_data);
$query_allInterests = "SELECT * FROM interests";
$allInterests = mysql_query($query_allInterests, $data) or die(mysql_error());
$row_allInterests = mysql_fetch_assoc($allInterests);
$totalRows_allInterests = mysql_num_rows($allInterests);
?>
<form method="get">
<? do { ?>
<input <?
if(isset($_GET['interests']) && $_GET['interests'] != "") {
$theCounter = 0;
$theArray = array($_GET['interests']);
foreach ($theArray as $value) {
// if ($value == $row_allInterests['id']) {$theCounter++;}
if (in_array($row_allInterests['id'], $theArray)) {$theCounter++;}
}
if($theCounter > 0){echo "checked";}
}
?> name="<? echo $row_allInterests['id']; ?>" class="doCheck" type="checkbox" id="<? echo $row_allInterests['id']; ?>" value="<? echo $row_allInterests['id']; ?>" /> <? echo $row_allInterests['interest']; ?><br />
<? } while ($row_allInterests = mysql_fetch_assoc($allInterests)); ?>
</form>
而不是
$theArray = array($_GET['interests']);
使用
$theArray = explode( ',', $_GET['interests']);