将GET数组值与Recordset循环值进行比较

时间:2014-09-02 15:39:12

标签: php arrays get recordset

我没有运气试图将作为GET传递的数组中的值与数据库记录集do / while循环中的值进行比较。我试图勾选一个复选框,如果GET数组中的任何值与记录集ID匹配。如果您在URL中仅使用一个兴趣ID而不使用逗号,则它可以正常工作。谢谢。

我的网址是: interestSearch.php?interests = 3(这个有效) interestSearch.php?interests = 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>

1 个答案:

答案 0 :(得分:0)

而不是

$theArray = array($_GET['interests']);

使用

$theArray = explode( ',', $_GET['interests']);