CodeIgniter - Checking to see if a radio button is checked in the database -
im having bit of trouble putting code ... im trying add code code have @ moment check radiobuttons checked in database.
the code have @ moment takes roles database, outputs them using foreach statement, splits results 2 columns, have @ moment.
<?php $i = 0; $output = ""; foreach($roles $row){ if($i > 0){ $i = 0; } if($i == 0) { $output .= "<div class='box'>"; } $output .= '<div class="row">'; $output .= ' <input name="_'.$row->key.'" type="radio" id="'.$row->key.'" class="radio" />'; $output .= ' <label for="'.$row->key.'" style="text-transform: lowercase;">'.$row->name.'</label>'; $output .= '</div>'; if($i ==0) { $output .= "</div>"; } $i++; } if($i != 1) { $output .= "</div>"; } echo $output;
?>
ok, want check radio button in code posted, when there match in database, values checked user, use following.
model
function get_roles_by_id($freelancerid) { $query = $this->db->query('select * '.$this->table_name.' user_id = "'.$freelancerid.'"'); return $query->result(); }
then controller looks this
$data['positions'] = $this->freelancer_roles->get_roles_by_id($freelancer_id);
as bring array cant use foreach statement check radio button id's returned in positions array.
could me figure out.
cheers,
i think understand question , seems simple thing trying do. if
you should have model return array of names of checkboxes saved user in following format: array("checkbox1", "checkbox2", "checkbox3") in output can use native php function in_array()
for example:
$output .= '<div class="row">'; $output .= ' <input name="_'.$row->key.'" type="radio" id="'.$row->key.'" class="radio"'; if(in_array($row->name, $data['positions']) { $output .= ' checked '; } $output . = '/>'; $output .= ' <label for="'.$row->key.'" style="text-transform: lowercase;">'.$row->name.'</label>'; $output .= '</div>';
as side note, have following code:
if($i > 0){ $i = 0; } if($i == 0) { $output .= "<div class='box'>"; }
if follow logic in code see $i equal 0 second if statement, making both if statements redundant.
Comments
Post a Comment