CodeIgniter Forums
passing an array to form - update - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: passing an array to form - update (/thread-11826.html)



passing an array to form - update - El Forum - 09-24-2008

[eluser]Unknown[/eluser]
I am writing a form that's on the back-end admin that displays a list of businesses. Right now, I've built the part where it displays the businesses from the db and keeps them checked. I am trying to to get the form to insert a 1 or 0 in the display column in the db and return checked or unchecked depending on whether or not the the boxes in the view are checked - display 1. unchecked - display 0.

Here's my code:

Controller:

Code:
<?php
    class whats_nearby extends Controller {


    function index()
    {
    
        }        
    function whats_nearby()
    {
        parent::Controller();
        $data['types'] = $this->artslookup->get_all_by_type('businessType');
        $this->load->model('lookup_model');
            if($this->input->post('businessType'))
        {
            $this->lookup_model->update_whatsnearby($this->input->post('businessType'));
            $data['showConfirm'] = TRUE;
            
            
    }
        $this->load->view('admin/whats_nearby/index',$data);    
            
    
            
            }
        
            }

?>

Model:

Code:
function update_whatsnearby($lookup)
    
    {
    $data = array(
               'title' => $title,
               'name' => $name,
               'date' => $date
            );

    $this->db->where('id', $id);
    $this->db->update('mytable', $data);
    
    }    
        
}

View:
Code:
<?
$this->load->view('/admin/header');
?>
<?
$this->load->view('/admin/nav-column');
?>
<div id="main_column">
<h2>What's NearBy</h2>


&lt;? if (isset($_POST['Submit_x']) && isset($_POST
['submit_y'])) {
    printf('<h1> You Clicked at the following coordinates: x-%s, y-%s</h1>',
    ($_POST ['Submit_x']),
    ($_POST ['Submit_y'])
    );
    } else {
?&gt;
&lt;form method="post" action=" &lt;?php
echo ($_SERVER['PHP_SELF']); ?&gt;"&gt;
&lt;? while(list($key,$thisType) = each($types)): if ($thisType['display'] != '0')
echo '<br>&lt;input type=checkbox name="businessType[]" checked&gt;' ?&gt;

&lt;? if(in_array($key,$thisType['lookupType']))echo 'checked'; ?&gt;
&lt;?=$thisType['lookupType']?&gt;
&lt;? endwhile; ?&gt;

<br>
<br>
<br>
&lt;input type="image" name= "Submit" src="/images/button_submit_changes.gif"&gt;
&lt;/form&gt;
&lt;?
    }
?&gt;
&lt;? if ($this->config->item('enable_profiler')) $this->output->enable_profiler(true); ?&gt;

&lt;?
$this->load->view('/admin/footer');
?&gt;



If you can help I would really appreciate it. I've been hung up on this for a while now.


passing an array to form - update - El Forum - 09-24-2008

[eluser]JoostV[/eluser]
By default, CI can not deal with arrays in $this->input and $this-=>validation. Could this be what is buggin you?

If so, the way I see you have three options:

- use $_POST['businessType'] to access your array of post values from checkboxes, instead of $this->input->post('businessType'). $_POST can handle arrays.
- use checkboxenames like businessType_1, businessType_2 and loop through those using $this->input or $this->validation. (not a very elegant solution, but it works)
- search the forums for a library that DOES allow CI to work with arrays in $this->input and $thjis->validation. I'm sure there are some out there.


passing an array to form - update - El Forum - 09-24-2008

[eluser]Unknown[/eluser]
I was actually hoping to get some help with passing the array data to the model from the controller and get a dynamic update with a 1 or 0 in the table column....The view as far as I know is sorted out. Thanks for your input.


passing an array to form - update - El Forum - 09-24-2008

[eluser]Colin Williams[/eluser]
Quote:By default, CI can not deal with arrays in $this->input and $this-=>validation. Could this be what is buggin you?

Actually, $this->input->post() has no problem handling posted arrays, however validation is limited in this regard.


passing an array to form - update - El Forum - 09-24-2008

[eluser]JoostV[/eluser]
Oops, sorry. Barking up the wrong tree there, then.