gpt4 book ai didi

php - 在 foreach 循环中处理交叉引用表中的复选框检查

转载 作者:行者123 更新时间:2023-11-29 13:15:17 24 4
gpt4 key购买 nike

我有三个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com