Page 1 of 1
call function for each element
Posted: Mon May 25, 2009 8:43 pm
by invisibled
Ok so, i have a signup form. Users sign up for the events and submit the form and the results go to the database. The problem is, they can signup for multiple events in the same form. I want to write some code that will insert a new row for each event they signup for. for example
john smith,
john@smith.com, event 1
john smith,
john@smith.com, event 2
here is my php
Code: Select all
function insert(){
extract($_POST);
mysql_query("INSERT INTO module_novemberAttendees VALUES(
'',
'$username',
'$school',
'$phone',
'$email',
'$event_id',
'$attend'
)");
}
if(isset($_POST['username'])):
insert();
endif;
and you can view an example of the form at
http://invisibled.com/dev/fnaa/events (click on add this event to your list). All suggestions nad tips are welcome!
Re: call function for each element
Posted: Tue Jun 09, 2009 2:32 pm
by invisibled
bumpity bump

Re: call function for each element
Posted: Tue Jun 09, 2009 2:37 pm
by Raph
Your page is very confusing, and I couldn't find the form for the signup, so I'll just assume you save the POST-data in an array. What you then have to do is to simply loop through the array when inserting them into your DB.
Re: call function for each element
Posted: Tue Jun 09, 2009 4:56 pm
by mischievous
Umm... im with Raph, the page is very confusing... need more contrasting definitions to know where the elements of the page are laying... as for the form I couldn't find it either...
However, you will most likely have the events in an array
Soo... setup a for loop:
Code: Select all
for($i = 0; $i <= count($array)-1; $i ++){
mysql_query("INSERT INTO module_novemberAttendees VALUES(
'',
'$username',
'$school',
'$phone',
'$email',
'$event_id[$i]',
'$attend'
)");
}
Re: call function for each element
Posted: Tue Jun 09, 2009 8:15 pm
by omniuni
Holy Diety. Please consider making your page a bit less confusing! I too could not find the form, and most of the fonts were cut off in odd places. It took an accident for me to find the navigation.
Anyway, if you have an array with each event that you want to add, you can try a foreach loop:
Code: Select all
$myevetsarray; //this has each event as an element
foreach($myeventsarray as $event){
//do your database call to add a row
}
btw, this is nearly the same as mischievous's post, just using a foreach instead of a for loop with a count. Which one to go with is more a matter of style than anything else.
Re: call function for each element
Posted: Wed Jun 10, 2009 5:30 pm
by invisibled
sorry guys. the form is on
http://invisibled.com/dev/fnaa/events_studio_day and the site is suppose to look cut off, there may have been some browser inconsistencies before but this is the site.
click on "add this event to your list" and you will see the event get add'ed.
I get the part of having the foreach, but then what? like, how do i tell php that "foreach instance of an added event, insert a row with the main details and that events number of signed up users". Does that make sence?
each time you add an event to the sign up form, it adds the "module" and the number of people attending has its own id, otherwise when you post it, it just takes the last instance of that name posted.
Re: call function for each element
Posted: Wed Jun 10, 2009 6:22 pm
by invisibled
=====UPDATE====
i have got it almost working with this code.
Code: Select all
function insert(){
extract($_POST);
foreach($_POST['attend'] as $k => $v){
mysql_query("INSERT INTO module_novemberAttendees VALUES(
'',
'$username',
'$school',
'$phone',
'$email',
'$event_id',
'$v'
)");
}//foreach
}//insert
if(isset($_POST['username'])):
insert();
print "<h4>Your information has been stored. Thank you!</h4>";
endif;
?>
i made the names of the elemets name="attend[]" and name="event_id[]" so i can grab attend from the foreach, and it inserts as many rows as their are names, but now i need to do it with the event_id. i thought maybe using && in the foreach but it doesnt like that. Suggestions?
Re: call function for each element
Posted: Wed Jun 10, 2009 7:52 pm
by mischievous
I'm very confused as to what your doing there? you have a form and then the events are actually divs and not inside of any type of form element? are those then getting changed over to a hidden input with javascript somewhere?
Idk...

Re: call function for each element
Posted: Thu Jun 11, 2009 2:29 pm
by invisibled
there is an attendee's input when you add an event to the list. so that gets stored in the DB and the events id gets thrown into a hidden field to relate the data to an event.