1

I am using Laravel Framework to do this PHP app.

I am parsing an XML from an API and the information I get is structured like this:

projectName

projectJobs- a list with jobs that I am outputting with a @foreach into a table (<td>);

I want to let the user group the jobs as he wishes. I was thinking of doing this:

@foreach($_SESSION['workingProjects'] as $projectName => $projectDetails)
    <tr class="success">
        <td>{{$projectDetails['givenName']}} <p class="text-muted" style="font-size: 0.7em">( {{ $projectName }} )</p></td>
        <td></td>
        <td></td>
        @foreach($projectDetails['jobs'] as $jobName => $jobUrl)
           <tr>
               <td>{{$jobName}}</td>
               <td>
                   <select>
                       <option name="group[1][{{$projectDetails['givenName']}}][{{$jobName}}]" value="1">
                           1
                       </option>
                       <option name="group[2][{{$projectDetails['givenName']}}][{{$jobName}}]" value="2">
                           2
                       </option>
                       <option name="group[3][{{$projectDetails['givenName']}}][{{$jobName}}]" value="3">
                           3
                       </option>
                       <option name="group[4][{{$projectDetails['givenName']}}][{{$jobName}}]" value="4">
                           4
                       </option>
                       <option name="group[5][{{$projectDetails['givenName']}}][{{$jobName}}]" value="5">
                           5
                        </option>
                     </select>
                  </td>
                  <td>
                      <input type="checkbox" name="k1">k1</input>
                      <input type="checkbox" name="k2">k2</input>
                      <input type="checkbox" name="k3">k3</input>
                      <input type="checkbox" name="k4">k4</input>
                      <input type="checkbox" name="k5">k5</input>
                   </td>
               </tr>
           @endforeach
       @endforeach
   </tbody>

Having more projects, I need to have a list of projectNames which each have a list of projectJobs grouped by the select in 1-5 range.

Example:

PROJECT1 => [ GROUP1 => [ JOB1, JOB2, JOB5 ] GROUP2 => [ JOB3, JOB4 ] ];

The problem is that I only get the crsf_token() posted and I don't think this is the proper way to do it anyway.

Clarification:

I have a list of projects, each project has many jobs, each job needs to be grouped by the user in one of groups. I need to get the user grouping back to the applicaton.

For example User1 has 3 Projects : P1, P2, P3; each project has it's own Jobs: J1_1, J1_2, J1_3, J1-4, J1_5, J2_1, J2_2, etc.

I want to enable the user to group them as he wishes and send that info to me. For example for P1, the User groups the jobs like this: Group1: J1_2, J1_3, Group2: J1_1, J1_4, Group3: J1_5

Alexandru Antochi
  • 1,295
  • 3
  • 18
  • 42
  • You have some issues on your html. You are opening the `` at the beginning an never closing it. The input field [checkbox](https://www.w3schools.com/tags/att_input_checked.asp) doesn't have a closing tag. Can you give more details about the expected result and what's exactly the problem, please? – borracciaBlu Apr 13 '17 at 00:38
  • So I have a list of projects, each project has many jobs, each job needs to be grouped by the user in one of groups. I need to get the user grouping back to the applicaton. – Alexandru Antochi Apr 13 '17 at 07:44

1 Answers1

0

I think that as approach is reasonable, just clean up the html and it should work fine with all the details in the Request $request object.

<form role="form" method="POST" action="{{ url('/projects/') }}">
    {{ csrf_field() }}

    <table>
        @foreach($_SESSION['workingProjects'] as $projectName => $projectDetails)
            <tr class="success">
                <td>{{$projectDetails['givenName']}} <p class="text-muted" style="font-size: 0.7em">( {{ $projectName }} )</p></td>

                <td></td>
                <td></td>
            </tr>

            @foreach($projectDetails['jobs'] as $jobName => $jobUrl)
                <tr>
                   <td>{{$jobName}}</td>
                   <td>
                       <select name="job[{{$projectDetails['givenName']}}][{{$jobName}}]">
                           <option value="1">
                               Group 1
                           </option>
                           <option value="2">
                               Group 2
                           </option>
                           <option value="3">
                               Group 3
                           </option>
                           <option value="4">
                               Group 4
                           </option>
                           <option value="5">
                               Group 5
                            </option>
                        </select>
                  </td>
                  <td>
                      <input type="checkbox" name="k1" value="k1">k1
                      <input type="checkbox" name="k2" value="k2">k2
                      <input type="checkbox" name="k3" value="k3">k3
                      <input type="checkbox" name="k4" value="k4">k4
                      <input type="checkbox" name="k5" value="k5">k5
                   </td>
               </tr>
           @endforeach
       @endforeach
    </table>

    <button type="submit">
        Submit
    </button>

</form>

Just be careful with the html:

  • for a select tag the syntax is <select name=""> and in option you have the value <option value="3">.
  • for a input you need to define the name and the value in the tag.
borracciaBlu
  • 4,017
  • 3
  • 33
  • 41
  • So can I use arrays of arrays such as name="name[value1][value2][value3]" when sending information back to the application through a POST? – Alexandru Antochi Apr 13 '17 at 09:42
  • yes, you can :). if you are using a checkbox just remember to add an additional empty []. Like name="name[value1][value2][value3][]" because you can have multiple values. – borracciaBlu Apr 13 '17 at 09:43
  • read here http://stackoverflow.com/questions/1978781/how-to-post-multiple-input-type-checkbox-as-array-in-php – borracciaBlu Apr 13 '17 at 09:45