Sorry if this question has asked before but i has been looking for and i cannot find what i looking for.
This is my database structure
| ID | collection_ID | valuation | postby_ID | post_datetime |
This is my view page (for input data)
<form action="insert.php" method="post">
<?php $i = 0; ?>
<?php foreach ($significance as $data): ?>
<input type="text" name="var[<?php echo $i; ?>][]" value="<?php echo $data->category; ?>" >
<select name="var[<?php echo $i; ?>][]">
<option value="0"> 0 </option>
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
</select>
<input type="text" name="var[<?php echo $i; ?>][]">
<?php $i++; ?>
<?php endforeach; ?>
<button type="submit"> Submit </button>
</form>
This is my Controller so far:
$collectionID = $this->input->get('coll_id'); //get from url
$this->form_validation->set_rules('val[]', 'Some text', 'required', array('required'=>'%s required'));
if ($this->form_validation->run == FALSE)
{
$data = array(
'page_title' => 'Some Title',
'part' => 'input-data',
'detail' => $this->collection_db->get_collection_data($collectionID),
'significance' => $this->significance_db->get_significance_list()
);
$this->load->view('form-registration', $data);
}
else
{
$db_data = array(
'collection_ID' => $collectionID,
'valuation' => json_encode($this->input->post('val)),
'post_by' => $this->session->userdata('user_ID'),
'post_datetime => get_datetime_format() //some helper function
);
if ($this->significance_db->save_data($db_data) == TRUE)
{
'some function here if TRUE';
}
else
{
'some function here if FALSE;
}
}
This is my Model so far:
function save_data($db_data)
{
$this->db->insert('table_name', $db_data);
if ($this->db->affected_rows > 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
And this is my value in valuation column database since the foreach loop generate 6 form field:
[["variable 1", "1", "description 1"],["variable 2", "0", "description 2"],["variable 3", "3", "description 3"],["variable 4", "3", "description 4"],["variable 5", "2", "description 5"],["variable 6", "0", "description 6"]]
So, this is a good example for the result or i do a wrong coding? because in another view page i want to extract the result using json_decode in foreach loop like this:
Variable : variable 1
Score: 1
Description: description 1
Variable : variable 2
Score: 0
Description: description 2
Variable : variable 3
Score: 3
Description: description 3
Variable : variable 4
Score: 3
Description: description 4
Variable : variable 5
Score: 2
Description: description 5
Variable : variable 6
Score: 0
Description: description 6
Another question is: How i generate json_encode result from view and controller above become like this:
[
["variable" => "variable 1", "score" => "1", "description" => "description 1"],
["variable" => "variable 2", "score" => "0", "description" => "description 2"],
[etc...]
]