I have two arrays both having elements in string type. Example :
First Array
$default_complaint = array("Login", "Printer", "Monitor", "Computer",
"Network", "Other");
Second Array
$selected_complaint = explode(" ", $s['kindof_request']);
// Ex : it return like this => array ("Login", "Printer", "Monitor");
Now, how can I create the checkboxes that ticked in html by comparing that two arrays given above. So, I create like this:
<?php
$default_complaint = array("Login", "Printer", "Monitor", "Computer", "Network", "Lain-lain");
$selected_complaint = explode(" ", $s['kindof_request']);
foreach ($default_complaint as $dc) {
foreach ($selected_complaint as $sc) {
$check = strcmp($dc, $sc) ;
if ($check == 0) { //True
echo '<input type="checkbox" checked="checked">'. "$sc" ."<br />";
} else{ //false
echo '<input type="checkbox">'. "$dc"."<br />";
}
}
}
?>
My code still give me weird result. So, How to create like this, => (0) meaning checked.
(0)Login (0)Printer (0)Monitor ()Computer ()Network ()Others
You have two loops, but you only want to loop over the first array. The second one is only used for checking.
One possibility for the loop is:
foreach ($default_complaint as $dc) {
if (array_search($dc, $selected_complaint) !== FALSE) {
echo '<input type="checkbox" checked="checked">'. "$dc" ."<br />\n";
} else{
echo '<input type="checkbox">'. "$dc"."<br />\n";
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다