mercredi 31 décembre 2014

Multiple loop conditions for a set of checkbox choices

Using CodeIgniter, I am creating an admin area where I am able to create footnotes for any given product line.


HTML



<p>Choose Product Line</p>
<label><input type="checkbox" name="productline[]" value="1" />Product One</label>
<label><input type="checkbox" name="productline[]" value="2" />Product Two</label>
<label><input type="checkbox" name="productline[]" value="3" />Product Three</label>
<label><input type="checkbox" name="productline[]" value="4" />Product Four</label>


PHP



for ( $i = 0; $i < count( $this->input->post( 'product[]' ) ); $i++ )
{
$line__id = $this->input->post( 'product' )[$i];

$footnote_data = array(
'line__id' => $line__id,
'code' => $this->input->post( 'footnotecode' ),
'text' => $this->input->post( 'footnotetext' ),
'status' => $this->input->post( 'status' ),
'createdon' => date( 'Y-m-d H:i:s' )
);

error_log( print_r( $footnote_data, true ) );
}


Which will output something like this:


LOG:



Array
(
[line__id] => 1
[code] => ABC123
[text] => Hello World
[status] => 1
[createdon] => 2014-12-31 22:28:59
)

Array
(
[line__id] => 2
[code] => ABC123
[text] => Hello World
[status] => 1
[createdon] => 2014-12-31 22:28:59
)
....


All is fine, I'm looping through each product line that was selected.


As you can see in the $footnote_data array, I have several other fields. They are simple text input fields, and are 1:1 with the current iteration of the loop $i.


I also have 2 other checkbox choices. You have to choose the product line (above) then enter the code and actual footnote text. Then, you have to choose what type of footnote this is.


HTML



<p>Footnote Type</p>
<label><input type="checkbox" name="footnotetype[]" value="awesome" />Awesome</label>
<label><input type="checkbox" name="footnotetype[]" value="cool" />Cool</label>
<label><input type="checkbox" name="footnotetype[]" value="average" />Average</label>


I can choose as many footnote types that I want for the current footnote I am creating. I'm just not sure the best way to dive into the second series of checkboxes (the type).


I have tried a nested loop within the main productline loop, but that didn't get the result(s) I was looking for. If I log the result I am looking for, this is what it would look like:


OUTPUT



Array
(
[line__id] => 2
[type] => Awesome
[code] => ABC123
[text] => Hello World
[status] => 1
[createdon] => 2014-12-31 22:28:59
)

Array
(
[line__id] => 2
[type] => Cool
[code] => ABC123
[text] => Hello World
[status] => 1
[createdon] => 2014-12-31 22:28:59
)

Array
(
[line__id] => 3
[type] => Average
[code] => ABC123
[text] => Hello World
[status] => 1
[createdon] => 2014-12-31 22:28:59
)

....


See how the same product line can have multiple footnote types? The actual record would/could be exactly the same for each product line selected. Only the type could be different.


Another thought I had - once the initial loop is done - and I have a footnote ID for the newly created footnote, do I turn right around and update the db for each footnote type? I'm talking myself in circles...


My DB structure looks like this:


DATABASE



footnote_id
type_id // this is the question
text
code
status
createdon


Thank you for your help!





Aucun commentaire:

Enregistrer un commentaire