Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm looking for some assistance with a php script I'm writing. It creates a table that is then exported to an excel file for download, the issues as I see them are as follows:

$students contains 559 items and the $classes has 22 items and $attributes has 6 items

<tbody>
    <?php foreach($students as $student){
    $pupil = Students::retrieveByPK($student['adno']); ?>
<tr>
    <td><?php echo strtoupper($pupil->surname); ?></td>
    <td><?php echo $pupil->first_name; ?></td>
    <td><?php echo $pupil->year_group; ?></td>
    <?php foreach($classes as $class){
        $shortYear = str_replace('Year ', '',$pupil->year_group);
        $slashPos = strpos($class['class_name'],'/');
        $className = $shortYear.substr($class['class_name'],1);
        $classId = Classes::idFromClassName($className); 
        foreach($attributes as $attribute){
           $attr = AttributeResults::getExportAttribute($pupil->adno, $classId, $attribute['attribute_id']); ?>
        <td align="center"><?php echo $attr[0]['attribute_value']; ?></td>
        <?php } ?>
     <?php } ?>
</tr>
    <?php } ?>
</tbody>

I'm essentially running the AttributeResults::getExportAttribute method in the region of 74000 times and wondered if there was a better way of bringing this data back?

Any assistance would be greatly appreciated.

share|improve this question
Depends, where is all that data coming from? Is each of your static class calls a database query? – deceze Jan 17 '12 at 9:25
Yes at the moment they are – timothystringer Jan 17 '12 at 9:33
2  
Then you should use one big SQL query to get the data instead of 74000 tiny ones. – deceze Jan 17 '12 at 9:34
I think I still need to run for each of the $classes as the next level of info ($attributes) comes specifically from the id's I get in the $classes, so essentially you would suggest running 22 larger queries? – timothystringer Jan 17 '12 at 9:37

migrated from stackoverflow.com Jan 17 '12 at 13:10

1 Answer

I think what your better off doing is to have a join on your database call, so that the class data and student data is returned in one object.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.