1

I want explode data from table "school_minat" into checklist in checbox looping table from "dayaminat".

I already try, but my checklist show only one. whereas my data minat_id in table school_minat 4 data = smk2,smk3,smk1,smk4

<center> 
<?php $sql = "select * from dayaminat";
$rs = mysql_query($sql);
$i = 0;
while($row = mysql_fetch_array($rs)){  // Looping data from Table "dayaminat"
    ?>
    <input name='minat[]' 
    <?php 
    $values = $result['minat_id']; // Data "minat_id" already Selected in Database table "school_minat"
    $array_of_values = explode(",", $values); //Explode Data "Minat"  already Selected in Database  table "school_minat"
    if (in_array($row['minat_id'],$array_of_values)) {
    echo 'checked="checked"';
    } 
    ?> 
    value='<?php echo $row['minat_id']?>'  type='checkbox'>&nbsp; <?php echo $row['minat'] ?> &nbsp;
    <?php 
    $i ++;
}?>
</center>

Help me. Thank's :)

2 Answers 2

2

You must loop in your $array_of_values

<center> 
<?php $sql = "select * from dayaminat";
$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs)){  // Looping data from Table "dayaminat"
    $values = $result['minat_id']; // Data "minat_id" already Selected in Database table "school_minat"
    $array_of_values = explode(",", $values); //Explode Data "Minat"  already Selected in Database  table "school_minat"

    for($i = 0; $i < count($array_of_values); $i++) {
    echo "<input name='minat[]' ";
    if (in_array($row['minat_id'], $array_of_values)) {
    echo 'checked="checked"';
    } 
    ?> 
    value='<?php echo $array_of_values[$i]; ?>'  type='checkbox'>&nbsp; <?php echo $array_of_values[$i]; ?> &nbsp;
    <?php 
  }
}?>
</center>
Sign up to request clarification or add additional context in comments.

3 Comments

i try type in outside while i type $values = $result['minat_id']; $array_of_values = explode(",", $values); echo $values; // My data show smk2,smk3,smk1,smk4 i try type again echo $array_of_values; // only show text "Array"
if you want see $array_of_values you must do this print_r($array_of_values);
i try it, is array show. but why my checklist always not show :(
1

You are looping only through the database row. As there is just 1 row you get only 1 result.

So try looping through the array! Probably using foreach loop..

<center> 
<?php $sql = "select * from dayaminat";
$rs = mysql_query($sql);
$i = 0;
while($row = mysql_fetch_array($rs)){  // Looping data from Table "dayaminat"

    $values = $result['minat_id']; // Data "Minat" already Selected in Database table "school_minat"
    $array_of_values = explode(",", $values); //Explode Data "Minat"  already Selected in Database  table "school_minat"?>
    foreach ($array_of_values as $arrayItem){

        <input name='minat<?php echo $i;?>' 
        <?php 

        if (in_array($row['minat_id'],$arrayItem)) {
        echo 'checked="checked"';
        } 
        ?> 
        value='<?php echo $arrayItem ?>'  type='checkbox'>&nbsp; <?php echo $arrayItem ?> &nbsp;
        <?php 
        $i ++;
    }
}?>
</center>

Comments

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.