[英]Comparing array values with mysqli result
Can someone help me to solve this issue? 有人可以帮我解决这个问题吗?
Below code i used in an edit page, and i want to compare the checkbox values and if it is matching make it as checked. 下面的代码我在编辑页面中使用了,我想比较复选框的值,如果匹配则将其设为选中状态。
Example of my two array values 我的两个数组值的示例
$CategoryDetails : Array ( [0] => 6 [1] => 1 )
$Category_data : Array ( [0] => 1 [1] => 3 [2] => 6 [3] => 2 [4] => 4 [5] => 7)
$Category_data
is am using to create checkbox and $CategoryDetails
is the selected values. $Category_data
用于创建复选框, $CategoryDetails
是选定的值。 So as per above eg checkbox 1 & 3 should be checked, but below code giving me only one checked ie. 因此,按照上面的方法,例如应该选中复选框1和3,但是下面的代码仅给我一个复选框,即。 checkbox3 复选框3
I need to compare each value of array1 with array2. 我需要将array1的每个值与array2进行比较。
<?php
$CategoryDetails = isset($category_list) ? $category_list : ' ';
$Category = dbSelectByWhere("Highlight_categories", "WHERE Highlight_cat_status=1", "Order By Highlight_Category_name");
$k = 0;
while ($Category_data = dbFetchArray($Category)) {
?>
<input type="checkbox" name="category[]" id="Category" value="<?php echo $Category_data['Highlight_category_id']; ?>" data-parsley-mincheck="1" required class="flat" <?php
if (isset($CategoryDetails) && ($CategoryDetails[$k] == $Category_data['Highlight_category_id'])) {
echo 'checked';
if ($k < (count($CategoryDetails) - 1)) {
$k++;
}
}
?>/> <?php echo $Category_data['Highlight_Category_name']; ?>
<?php } ?>
1st : You need to use in_array function. 1st:您需要使用in_array函数。
2nd : in_array function searches an array for a specific value .if value exists it will return true else it will return false . 第二个: in_array函数在数组中搜索特定值。如果值存在,它将返回true,否则将返回false。
3rd : If empty declare the variable as array 第三:如果为空,则将该变量声明为数组
$CategoryDetails = isset($category_list) ? $category_list : array();
PHP : PHP的:
while ($Category_data = dbFetchArray($Category)) {
?>
<input type="checkbox" name="category[]" id="Category" value="<?php echo $Category_data['Highlight_category_id']; ?>" data-parsley-mincheck="1" required class="flat" <?php
if (isset($CategoryDetails) && in_array($Category_data['Highlight_category_id'],$CategoryDetails)) {
echo 'checked';
}
?>/> <?php echo $Category_data['Highlight_Category_name']; ?>
<?php } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.