【发布时间】:2017-01-24 21:34:19
【问题描述】:
我如何将笑话类别表中的复选框显示为选中,以及那些不显示但未选中的复选框。这是我目前拥有的:
<div class="control-group">
<?php
$sql = 'SELECT jokecategory.joke_id, jokecategory.category_id, category.name
FROM jokecategory
INNER JOIN category ON category.id = jokecategory.category_id
WHERE joke_id= :joke_id';
$stmt = $dbConnection->prepare($sql);
$stmt->bindValue(':joke_id', $id);
$stmt->execute();
?>
<fieldset>
<legend class="control-label">Categories:</legend>
<?php foreach ($stmt as $row) { ?>
<div class="controls">
<label for="category<?php echo($row['category_id']);?>">
<input type="checkbox" id="category<?php echo $row['category_id']; ?>" name="checkbox[]" value="<?php echo $row['category_id']; ?>" checked>
<?php echo($row['name']); ?></label>
</div>
<?php } ?>
with(类别表中的数据):
<?php $sql = 'SELECT id, name FROM category';
foreach ($dbConnection->query($sql) as $data) { ?>
<div class="controls">
<label for="category<?php echo($data['id']);?>">
<input type="checkbox" name="categories[]" id="category<?php echo($data['id']); ?>" value="<?php echo($data['id']);
?>">
<?php echo($data['name']); ?></label>
</div>
<?php } ?>
</fieldset>
</div>
目前它分为两个部分,因为我不确定如何将输出数据显示到一个合并组中。所以它是这样的:
Joke id: xxxxx
joke_text: xxxxxx
author_name: xxxxx
categories
1: [x]
2: [x]
3: [ ]
4: [ ]
【问题讨论】:
-
首先,您是否从查询中获取数据?获取后您是否尝试过
print_r($data)?其次,$joke_id and other variables将始终具有查询的最后一个数据,因为这些变量不是数组。第三,你的foreach调用$data['category_id'],对吗? -
我接收到所有其他字段的数据。这只是我不确定如何组合的循环元素。
-
使用 'print_r($data)' 和 'joke.id = 28',它在 jokecategory 表下有 4x 记录。但是,'$data['category_id'] 仅显示 jokecategory 表中的 1x 数据项。
-
你能分享
$data数组吗? ( print_r() ) -
$data 输出:数组([id] => 28 [joke_text] => jhohio [joke_date] => 2017-01-23 12:41:11 [name] => user [email] => user@email.com ) 数组 ( [joke_id] => 28 [category_id] => 4 [name] => 走吧)