pereira2k5 Posted March 18, 2008 Share Posted March 18, 2008 Hello I am a new user and a noob when it comes to PHP coding, people this is the thread to get ur laugh of the day. My question is how do I created two do-while php loops in the same webpage. The basic code is converting Celsius to Fahrenheit and vice-versa. When I get the results I get the same loop going rather than two different ones. So when The second part comes up with Converting Celcius to Fahrenheit, the celcius starts at 103.9 instead of 0 and loops for a while. can anyone help me? Thanks alot <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Fahrenheit</title> <style type="text/css"> <!-- #Layer1 { position:absolute; width:356px; height:53px; z-index:1; left: 0px; top: 0px; } #Layer2 { position:absolute; width:356px; height:54; z-index:2; left: 362px; top: 0px; } .style3 {font-size: 24px} --> </style> </head> <body> <div id="Layer1"> <table width="359" border="1" id="F2C"> <tr> <td width="406"><h1 class="style3"> Converting Fahrenheit to Celcius </h1></td> </tr> <tr> <td><?php do { $fahrenheit++; $celsius = (($fahrenheit - 32)*.55); echo " $celsius degree(s) celcius"; echo " <p> $fahrenheit degree(s) farhrenheit ="; } while ($fahrenheit<=100); ?></td> </tr> </table> </div> <div id="Layer2"> <table width="355" border="1"> <tr> <td><h1 class="style3"> Converting Celcius to Fahrenheit </h1></td> </tr> <tr> <td><?php do { $celsius++; $fahrenheit = ($celsius*1.+32; echo " $fahrenheit degree(s) farhrenheit"; echo "<p>$celsius degree(s) celcius ="; } while ($celsius<=100); ?> </td> </tr> </table> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/96661-multiple-do-whiles-in-php/ Share on other sites More sharing options...
Cep Posted March 18, 2008 Share Posted March 18, 2008 It is because in your program flow you have not reset the value of $celsius back to 0. Just before your second do statement add, $celsius = 0; Link to comment https://forums.phpfreaks.com/topic/96661-multiple-do-whiles-in-php/#findComment-494722 Share on other sites More sharing options...
kenrbnsn Posted March 18, 2008 Share Posted March 18, 2008 I would use "for" loops instead of "do" loops in these cases: <?php for ($f=0;$f<101;$f++) { $c = (($f - 32)*.55); echo $c . ' degrees Celsius is ' . $f . ' degrees Fahrenheit<br>'; }?> and <?php for ($c=0;$c<101;$c++) { $f = ($c*1.+32; echo $f . ' degrees Fahrenheit is ' . $c . ' degrees Celsius<br>'; }?> Ken Link to comment https://forums.phpfreaks.com/topic/96661-multiple-do-whiles-in-php/#findComment-494739 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.