Welcome Guest, Not a member yet? Register   Sign In
Array of fields with only a number as there id
#1

[eluser]brucebat[/eluser]
Hi all,

I have a problem for you to wrap your brains around.

How can you tell the differene between a row of fields which all have the same html name tag.

Image shown below
[Image: lhDcs.jpg]

This was cceated using a for loop of 14 times and within it using the form_helper functions.

Code:
for ($i =0; $i< 15; $i++)
                {
            
                    
                    //time part of event field
                    echo form_input ($i),
                    //the event itself
                    form_input ($i),
                    //supplies used
                    form_input ($i),
                    //successful?
                    form_dropdown ($i, $success),
                    //comment if necessary
                    form_input ($i);
                    echo '<br/>';
                }


I thought about concatenating them:

e.g.
Code:
form_input ("time".$i);

To give a unique name for each field within a single row but I dont know if that would work using a forloop later on in my validation controller?

How will i be able to distinguish so I know that this is e.g.

"supply field no 15 out of 30 etc?"

Is there another variable I can put in form_input function?

Thanks
#2

[eluser]ontguy[/eluser]
You could dynamically add validation for the dynamic fields.
Code:
$this->form_validation->set_rules('time'.$i, 'Username', 'required');
#3

[eluser]brucebat[/eluser]
How would form_validation know that?

Because in the view it will be rendered as "time1, time2, time3" and will not be recognized as 'time'.$i etc.

So when it goes to post it will be time1,time2,time3 ?
#4

[eluser]brucebat[/eluser]
I have a possible idea,

What if i was to have an actualy variable like $i which was an array populated by my fields (time,event, supplies, success, comment) and just record the total index value and then in my validation controller pass them all through in a for loop of $i?
#5

[eluser]ontguy[/eluser]
[quote author="brucebat" date="1309482971"]How would form_validation know that?

Because in the view it will be rendered as "time1, time2, time3" and will not be recognized as 'time'.$i etc.

So when it goes to post it will be time1,time2,time3 ?[/quote]

In your controller would the stay loop as you'll do in the view.
Code:
//in your controller
for ($i =0; $i< 15; $i++)
{
  $this->form_validation->set_rules('time'.$i, 'Time', '');            
  :
}

if ($this->form_validation->run() == FALSE)
{

//in your view
for ($i =0; $i< 15; $i++)
{
  
  
  //time part of event field
  echo form_input ($i),
  //the event itself
  form_input ($i),
  //supplies used
  form_input ($i),
  //successful?
  form_dropdown ($i, $success),
  //comment if necessary
  form_input ($i);
  echo '<br/>';
}
#6

[eluser]brucebat[/eluser]
Still working on it!

Thanks ontguy
#7

[eluser]brucebat[/eluser]
Nvm

Ignore this. sorry




Theme © iAndrew 2016 - Forum software by © MyBB