Storing Array of Identically-Named Elements |
[eluser]jpcody[/eluser]
The closest issue I've been able to find is this one, but I have some some modifications that I can't seem to grok. I've essentially got a list of inputs, where there might be one or many. Each is named 'goal,' as they are multiple goals being added, and they are added dynamically depending on how many goals a user has. When I submit this, however, I'm looking to store each individual goal as an element in some session data, then iterate over it in the next view to display all the goals. My problem is that only the last input seems to be stored. I assume that is because the controller below rewrites item_goals each time it finds a new one. I've found this forum post to extend the input class, but I seem to be unable to make magic happen. Code: function goalsAdd(){ Do you think you could help me either do this natively with CI or extend the Input class?
[eluser]skunkbad[/eluser]
I think you are going to need to use goal[] as the input name in your form for each goal. Then you would just loop through the array to get your goals: Code: <?php
[eluser]jpcody[/eluser]
Hey Skunkbad, thanks so much for chiming in. I'm particularly looking to store this in the controller and pass it as session data. I wasn't very clear about this at all before. Each goal field is currently named as 'goal' so I think most of what I need to be doing is simply iterating over each of these in the controller. I've tried extending the input class like so: Code: function posts($index = '', $xss_clean = FALSE) But had no luck there, either. It simply pulls the value of the last input with a name of 'goal.' Any idea if what I'm looking to do is possible by iterating through each instance of goal in the controller? It seems when I keep trying to do this and pass the data to the view, I'm getting an invalid argument supplied for foreach. Thanks again for your help.
[eluser]pickupman[/eluser]
That's because you keep writing to the array you are iterating through. Code: function posts($index = '', $xss_clean = FALSE)
[eluser]jpcody[/eluser]
Thanks for responding, pickupman. It seems the furthest I can get with this is an error message re: invalid parameters passed to implode. Could my problem be even earlier in the process than I'm realizing?
[eluser]pickupman[/eluser]
Ah...maybe try Code: $this->input->post('goals[]');
[eluser]Phil Sturgeon[/eluser]
Code: $goals = $this->input->post('goals'); Then you have an array of them all. You can set arrays in the session library.
[eluser]jpcody[/eluser]
Naming the inputs as "name[]" led $this->input->goal('name'); to return a array. Thanks so much for all of your help on this! |
Welcome Guest, Not a member yet? Register Sign In |