Reputation: 101
Actually i fetched data successfully but checkbox is not showing checked values. Maybe the logic is wrong. And it is necessary to use foreach here, can't we direct echo in value field, i am new to it and maybe the mistakes are silly
view
<html>
<?php
foreach($post_id as $data){
$a=$data->id;
$b=$data->name;
$c=$data->email;
$d=json_decode($data->skills);
$e=$data->notes;
$f=$data->gender;
}
?>
<?php var_dump($d);?>
<?php var_dump($f);?>
<body>
<form method="post" action="<?php echo site_url('Student_info/update'); ?>">
<table>
<tr>
<td>ENTER NAME</td>
<td><input type="text" name="name" value="<?php echo $b; ?>"></td>
</tr>
<tr>
<td>ENTER EMAIL </td>
<td><input type="email" name="email" value="<?php echo $c; ?>"></td>
</tr>
<tr>
<td>ENTER SKILLS</td>
<td>
<input type="checkbox" name="skills[]" value="php" <?php if($d == "php"){echo 'checked="checked"'; } ?>>php<br>
<input type="checkbox" name="skills[]" value="dotnet" <?php if($d == "dotnet"){echo 'checked="checked"'; } ?> >dotnet<br>
<input type="checkbox" name="skills[]" value="java" <?php if($d == "java"){echo 'checked="checked"'; } ?>>java<br>
<input type="checkbox" name="skills[]" value="ruby_on_rails" <?php if($d == "ruby_on_rails"){echo 'checked="checked"'; } ?> >ruby_on_rails<br>
</td>
</tr>
<tr>
<td>NOTES</td>
<td> <textarea name="notes" rows="4" cols="50"><?php echo $e; ?></textarea> </td>
</tr>
<tr>
<td>GENDER</td>
<td> <input type="radio" name="gender" value="male" <?php if($f == "male"){echo 'selected="selected"'; } ?>> Male<br>
<input type="radio" name="gender" value="female" <?php if($f == "Female"){echo 'selected="selected"'; } ?>> Female<br> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="add" value="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
Upvotes: 1
Views: 4169
Reputation: 16117
Result of this $d
is an object not a string value because you are using json_decode()
here:
$d=json_decode($data->skills); // this will produce an object
You can use in_array()
, but for this you need to use second param as TRUE in json_decode()
function, this will return the result in array, something like:
$d=json_decode($data->skills,TRUE); // this will return an array
Than you can check like that:
<input type="checkbox" name="skills[]" value="php" <?=(in_array("php",$d) ? 'checked=""' : '')?>>php
Upvotes: 1