I'm trying to do some loops in loop using mysql but it seems that there not interpreted
it returns only the first result then nothing even If I have many records in my database, It should return everything
below is my code that display the results
while ($cr = mysql_fetch_assoc($result)):
$i = 0;
++$i;
?>
<div style="width:200px" class="fif"><table style="width:100%">
<tr class="fbgreybox">
<td colspan="3" style="text-align: center;font-weight: bold">
<img src="images/calendar_2.png"> Séance du : <?php echo date('d-m-Y', strtotime($cr['record'])); ?>
</td>
</tr>
<?php
$sql = "SELECT `exercise` FROM `workouts` WHERE `record` = '{$cr['record']}' AND `user`= {$_SESSION['userid']} GROUP BY `exercise`";
$result = mysql_query($sql);
while ($exo = mysql_fetch_assoc($result)) :
?>
<tr class="fbinfobox">
<td colspan="3" style="text-align: left">
<img src="images/Sport-dumbbell.png"> ////<?php echo exerciseName($exo['exercise']); ?>
</td>
</tr>
<tr>
<td style="text-align: center;font-weight: bold">Séries</td>
<td style="text-align: center;font-weight: bold">Reps</td>
<td style="text-align: center;font-weight: bold">Poids</td>
</tr>
<?php
$rqt = "SELECT `set_number`, `reps`, `weight` FROM `workouts` WHERE `exercise` = {$exo['exercise']} AND `record` = '{$cr['record']}' AND `user` = {$_SESSION['userid']} ORDER BY `set_number`";
$result2 = mysql_query($rqt);
while ($detail = mysql_fetch_assoc($result2)):
?>
<tr>
<td>
Série ////<?php echo $detail['set_number']; ?>
</td>
<td>
<?php echo $detail['reps']; ?>
</td>
<td>
<?php echo $detail['weight']; ?>
</td>
</tr>
<?php
endwhile;
endwhile;
?>
</table>
</div>
<?php
endwhile;
there is not mistakes in the code so I really do not understand why it is not interpreted.
anykind of help will be much appreciated.
You are using $result in both mysql_fetch_assoc() function. AVOID THAT.
In inner loop you can use another variable:
$sql = "SELECT `exercise` FROM `workouts` WHERE `record` = '{$cr['record']}' AND `user`= {$_SESSION['userid']} GROUP BY `exercise`";
$workoutsResult = mysql_query($sql);
while ($exo = mysql_fetch_assoc($workoutsResult )) :
If I were you, I would first loop through the first query and put it in an array. Then you loop through this array, and search for the inner queries and also put in arrays inside the other array.
Then, you loop through them to build your table.
It'll make your code cleaner, and will avoid problems like the one you're having, the one @Suresh pointed out.