Zaynt Posted February 6, 2009 Share Posted February 6, 2009 I need replace the $n in cell 2 of the table with the the product of all the parent rows e.g row 5 will output the value 120 (1*2*3*4*5) anyone have any idea on how to solve this problem? <table border="1"> <?php $n = 0; while($n<25){ $n++; echo"<tr>"; echo"<td>Row number $n</td>"; echo" <td>$n</td>"; echo"</tr>"; } ?> </table> Link to comment https://forums.phpfreaks.com/topic/144070-solved-calculate-product-of-parent-rows-in-while-loop/ Share on other sites More sharing options...
Mark Baker Posted February 6, 2009 Share Posted February 6, 2009 If it's just a simple factorial: function factorial($factLoop=1) { $factorial = 1; while ($factLoop > 1) { $factorial *= $factLoop--; } return $factorial; } for ($i = 1; $i < 12; $i++) { echo factorial($i).'<br />'; } Link to comment https://forums.phpfreaks.com/topic/144070-solved-calculate-product-of-parent-rows-in-while-loop/#findComment-755933 Share on other sites More sharing options...
Zaynt Posted February 6, 2009 Author Share Posted February 6, 2009 ok...that was a little bit more complicated than I imagined. I got this assignment at school and we have been working with php for 2 days now, and have just learned how to use for,while and do while -loops. We haven't even started with functions yet, I have just played with some on my own. I will try to wrap my mind around this code of yours, because it seems to work just fine. Now I just have to figure out how it works. Thank you very much for the help so far(I may need some more later on)! Link to comment https://forums.phpfreaks.com/topic/144070-solved-calculate-product-of-parent-rows-in-while-loop/#findComment-755949 Share on other sites More sharing options...
Mark Baker Posted February 6, 2009 Share Posted February 6, 2009 If you don't want the function: for ($i = 1; $i < 12; $i++) { $factLoop = $i; $factorial = 1; while ($factLoop > 1) { $factorial *= $factLoop--; } echo $factorial.'<br />'; } [code=php:0] Link to comment https://forums.phpfreaks.com/topic/144070-solved-calculate-product-of-parent-rows-in-while-loop/#findComment-755958 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.