0

I have a database which consists of these two tables.

Clubs

-id* primary key

-name

Course -id

-clubid* foreign key

-name

======================================HTML=======================================

On my webpage I have a form which allows a user to register a club which by default starts with one course and allows them to click 'add course' this works by cloning the input field and changing the name.

So on submit, their maybe one, two or more courses that need to be added to my database

======================================PHP=========================================

So far I have been just allowing one course per club but there needs to be a one to many relationship.

    if($_POST){
        if(isset($_POST['register'])){
            $clubid = $clubs->addClub($_POST['golfclub'], $_POST['email'],$_POST['password1'], $_POST['password1'],date('day',time()));
            $clubs->addClubCourse($clubid, $_POST['course1']);
            header('Location: ./index.php');
    }
}

The addClubCourse uses the most recently added id in the club table to enter the course at this point. How can I do an iteration to add say,

$_POST['course1'], $_POST['course2']) .... etc without knowing how many courses their will be. 

====SQL===== Simply inserts the new course with the club id relevant to the registration.

Thanks for the help

3
  • for ($i = 1; $i < $limit; $i++) { addClubCourse($_POST["course$i"}); } Commented Aug 11, 2014 at 19:55
  • oh, the easiest way would be to simply use a counter to find the limit thanks Commented Aug 11, 2014 at 19:56
  • if that's the only "repeated" field you have, you could just use name="course[]" and PHP will put all the values into a sub-array in $_POST['course']. easy to loop on that with a simple foreach(). Commented Aug 11, 2014 at 19:59

2 Answers 2

2

Name your html field so it becomes an array:

<input name="course[]">

etc.

Then, you can do:

foreach($_POST['course'] as $course) {
    $clubs->addClubCourse($clubid, $course);
}
Sign up to request clarification or add additional context in comments.

3 Comments

var clone = $('#course1').clone(false)[0].outerHTML.replace(/1/g, counter).replace(/course1/,"course"+counter); I'm currently using this line to assign the new name to each html field, is it possible to add the square brackets in here to make it an array ?
Can you just name the field itself "course[]" and then you can just do var clone = $('#course1').clone()? No need to change anything about the html in this case.
Thanks, yeah that worked perfectly, never knew you could manipulate a name in that way, thanks for that.
0

Or iterate through $_POST but validate also:

  foreach($_POST as $key => $value){

    if(preg_match('/^course\d+$/', $key)){
         $courses[$key] = $value;
    }
  }

The array input is a more elegant solution though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.