Click here to Skip to main content
14,875,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to PHP. I am responsible to develop a system to register trainings. A delegate can attend one or more training(s). I am using a twig template for views.

Capture the delegate fields
a. Delegate count (number)
b. Municipality
c. A switch for “is billable”
d. Amount billed
e. Click the add button to include another line item then start at A for the new line item. Rinse repeat till done. Then Click save to save the items.

What I have tried:

----Javascript code----

JavaScript
var delegates = new Array();

function addDelegate(){
    
    // var course = document.getElementById('course_id').options[e.selectedIndex].text;
    var course = document.getElementById("course_id");
    var courseName = course.options[course.selectedIndex].text;
    var courseValue = course.options[course.selectedIndex].value;
    var accountNo = document.getElementById('account_no').value;
    var municipality = document.getElementById('municipality_id');
    var municipalityName = municipality.options[municipality.selectedIndex].text;
    var municipalityValue = municipality.options[municipality.selectedIndex].value;
    var billableAmount = document.getElementById('amount_billed').value;
    
    var delegate = {
        course: courseName,
        courseVal: courseValue,
        accountNo: accountNo,
        municipality: municipalityName,
        billableAmount: billableAmount,
        municipalityVal: municipalityValue,

    };

    

    delegates.push(delegate);
    var table = document.getElementById('tableDelegate');
    var tr = document.createElement('tr');
    tr.innerHTML = '<td>' + '<input type="text"' + 'class="form-control"' + ' id="course_id1" ' + 'name="course_id1"' +
    'value='+  delegate.course + '>' + '</td>' +
    '<td>' + '<input type="text"' + 'class="form-control"' + ' id="account_no1" ' + 'name="account_no1"' +
    'value='+  delegate.accountNo + '>' + '</td>' +
    '<td>' + '<input type="text"' + 'class="form-control"' + ' id="municipality_id1" ' + 'name="municipality_id1"' +
    'value='+  delegate.municipality + '>' + '</td>'+
    '<td>' + '<input type="text"' + 'class="form-control"' + ' id="amount_billed1" ' + 'name="amount_billed1"' +
    'value='+  delegate.billableAmount + '>' + '</td>';
    table.appendChild(tr);
}


----HTML/TWIG Code----

HTML
<div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Delegate</h5>
        
            <span>×</span>
        
      </div>
      <div class="modal-body">

    <div class="container">
        <div class="row">
            <div class="col">
                
                    Details

                    <div class="col-12">
                        Course:
                        
                            None
                            <% for (var i in this.courses_list){ %>
                            selected<% } %>><%= this.courses_list[i].course_name %>
                            <% } %>
                          
                    </div>
                    <div class="form-group mb-3">
                        
                        Account No:
                        
                        <small class="form-text text-muted">Enter the Account No</small>
                    </div>

                    <div class="col-12">
                        Municipality:
                        
                            None
                            <% for (var i in this.municipalities){ %>
                            selected<% } %>><%= this.municipalities[i].municipality_name %>
                            <% } %>
                          
                    </div>
                    <div class="form-group mb-3">
                        Is billable:
                          
                    </div>
                    <div class="ps-3">
                        <div class="form-check border  mb-1 py-1  delegate-item">
                            checked<% } %>>
                                
                                No
                                
                        </div>
                        <div class="form-check border  mb-1 py-1  delegate-item">
                            checked<% } %>>
                                
                                Yes
                                
                        </div>
                    </div>

            

                    <div id="divAmont" class="form-group mb-3 <% if (this.details.is_billable=='1'){ %>show<% } %>">
                        Billable Amount:
                        
                        <small class="form-text text-muted">Enter the Billable Amount</small>
                    </div>
                    
                

                <div class="container">
                    <div class="row">
                        <div class="col">
                            
                                
                            
                        </div>
                    </div>
                    <div id="toolbar-spacer"></div>
                    <div class="row">
                        <div class="col">
                             
                                                                     
                                <table class="table records table-sm  table-hover" name="tableDelegate[]" id="tableDelegate"><thead>                                    <tr><th>Course</th>                                    <th>Account No</th>                                    <th>Municipality</th>                                    <th>Billable Ammount</th>                                    <th></th>                                </tr></thead>                                <tbody></tbody>                            </table>
                        </div>
                    </div>
                </div>

            
                
            
            </div>
            <div class="modal-footer">
                <div class="container">
                    <div class="row">
                        <div class="col">
                            
                                
                            
                        </div>
                        <div class="col text-end d-flex justify-content-between">
                            
                                Cancel
                            
                            
                                Save
                            
                        </div>
                    </div>
                </div>
            </div>
            </div>
            </div>

----controller code----
PHP
system->get("REQUEST.search");

        switch ($this->system->get("VERB")) {
        case "POST":
            $data = $this->_save($data);
            break;
        case "DELETE":
            $data = $this->_delete($data);
            break;
        default:
            if ($data['id']) {
                $data = $this->_details($data);
            } else {
                $data = $this->_list($data);
            }
            break;
        }

        $this->render("delegates\\page.twig", $data, true);
        $profiler->stop();
    }
    function _list($return) {

        $where = "1";
        $params = array();

        if ($return['search']) {
            $where .= " AND (delegates.account_no LIKE :SEARCH)";
            $params[':SEARCH'] = "%" . $return['search'] . "%";
        }
        $list_data = (new delegateModel())
            ->_order("delegates.account_no ASC")
            ->_where($where, $params)
            ->getAll()->toSchema(new delegatesListSchema())
        ;
        
        // System::debug($list_data);
        $return['list'] = $list_data;

        return $return;
    }
    function _details($return) {

        $details = (new delegateModel())->get($return['id']);

        $return['details'] = $details->toSchema(new delegatesDetailsSchema());

        
        $courses_list = (new CourseModel())
            ->_order("courses_list.name ASC")
            ->getAll()
            ->toSchema((new CourseListSchema())->selected($return['details']['course_id']))
        ;

        $return['courses_list'] = $courses_list;


        $municipalities = (new municipalityModel())
            ->_order("municipalities.name ASC")
            ->getAll()
            ->toSchema((new municipalitiesListSchema())->selected($return['details']['municipality_id']))
        ;

        //System::debug($municipalities);
        $return['municipalities'] = $municipalities;

        
        return $return;
    }

    function _save($return) {

        // System::debug($_POST);

        $values = array(
            
            "tableDelegates"=>(array)$this->system->get("POST.tableDelegate[]")
        );

        if ($values['is_billable']!= '1'){
            $values['is_billable'] = '0';
            $values['amount_billed'] = '0';
        }
        //System::debug($values);
        $details = (new delegateModel())->get($return['id']);
        $return['errors'] = $details->validate($values);

        if (empty($return['errors'])) {
            $id = $details->save($values);
            $return['id'] = $id;
        }
        return $return;
    }
    function _delete($return) {
        $details = (new delegateModel())->get($return['id']);

        $details->delete();

        return $return;
    }

}
Posted
Updated 29-Mar-21 1:40am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900