TheStalker Posted July 21, 2008 Share Posted July 21, 2008 hi, I have just created this loop to calculate totals for customers but im guessing that it will use up lots of memory and seems to take a while for the page to load. The problem i have is there is always going to be customers added and deleted and i dont want to limit the number of customers that the loop can calculate for. here is the code <html> <body> <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'randv'; mysql_select_db($dbname); ?> <?php for ($i=2; $i>=2; $i++) { $result = mysql_query("SELECT WeeklyCharge FROM accounts WHERE CustomerID='$i'"); while($row = mysql_fetch_array($result)) $oldTotal = $row ['WeeklyCharge']; echo "<br>"; //DAILY PAPER 1 $result1 = mysql_query("SELECT dailyNewspaper1 FROM accounts WHERE CustomerID='$i'"); while($row1 = mysql_fetch_array($result1)) $daily1 = $row1 ['dailyNewspaper1']; echo $daily1; $result2 = mysql_query("SELECT price FROM dailypapers WHERE paper='$daily1'"); while($row2 = mysql_fetch_array($result2)) $dailyPrice1 = $row2 ['price']; echo $dailyPrice1; echo "<br>"; //DAILY PAPER 2 $result3 = mysql_query("SELECT dailyNewspaper2 FROM accounts WHERE CustomerID='$i'"); while($row3 = mysql_fetch_array($result3)) $daily2 = $row3 ['dailyNewspaper2']; echo $daily2; $result4 = mysql_query("SELECT price FROM dailypapers WHERE paper='$daily2'"); while($row4 = mysql_fetch_array($result4)) $dailyPrice2 = $row4 ['price']; echo $dailyPrice2; echo "<br>"; //DAILY PAPER 3 $result5 = mysql_query("SELECT dailyNewspaper3 FROM accounts WHERE CustomerID='$i'"); while($row5 = mysql_fetch_array($result5)) $daily3 = $row5 ['dailyNewspaper3']; echo $daily3; $result6 = mysql_query("SELECT price FROM dailypapers WHERE paper='$daily3'"); while($row6 = mysql_fetch_array($result6)) $dailyPrice3 = $row6 ['price']; echo $dailyPrice3; echo "<br>"; echo "<br>"; //SAT PAPER 1 $result7 = mysql_query("SELECT satNewspaper1 FROM accounts WHERE CustomerID='$i'"); while($row7 = mysql_fetch_array($result7)) $sat1 = $row7 ['satNewspaper1']; echo $sat1; $result8 = mysql_query("SELECT price FROM satpapers WHERE paper='$sat1'"); while($row8 = mysql_fetch_array($result8)) $satPrice1 = $row8 ['price']; echo $satPrice1; echo "<br>"; //SAT PAPER2 $result9 = mysql_query("SELECT satNewspaper2 FROM accounts WHERE CustomerID='$i'"); while($row9 = mysql_fetch_array($result9)) $sat2 = $row9 ['satNewspaper2']; echo $sat2; $result10 = mysql_query("SELECT price FROM satpapers WHERE paper='$sat2'"); while($row10 = mysql_fetch_array($result10)) $satPrice2 = $row10 ['price']; echo $satPrice2; echo "<br>"; //SAT PAPER3 $result11 = mysql_query("SELECT satNewspaper3 FROM accounts WHERE CustomerID='$i'"); while($row11 = mysql_fetch_array($result11)) $sat3 = $row11 ['satNewspaper3']; echo $sat3; $result12 = mysql_query("SELECT price FROM satpapers WHERE paper='$sat3'"); while($row12 = mysql_fetch_array($result12)) $satPrice3 = $row12 ['price']; echo $satPrice3; echo "<br>"; echo "<br>"; //SUN PAPER 1 $result13 = mysql_query("SELECT sunNewspaper1 FROM accounts WHERE CustomerID='$i'"); while($row13 = mysql_fetch_array($result13)) $sun1 = $row13 ['sunNewspaper1']; echo $sun1; $result14 = mysql_query("SELECT price FROM sunpapers WHERE paper='$sun1'"); while($row14 = mysql_fetch_array($result14)) $sunPrice1 = $row14 ['price']; echo $sunPrice1; echo "<br>"; //SUN PAPER 2 $result15 = mysql_query("SELECT sunNewspaper2 FROM accounts WHERE CustomerID='$i'"); while($row15 = mysql_fetch_array($result15)) $sun2 = $row15 ['sunNewspaper2']; echo $sun2; $result16 = mysql_query("SELECT price FROM satpapers WHERE paper='$sun2'"); while($row16 = mysql_fetch_array($result16)) $sunPrice2 = $row16 ['price']; echo $sunPrice2; echo "<br>"; //SUN PAPER 2 $result17 = mysql_query("SELECT sunNewspaper3 FROM accounts WHERE CustomerID='$i'"); while($row17 = mysql_fetch_array($result17)) $sun3 = $row17 ['sunNewspaper3']; echo $sun3; $result18 = mysql_query("SELECT price FROM satpapers WHERE paper='$sun3'"); while($row18 = mysql_fetch_array($result18)) $sunPrice3 = $row18 ['price']; echo $sunPrice3; echo "<br>"; echo "<br>"; echo "<br>"; echo "Daily Total="; $dailyTotal=$dailyPrice1+$dailyPrice2+$dailyPrice3; printf ("%01.2f", $dailyTotal); echo "<br>"; echo "Sat Total="; $satTotal=$satPrice1+$satPrice2+$satPrice3; printf ("%01.2f", $satTotal); echo "<br>"; echo "Sun Total="; $sunTotal=$sunPrice1+$sunPrice2+$sunPrice3; printf ("%01.2f", $sunTotal); echo "<br>"; echo "<br>"; echo "Weekly Total="; $weeklyTotal=$dailyTotal+$satTotal+$sunTotal; printf ("%01.2f", $weeklyTotal); echo "<br>"; echo "Weekly Total + Delivery Charge="; $deTotal=$weeklyTotal+0.70; printf ("%01.2f", $deTotal); echo "<br>"; echo "<br>"; echo "Old Total="; printf ("%01.2f", $oldTotal); echo "<br>"; echo "<br>"; echo "Weekly Total + Delivery Charge + Old Total="; $newTotal=$deTotal+$oldTotal; printf ("%01.2f", $newTotal); mysql_query("UPDATE accounts SET WeeklyCharge = '".$newTotal."' WHERE CustomerID='$i'"); } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/115832-betterfaster-way-to-do-this-for-loop/ Share on other sites More sharing options...
trq Posted July 21, 2008 Share Posted July 21, 2008 Your code is near impossible to read because of a lack of indentation, however, it appears you are attempting to execute multiple queries within multipel loops. This is never efficient, take alook into using sql joins. There is an article on our main site that should get you started. Link to comment https://forums.phpfreaks.com/topic/115832-betterfaster-way-to-do-this-for-loop/#findComment-595464 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.