0

感谢您花时间看这个问题。

目前,我有一段代码可以创建四个标记为“奢侈品、品牌、零售商”和“B2B”的复选框。我研究了许多 PHP 方法来创建复选框,我觉得 implode() 函数是最简单且适合我的工作的。我查看了许多创建内爆的教程,但是,它们不符合我的标准,因为我希望数据库值反映在前端。目前在我的数据库中,implode() 有效,因此(例如),如果我检查“奢侈品”、“品牌”、“零售商”,然后按“提交”按钮,则三个项目“奢侈品、品牌、零售商”将在该指定单元格中。看起来我的代码在后端工作,但这些是我的问题:

  1. 我不确定(尽管有多个 Google)如何检索存储在单单元格数组中的那些值,并将其选为“已选择”(这将“选中”前端的框)

有人可以看看我下面的代码,让我知道似乎缺少/错误/错误的内容,以便我可以尝试修改吗?任何事情都将不胜感激,谢谢!

<?

if (isset($_POST['formSubmit2'])){
    $category = mysql_real_escape_string(implode(',',$_POST['category']));
    $accountID = $_POST['accountID'];

    mysql_query("UPDATE Spreadsheet SET category='$category' WHERE accountID='$accountID'");
}


$query = mysql_query("SELECT * FROM Spreadsheet LIMIT $firstRow,$rpp");

while($row = mysql_fetch_array($query)){  

// Begin Checkboxes

$values = array('Luxury','Brand','Retailer','B2B');

  ?>

<form name ="category" method ="POST" action ="" >

 <?

echo "<input type = 'hidden' name = 'accountID' value = '" . $row['accountID'] . "' >";
        for($i = 0; $i < count($values); $i++){
 ?>

 <input type="checkbox" name="category[]" value="<?php echo $values[$i]; ?>" id="rbl_<? echo $i; ?>" <? if($row['category'] == $i) echo "checked=\"checked\""; ?>/>
 <? echo $values[$i] ?><br>

 <? } ?>

        <input type ="Submit" name ="formSubmit2" value ="Submit" />
        </form>

 <? } ?>
4

1 回答 1

0

The best approach i can recommend given what you have is to, explode the values out of the db giving you a new array of all the select fields. Then use in_array to compare the list you have with this new list in the loop. then flag the checkboxs as needed.

于 2012-12-18T16:15:21.973 回答