I'm bringing in this array for datatables and putting it into an associative array. Before I was using datatables I was using straight PHP to create my table and could manipulate the indexed arrray, here's an example
<?php
foreach ($types as $val) {
setlocale(LC_MONETARY, 'en_US');
echo '<tr>';
echo '<td>' . $val[0] . '</td>';
echo '<td>' . $val[1] . '</td>';
echo '<td>' . $val[5] . '</td>';
echo '<td>' . number_format($val[2] * .01, 2) . '</td>';
echo '<td>' . $val[3] . '</td >';
echo '<td>' . money_format('%2n', $val[4] * .01) . '</td>';
switch ($val[5]) {
case 'S';
echo '<td>' . money_format('%2n', $val[3]) . '</td>';
break;
case 'U';
echo '<td>U</td>';
break;
case 'H':
$totalHourly = ($val[2] * .01) * ($val[4] * .01);
echo '<td>' . $totalHourly . '</td>';
break;
case 'M':
echo '<td>M</td>';
break;
default;
if ($val[2] > 0) {
$totalGross = ($val[2] * .01) * ($val[4] * .01);
echo '<td>' . money_format('%.2n', $totalGross) . '</td>';
} else {
echo '<td>' . money_format('%.2n', $val[3]) . '</td>';
}
}
echo '<td><a href=EmployeeInfo.php?empNum=' . $val[0] . '>Info</a></td>';
echo '<td><a href=EmployeePayroll.php?empNum=' . $val[0] . '>Payroll</a></td>';
echo '</tr>';
}
?>
Now I am using datatables and from the example am using an associative array. The reason I'm using datatables is for the hide and show chid row. Here is an example of my associative array.
while ($row = db2_fetch_array($stmt)) {
$load['data'][] = array(
'empNum' => $row[0],
'empName' => $row[1],
'unitRate' => $row[2],
'salary' => $row[3],
'hourly' => $row[4],
'appFlag' => $row[5],
'app1' => $row[6],
'app2' => $row[7],
'app3' => $row[8],
'app4' => $row[9],
'app5' => $row[10],
'uni1' => $row[11],
'uni2' => $row[12],
'uni3' => $row[13],
'uni4' => $row[14],
'uni5' => $row[15],
'gross' => ($row[2] * .01) * ($row[4] * .01)
);
}
The very last line is the simplest way I have found of manipulating this array. Is there a way I can apply the same logic to my associative array the same way I did my indexed array? And if not, what is the best way of adding logic and then creating an associative array?
This is what I did.
while ($row = db2_fetch_array($stmt)) {
$empNum = $row[0];
$empName = $row[1];
$unitRate = $row[2];
$salary = $row[3];
$hourly = $row[4];
$appFlag = $row[5];
$app1 = $row[6];
$app2 = $row[7];
$app3 = $row[8];
$app4 = $row[9];
$app5 = $row[10];
$uni1 = $row[11];
$uni2 = $row[12];
$uni3 = $row[13];
$uni4 = $row[14];
$uni5 = $row[15];
switch ($row[5]) {
case 'S';
$gross = $row[3];
break;
default;
if ($row[2] > 0) {
$gross = ($row[2] * .01) * ($row[4] * .01);
} else {
$gross = $row[3];
}
}
$load['data'][] = array(
'empNum' => $empNum,
'empName' => $empName,
'unitRate' => $unitRate,
'salary' => $salary,
'hourly' => $hourly,
'appFlag' => $appFlag,
'app1' => $app1,
'app2' => $app2,
'app3' => $app3,
'app4' => $app4,
'app5' => $app5,
'uni1' => $uni1,
'uni2' => $uni2,
'uni3' => $uni3,
'uni4' => $uni4,
'uni5' => $uni5,
'gross' => $gross
);
There's no reason to do it all in one shot. You can create your child array, manipulate it however you want, THEN stuff it into the parent array
$foo = array()
$foo['empnum'] = ...
...
$foo['gross'] = $foo['x'] * $foo['y'];
if ( ...) {
$foo['if_results'] = ... :
}
$data[] = $foo;
Note that you cannot embed if/switch inside the ... = array(...)
definition. if/switch are not function calls, they do not have return values, and therefore can be used in a context where they'd expected to be function calls.
You can use the foreach construct to go through your associative array and manipulate its content:
$data = array(
'empNum' => $row[0],
'empName' => $row[1],
'unitRate' => $row[2],
'salary' => $row[3],
'hourly' => $row[4],
'appFlag' => $row[5],
'app1' => $row[6],
'app2' => $row[7],
'app3' => $row[8],
'app4' => $row[9],
'app5' => $row[10],
'uni1' => $row[11],
'uni2' => $row[12],
'uni3' => $row[13],
'uni4' => $row[14],
'uni5' => $row[15],
'gross' => ($row[2] * .01) * ($row[4] * .01)
);
foreach($data as $key => $value) {
$data[$key] = 1; // add your complicated logic here
}
$load['data'][] = $data;
If I understand you well you are thinking you need to do the ifs and whiles directly in the array defenition. And that's not true. You can do it like this:
$count = 0;
while ($row = db2_fetch_array($stmt)) {
$load['data'][] = array(
'empNum' => $row[0],
'empName' => $row[1],
'unitRate' => $row[2],
'salary' => $row[3],
'hourly' => $row[4],
'appFlag' => $row[5],
'app1' => $row[6],
'app2' => $row[7],
'app3' => $row[8],
'app4' => $row[9],
'app5' => $row[10],
'uni1' => $row[11],
'uni2' => $row[12],
'uni3' => $row[13],
'uni4' => $row[14],
'uni5' => $row[15],
'gross' => ($row[2] * .01) * ($row[4] * .01)
);
if <condition> {
$load['data'][$count]['uni3'] = <whatever>;
$load['data'][$count]['gross'] = <whatever>;
} else {
$load['data'][$count]['uni5'] = <whatever>;
$load['data'][$count]['gross'] = <whatever2>;
}
<any other code you what>
$count++;
}