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:

    <?php foreach($students as $student){
    $pupil = Students::retrieveByPK($student['adno']); ?>
    <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 } ?>
    <?php } ?>

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

  • \$\begingroup\$ Depends, where is all that data coming from? Is each of your static class calls a database query? \$\endgroup\$
    – deceze
    Commented Jan 17, 2012 at 9:25
  • \$\begingroup\$ Yes at the moment they are \$\endgroup\$ Commented Jan 17, 2012 at 9:33
  • 2
    \$\begingroup\$ Then you should use one big SQL query to get the data instead of 74000 tiny ones. \$\endgroup\$
    – deceze
    Commented Jan 17, 2012 at 9:34
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Jan 17, 2012 at 9:37

1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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