Welcome Guest, Not a member yet? Register   Sign In
Storing Array of Identically-Named Elements
#1

[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(){
        $item_title = $this->input->post('topic');
        $item_hours = $this->input->post('hours');
        $item_minutes = $this->input->post('minutes');
        $item_goals = $this->input->post('goal');
        $item_time = $meeting_hours . ":" . $meeting_minutes;

        $sessionData = array(
            'totaltime' => $item_time,
            'title' => $item_title,
            'goals' => $item_goals
        );
        
        $this->session->set_userdata($sessionData);
        redirect('create/items');
    }

Do you think you could help me either do this natively with CI or extend the Input class?
#2

[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
if(isset($_POST) && ! empty($_POST))
{
    foreach($_POST['goal'] as $k => $v)
    {
        $this_goal_number = $k + 1;
        echo '<p>Goal # ' . $this_goal_number . ' is ' . $v . '</p>';
    }
}
?&gt;
&lt;form action="" method="post"&gt;
    &lt;input type="hidden" name="goal[]" value="abc" /&gt;
    &lt;input type="hidden" name="goal[]" value="xyz" /&gt;
    &lt;input type="submit" value="submit" /&gt;
&lt;/form&gt;
#3

[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)
    {
        $posts = $this->_fetch_from_array($_POST, $index, $xss_clean);
        foreach($posts as $key => $value){
            $posts[$key] = $this->post($key);
        }
        return $posts;
    }

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.
#4

[eluser]pickupman[/eluser]
That's because you keep writing to the array you are iterating through.
Code:
function posts($index = '', $xss_clean = FALSE)
    {
        $posts = $this->_fetch_from_array($_POST, $index, $xss_clean);
        $post = array();
        foreach($posts as $key => $value){
            $post[$key] = $this->post($key);
        }
        return $post;
    }

//Not quite sure what you've got going on with this. But this could be done easier with
$data['goals'] = $this->input->post('goals'); //Get contents of input
$data['goals'] = implode(";",$data['goals']); //Convert array to delimited string
$this->session->set_userdata('goals',$data['goals']); //Save in users session

//Later on access via
$data['goals'] = explode(";",$this->session->userdata('goals'));
#5

[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?
#6

[eluser]pickupman[/eluser]
Ah...maybe try
Code:
$this->input->post('goals[]');
#7

[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.
#8

[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!




Theme © iAndrew 2016 - Forum software by © MyBB