1

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   

3 Answers 3

2

This should work for you:

(You don't have to do a nested foreach loop)

<?php

    $default_complaint = array("Login", "Printer", "Monitor", "Computer", "Network", "Lain-lain");                      
    $selected_complaint = explode(" ", $s['kindof_request']);

    foreach($default_complaint as $k => $v) {

        if(isset($selected_complaint[$k]) && in_array($selected_complaint[$k], $default_complaint))
            echo '<input type="checkbox" checked>' .  $v . "<br />";
        else
            echo '<input type="checkbox">' . $v . "<br />";
    }

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks 4 the help. but your code just ticked the last checkbox ()Login ()Printer (0)Monitor ()Computer ()Network ()Others , Thanks again...
1

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";
    }
}

1 Comment

Thanks 4 the help. but your code just ticked the last checkbox ()Login ()Printer (0)Monitor ()Computer ()Network ()Others , Thanks again...
1

You don't need two foreach loops. Only one will do it. Loop through $default_complaint array and check whether that element is present in $selected_complaint array using in_array(). Try using:

<?php
    $default_complaint = array("Login", "Printer", "Monitor", "Computer", "Network", "Other");
    $selected_complaint = explode(" ", $s['kindof_request']);

    foreach ($default_complaint as $dc)
    {
        if (in_array($dc, $selected_complaint))
            echo '<input type="checkbox" checked>' . $dc . '<br>';
        else
            echo '<input type="checkbox">' . $dc . '<br>';
    }
?>

You can also try array_search() instead of in_array().

3 Comments

Thanks 4 the help. but your code just ticked the last checkbox ()Login ()Printer (0)Monitor ()Computer ()Network ()Others , Thanks again...
@FadlyDzil I don't get any problem with this code. Please look here. Thanks.
Oke, I solved this problem with : $selected_complaint = array_map('trim', explode(',', $d['kind_of_request'])); There is commas where explode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.