Welcome Guest, Not a member yet? Register   Sign In
Use same field multiple times?
#1

[eluser]Joakim_[/eluser]
Once again a newbie question and it might more be a PHP or SQL question.


Is it possible to use same field multiple times? I have a class called line, and when the user clicks on add new line link he gets up a new line class which use the same fields but with different ID's.

Do I have to manually add these fields in the database, such as firstname1, firstname2 etc. or is there any way to do it automatically?

Code:
<div class="line">
<p>&lt;input type="text" name="firstname1" value="" /&gt;</p>
<p>&lt;input type="text" name="lastname1" value="" /&gt;</p>
<p>&lt;input type="text" name="email1" value="" /&gt;</p>
</div>

&lt;a href="#" onclick="Some javascript" id="new_line"&gt;Add New Line</a>

Any help are very appreciated. Thanks! Smile
#2

[eluser]marcoss[/eluser]
It is quite simple:

Code:
<div class="line">
<p>&lt;input type="text" name="lines[1][firstname]" value="" /&gt;</p>
<p>&lt;input type="text" name="lines[1][lastname]" value="" /&gt;</p>
<p>&lt;input type="text" name="lines[1][email]" value="" /&gt;</p>
</div>

now, when you add a new line, just increment the line ID (lines[2] instead of lines[1] and you will get this:

Code:
<div class="line">
<p>&lt;input type="text" name="lines[2][firstname]" value="" /&gt;</p>
<p>&lt;input type="text" name="lines[2][lastname]" value="" /&gt;</p>
<p>&lt;input type="text" name="lines[2][email]" value="" /&gt;</p>
</div>

Finally, when you process it, just loop the lines array,

Code:
&lt;?php
if(isset($_POST['lines'])) :
    foreach ($_POST['lines'] as $line) :
        //do something with the $line array
    endforeach;
endif;
?&gt;
#3

[eluser]Joakim_[/eluser]
Thank you very much for your help.

I get an error when I try to submit the form:

Message: Array to string conversion

and

Unknown column 'Array' in 'field list'

The Form looks like this:

Code:
&lt;form action="items/new" method="post"&gt;

<div class="line">
<p>&lt;input type="text" name="lines[1][firstname]" value="" /&gt;</p>
<p>&lt;input type="text" name="lines[1][lastname]" value="" /&gt;</p>
<p>&lt;input type="text" name="lines[1][email]" value="" /&gt;</p>
</div>

&lt;input type="submit" name="" value="Add New Item"  /&gt;

&lt;/form&gt;

The items/new function looks like this:

Code:
$this->db->insert('items', $_POST);
        
redirect('items');

Shall I add the if(isset($_POST['lines']... code into the items/new function?


I have a row in the database called lines. I assume all details (firstname, lastname and email) are stored in the same database row? How can I then display these details?

Sorry for all stupid questions, I'm new into both PHP and CL. Smile

Thanks a lot!
#4

[eluser]dbashyal[/eluser]
how about this.

if the form always have lines[1][...]

Code:
if(isset($_POST))
$this->db->insert('items', $_POST['lines'][1]);

if more fields like
lines[1][...]
lines[2][...]

Code:
if(isset($_POST) && is_array($_POST['lines'])) foreach ($_POST['lines'] as $v)
   $this->db->insert('items', $v);

i haven't tested though.
#5

[eluser]Joakim_[/eluser]
Thank you very much for your reply.

Shall I have firstname, lastname and email also as fields in the database? Or is it enough with "lines"?
#6

[eluser]dbashyal[/eluser]
http://ellislab.com/codeigniter/user-gui...ecord.html
says:
that code produces
INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date')

so i guess you need those fields in database. plz refer doc. i haven't gone through yet.




Theme © iAndrew 2016 - Forum software by © MyBB