Q:

While loop inside for loop not working while fetching mysql rows

<? php 
echo '<table>';
for($i=1;$i<=12;$i++)
{               
echo '<tr>';
while($row5=mysql_fetch_array($result5))
{
if($row5[3]=='monthly')
echo '<td>'.$row5[3].'</td>';
else if($row5[3]=='quarterly')
echo '<td rowspan="3">'.$row5[3].'</td>';
else if($row5[3]=='halfyearly')
echo '<td rowspan=""="6">'.$row5[3].'</td>';
else 
echo '<td rowspan="12">'.$row5[3].'</td>';
}
echo '</tr>';
}
echo '</table>';
? >

This code is printing only one row instead of 12 rows.  Please help me.  I am doing this for managing student fees.  I am stuck at the logic. 

A:

Create an array with sql result before :
$data = array();

while( $row5 = mysql_fetch_array($result5) )
    $data[] = $row5;

Then replace this : while($row5=mysql_fetch_array($result5))
foreach ( $data as $row5 ) {
    if($row5[3]=='monthly')
        echo '<td>'.$row5[3].'</td>';

    // ...
}

PS : Use mysqli_* instead of mysql_* which is deprecated

