Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To update your table with CI all at once, you need an array like this:</p> <pre><code> $data = array( 'column_1_name' =&gt; 'value_to_insert' 'column_2_name' =&gt; 'value_to_insert' 'column_2_name' =&gt; 'value_to_insert' ); </code></pre> <p>Right now, you have an array like this $checked_array = ('stat', 'exclude', 'override') - with more or fewer members.</p> <p>To create the array that you can use in your update statement (like $data above), what you need to do is create another array with the name of each column and a value equal to 1 if it is in $checked_array or 0 if it was not in $checked array.</p> <p>The function add_values() below can do that for you. You have to supply two arguments: </p> <ol> <li>$all_possibilities is an array of each column name that you will want to update to either 1 or 0. In this case, it should look like $all_possibilities = array('stat', 'exclude', 'override');</li> <li>$selected: this is an array with the names of the boxes that were actually selected - it is a subset of $all_possibilities (or it is the same if all the boxes were checked).</li> </ol> <p>This function goes in your controller. </p> <pre><code> function add_values($all_possibilities, $selected) { $array_for_query = array(); // this goes through each possible box that can be checked to see if that possibility was actually selected foreach($all_possibilities as $array_member) { // this checks to see if the current member of $all_possibilities is found in the $selected array if (in_array($array_member, $selected)) { // if it is, it adds that as a member of a new query $array_for_query and gives it a value of 1 (true) $array_for_query[$array_member] = 1; } else { // if it is not, same as above, but gives it a value of 0 $array_for_query[$array_member] = 0; } } // once all the possibilities have been checked and added to the query array, that array is returned return $array_for_query; } </code></pre> <p>Now you can call the function and pass the two arguments in. The returned array is assigned to $checked_and_unchecked.</p> <pre><code> $data['checked_and_unchecked'] = add_values($possibilities, $checked_array); </code></pre> <p>Make sure you also grab the gl_id from the form. You can do that with a hidden field in your view:</p> <pre><code> &lt;input type="hidden" name="gl_id" value="&lt;?php echo $gl_field['gl_id'];?&gt;" /&gt; </code></pre> <p>In the controller, grab that value with </p> <pre><code> $data['gl_id'] = $this-&gt;input-&gt;post('gl_id'); </code></pre> <p>Then you can pass the information to your model and update your table passing in the name of the table and the array you created.</p> <pre><code> $this-&gt;model_name-&gt;model_method($data); </code></pre> <p>This will then go in the model:</p> <pre><code> $this-&gt;db-&gt;update('table_name', $checked_and_unchecked); $this-&gt;db-&gt;where('gl_id', $gl_id); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload