ueon Posted May 22, 2008 Share Posted May 22, 2008 Hi, i'm currently having some troubles with my for loop. For some reason, it doesn't stop after it reached its conditions <?php include 'connect.php'; // Extracting Body Data $sql = "SELECT div_file, table_file, flash_file, image_fol FROM stageone WHERE id = '1'"; $result = mysql_query ($sql) or die (mysql_error()); list ($div_num, $table_num, $flash_num, $image_fol) = mysql_fetch_row($result); $int_val3 = 0; $int_val4 = 0; $int_val5 = 0; for ($int_val3 = 0; $int_val3 < $div_num; $int_val3++) { //$div_num remains '0' $div_top = $_POST["div_top".$int_val3]; $div_left = $_POST["div_left".$int_val3]; $div_image = $_POST["div_name".$int_val3]; echo "<div style='position:absolute; left:".$div_left."px; top:".$div_top."px; overflow:auto;'>\n"; echo "<img src='".$image_fol."/".$div_image."'>\n"; echo "</div>\n"; //$div_num becomes '1' } //create table variables for ($int_val4 = 0; $int_val4 < $table_num; $int_val4++) { $table_width.$int_val4 = $_POST["table_width".$int_val4]; $table_height.$int_val4 = $_POST["table_height".$int_val4]; $table_top.$int_val4 = $_POST["table_top".$int_val4]; $table_left.$int_val4 = $_POST["table_left".$int_val4]; $table_border.$int_val4 = $_POST["table_border".$int_val4]; $table_con.$int_val4 = $_POST["content".$int_val4]; echo "<div style='position:absolute; left:".$table_left."px; top:".$table_top."px; overflow:auto;'>\n"; } $sql_delete = mysql_query("DELETE * FROM stageone"); if ($sql_delete) { echo "Delete Success"; } ?> it outputs <!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=utf-8" /> <title> dvien</title> <link rel='stylesheet' href='' type='text/css'> <link rel='stylesheet' href='' type='text/css'> <script language='JavaScript' src='' type='text/javascript'></script> <script language='JavaScript' src='' type='text/javascript'></script> </head> <body> <div style='position:absolute; left:px; top:px; overflow:auto;'> <img src='asdfasfsfsfsafasf/'> </div> <div style='position:absolute; left:px; top:px; overflow:auto;'> <img src='asdfasfsfsfsafasf/'> </div> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> <div style='position:absolute; left:px; top:px; overflow:auto;'> Quote Link to comment https://forums.phpfreaks.com/topic/106847-preventing-a-infinite-loop/ Share on other sites More sharing options...
btherl Posted May 23, 2008 Share Posted May 23, 2008 What do you intend by this code? $table_width.$int_val4 = $_POST["table_width".$int_val4]; That line actually resets $int_val4. Quote Link to comment https://forums.phpfreaks.com/topic/106847-preventing-a-infinite-loop/#findComment-547764 Share on other sites More sharing options...
ueon Posted May 23, 2008 Author Share Posted May 23, 2008 thanks, but please ignore that error for now. But is there anything you can help with the loop? Quote Link to comment https://forums.phpfreaks.com/topic/106847-preventing-a-infinite-loop/#findComment-548310 Share on other sites More sharing options...
Wolphie Posted May 23, 2008 Share Posted May 23, 2008 <?php for($i = 0; $i < 100; $i++) { print $i; exit; // This will loop only once each time the script is executed. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106847-preventing-a-infinite-loop/#findComment-548315 Share on other sites More sharing options...
haku Posted May 23, 2008 Share Posted May 23, 2008 For some reason, it doesn't stop after it reached its conditions Then it isn't reaching the exit conditions. Echo $div_num before the for loop to see what it contains. I'm thinking it's either a negative number, or null, or a non-integer. Quote Link to comment https://forums.phpfreaks.com/topic/106847-preventing-a-infinite-loop/#findComment-548317 Share on other sites More sharing options...
btherl Posted May 26, 2008 Share Posted May 26, 2008 ueon, the error I pointed out is causing the loop to run forever. The condition for loop termination is that $int_val4 is not less than $table_num, but you are resetting $int_val4 to null every time through the loop. If you fix that and you still have problems, post your code again here. Quote Link to comment https://forums.phpfreaks.com/topic/106847-preventing-a-infinite-loop/#findComment-550027 Share on other sites More sharing options...
sasa Posted May 26, 2008 Share Posted May 26, 2008 change to for ($int_val4 = 0; $int_val4 < $table_num; $int_val4++) { $table_width = $_POST["table_width".$int_val4]; $table_height = $_POST["table_height".$int_val4]; $table_top = $_POST["table_top".$int_val4]; $table_left = $_POST["table_left".$int_val4]; $table_border = $_POST["table_border".$int_val4]; $table_con = $_POST["content".$int_val4]; echo "<div style='position:absolute; left:".$table_left."px; top:".$table_top."px; overflow:auto;'>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/106847-preventing-a-infinite-loop/#findComment-550039 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.