Cecatrix Cecatrix -3 years ago 80
HTML Question

PHP, Codeigniter: Foreach Should Echo Multiple Rows when accessed in Controller

Hi guys I will summarize my problem here I hope you would understand.

Main Goal: To have an on change event that will depend on my selected values in month that will access the controller and echo multiple rows or result.

Already Accomplished: I have an on change event in my multiple select that will trigger my controller and access the rows.

The On change event:

$('#month').change(function() {
var values = $(this).val(); // returns array

// loop over values
values.forEach(function(val){

if (val == 1)
{
$("#nomonthschoolfees").load(baseUrl+'/admin/summary/noMonthSchoolFees/'+studentId+'/'+schoolyearId);
// This will access my controller if 1 is selected in multiple select.
}
else if (val == 2)
{

}
});
});


Main Problems:

Problem 1: My 1st problem is, when it is echoing a row, the results of the datas are outside the select option box.

Problem 2: As I've tried my query in mysql, it should show at least 2 rows, but it is only showing/echoing 1 result of the foreach inside my controller.

Screenshot of the forms and problem:
enter image description here

Main Code (Controller):

public function noMonthSchoolFees($studentId = null, $schoolyearId = null)
{
if ($studentId && $schoolyearId) {
$studentfeesData = $this->model_feestudent->noMonthSchoolFees($studentId, $schoolyearId);

if ($studentfeesData) {
$arrayNumber = 0;
foreach($studentfeesData as $x => $studentfees) {
$rows = '
<tr id="row'.$x.'" class="'.$arrayNumber.'">
<td class="form-group">
<select style="width: 400px; text-align-last:center;" class="form-control" name="subparticulars['.$x.']" id="subparticulars'.$x.'" readonly/>
<option value="'.$studentfees['feetype_id'].'"> '.$studentfees['feetype_name'].' </option>
</select>
</td>
<td class="form-group">
<input type="text" class="form-control" name="subpaymentbalance[<?php echo $x; ?>]" id="subpaymentbalance<?php echo $x; ?>" onclick="copyBalance('.$x.')" style="text-align:center;" value="'.$studentfees['feestudent_amount'].'" readonly />
</td>
</tr>';
$arrayNumber++;
}// end of foreach

}// end of if ($studentfeesData)
} // end of if ($studentId && $schoolyearId)

echo $rows; // echo the results of rows.

}// end of function

Answer Source

You are overwritting $rows each loop.

replace

...
$rows = '
    <tr id="row'.$x.'" class="'.$arrayNumber.'">
...

with

public function noMonthSchoolFees($studentId = null, $schoolyearId = null)
{
    $rows = '';

    ...

    $rows .= '
        <tr id="row'.$x.'" class="'.$arrayNumber.'">
    ...

Also fix the closing tag (look at backslash on the end):

<select style="width: 400px; text-align-last:center;" class="form-control" name="subparticulars['.$x.']" id="subparticulars'.$x.'" readonly/>

with

<select style="width: 400px; text-align-last:center;" class="form-control" name="subparticulars['.$x.']" id="subparticulars'.$x.'" readonly>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download