作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有三个 MySQL 表,其中一个是交叉引用表:
-----------------------------------
Table name: resources
id
resource
-----------------------------------
Table name: companies
id
company
-----------------------------------
Table name: res_co_xref
res_id
co_id
-----------------------------------
我可以通过以下查询成功加入他们:
SELECT * FROM res_co_xref LEFT JOIN companies ON (companies.id = res_co_xref.co_id) WHERE res_co_xref.res_id=1
结果:
res_id co_id
1 12
1 13
1 16
有 10 家公司,仅应检查 3 家公司是否有此特定资源。
这是我的问题代码,当前包含一个嵌套循环...结果是每行三个复选框。
<?php foreach ($companyrow as $rowco):?> // this is coming from a PHP class
<tr>
<td><?php echo $rowco->company ?></td>
<td>
<?php foreach ($rescorow as $rescorowloop):?> // this is from another PHP class
<div class="sel">
<label class="checkbox">
<?php if($rowco->id == $rescorowloop->co_id): ?> // here I successfully checkmark the correct company but there are three checkboxes
<input type="checkbox" name="company_id[]" value="<?php echo $rowco->id; ?>" checked="checked" />
<?php else: ?>
<input type="checkbox" name="company_id[]" value="<?php echo $rowco->id; ?>" />
<?php endif; ?>
</label>
</div>
<?php endforeach;?>
<?php unset($rescorowloop);?>
</td>
</tr>
<?php endforeach;?>
<?php unset($rowco);?>
你会如何处理这个问题?非常感谢您的建议。
最佳答案
通常我会这样做:
foreach ($thing as $key => $value)
{
if ($value == "something that needs to be checked")
{
$chk = "checked='checked'";
}
else
{
$chk = "";
}
echo "<input type='checkbox' name='name' id='id' " . $chk . ">";
}
您正在寻找什么?
关于php - 在 foreach 循环中处理交叉引用表中的复选框检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21542202/
我是一名优秀的程序员,十分优秀!