OK, so I have the following code that reads a csv file that than outputs the result in the form of a HTML table:
$fhdrop = fopen("ac.csv", "r");
while (!feof($fhdrop) ) {
$at[] = fgetcsv($fhdrop, 1024);
}
$fhdrop2 = fopen("rc.csv", "r");
while (!feof($fhdrop2) ) {
$at2[] = fgetcsv($fhdrop2, 1024);
}
?>
<table border=1>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<tr>
<td><?php echo $at[0][0] ?></td>
<td><?php echo $at[0][1] ?></td>
<td><?php echo number_format($at[0][1]*$at2[0][1],2) ?></td>
<td><?php echo number_format($at[0][1]*$at2[1][1],2) ?></td>
</tr>
<tr>
<td><?php echo $at[1][0] ?></td>
<td><?php echo $at[1][1] ?></td>
<td><?php echo number_format($at[1][1]*$at2[0][1],2) ?></td>
<td><?php echo number_format($at[1][1]*$at2[1][1],2) ?></td>
</tr>
<tr>
<td><?php echo $at[2][0] ?></td>
<td><?php echo $at[2][1] ?></td>
<td><?php echo number_format($at[2][1]*$at2[0][1],2) ?></td>
<td><?php echo number_format($at[2][1]*$at2[1][1],2) ?></td>
</tr>
<tr>
<td><?php echo $at[3][0] ?></td>
<td><?php echo $at[3][1] ?></td>
<td><?php echo number_format($at[3][1]*$at2[0][1],2) ?></td>
<td><?php echo number_format($at[3][1]*$at2[1][1],2) ?></td>
Wag1 pplz!!! This account is mine now!!!
The contents of ac.csv are:
a, 5,
b, 10,
c, 24,
d, 21
The contents of rc.csv are:
not, 1.87,
notatall, 1.78
As you can guess from the original code, in the second to columns I need a multiplication. So for example in the second row, third coloumn I need 5*1.87 and 5*1.78. So basically a*not and a*notatall.
My code is not very efficient, how can I make it more efficient? I want all of these (somehow) put into a loop. It needs to have the exact same outcome, due to the complexity and the fact that it has 2d arrays, I have not yet been able to do this!
My only suggestion would be this one
<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
<?php
$fhdrop2 = fopen("rc.csv", "r");
while (!feof($fhdrop2) ) {
$at2[] = fgetcsv($fhdrop2, 1024);
}
$fhdrop = fopen("ac.csv", "r");
while (!feof($fhdrop) ){
$at = fgetcsv($fhdrop, 1024);
?>
<tr>
<td><?php echo $at[0]; ?></td>
<td><?php echo $at[1]; ?></td>
<td><?php echo number_format($at[1]*$at2[0][1],2); ?></td>
<td><?php echo number_format($at[1]*$at2[1][1],2); ?></td>
</tr>
<?php
}
?>
</table>
This should not optimize in speed but it should save some memory for bigger ac.csv files, because you do not need to create a big array. But i do not think you could optimize speed here. I hope someone can provide a better answer.
EDIT #01 Here is little more flexible loop
<tr>
<td><?php echo $at[0] ?></td>
<td><?php echo $at[1] ?></td>
<?php for($i = 0; $i < sizeof($at2); $i++){ ?>
<td><?php echo number_format($at[1]*$at2[$i][1],2) ?></td>
<?php } ?>
</tr>
With this one your rc.csv can store more than 2 Values to multiply. Not an speed or memory optimization but you will save code and time if you try to scale your functions/loops.
EDIT #02 I tried scrowler's approach and would suggest this one because of the logic/mark-up separation. The side effects are more use of Memory and a slightly slower script speed but it isn't significant (the speed).
Hope this will help