Creating a table for use in Excel 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:

&lt;tbody&gt;
    &lt;?php foreach($students as $student){
    $pupil = Students::retrieveByPK($student['adno']); ?&gt;
&lt;tr&gt;
    &lt;td&gt;&lt;?php echo strtoupper($pupil-&gt;surname); ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $pupil-&gt;first_name; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $pupil-&gt;year_group; ?&gt;&lt;/td&gt;
    &lt;?php foreach($classes as $class){
        $shortYear = str_replace('Year ', '',$pupil-&gt;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-&gt;adno, $classId, $attribute['attribute_id']); ?&gt;
        &lt;td align="center"&gt;&lt;?php echo $attr[0]['attribute_value']; ?&gt;&lt;/td&gt;
        &lt;?php } ?&gt;
     &lt;?php } ?&gt;
&lt;/tr&gt;
    &lt;?php } ?&gt;
&lt;/tbody&gt;


I'm essentially running the AttributeResults::getExportAttribute method in the region of 74000 times. Is there a better way of bringing this data back?
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.