derrick katungi Posted September 16, 2011 Share Posted September 16, 2011 <!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>Second Testing</title> <link href="styler.css" rel="stylesheet" type="text/css" /> </head> <body> <?php error_reporting(0); $num_rows = 16; $number1 = 4; $inn = array(7,10,13,16); $outt = array(14,16,21,23); $numm = array(3,5,7,9); $r_time_in = min($inn); for($i = 0; $i <= $num_rows; $i++) { $new_r_timer_in[$i] = $r_time_in; $new_r_timer_out[$i] = $new_r_timer_in[$i] + 1; $r_time_in += 1; } // for($y = 0; $y <= $number1; $i++) // { // for($i = 0; $i <= ($num_rows - 1); $i++) // { // if(($inn[$y] <= $new_r_timer_in[$i]) || ($outt[$y] <= $new_r_timer_out[$i])) // { // $numb[$i] = $numm[$i] + $numm[$i - 1]; // } // else // { // $numb[$i] = $numm[$i - 1]; // } // } // } for($i = 0; $i <= ($num_rows - 1); $i++) { echo '<table width="526" border="0"><tr> <td width="169"> <input type="text" name="$r_in[]" value="'.$new_r_timer_in[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td> <td width="169"> <input type="text" name="$r_out[]" value="'.$new_r_timer_out[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td> <td width="169"> <input type="text" name="$r_num[]" value="'.$numb[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td>'; } print("</tr>"); ?> </body> </html> the code above works very well in its current state. three columns are produced but only two have data, the third column is empty. when executed, the code is supposed to check whether a range(7 - is between the first element of array $inn and the first element of array $outt and then add the corresponding number in array $numm to the number in that(7 - range the commented out code is supposed to implement this idea but its not.........help. Quote Link to comment https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/ Share on other sites More sharing options...
derrick katungi Posted September 16, 2011 Author Share Posted September 16, 2011 in the first for loop, change $i++ to $y++.. it should now output something in the third column as well but its not the correct data.. Quote Link to comment https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/#findComment-1269900 Share on other sites More sharing options...
web_craftsman Posted September 16, 2011 Share Posted September 16, 2011 instead of $i <= ($num_rows - 1) use $i < $num_rows Quote Link to comment https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/#findComment-1269903 Share on other sites More sharing options...
derrick katungi Posted September 16, 2011 Author Share Posted September 16, 2011 thats true man but i have ever coded in C (borland) and i used to get funny results from $i < $num_rows so i switched to $i <= ($num_rows - 1)..but there's no biggie here, i still have a bigger problem dude.... this is my update so far... <!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>Second Testing</title> <link href="styler.css" rel="stylesheet" type="text/css" /> </head> <body> <?php error_reporting(0); $num_rows = 16; $number1 = 4; $inn = array(7,10,13,16); $outt = array(14,16,21,23); $numm = array(3,5,7,9); $r_time_in = min($inn); for($i = 0; $i <= $num_rows; $i++) { $new_r_timer_in[$i] = $r_time_in; $new_r_timer_out[$i] = $new_r_timer_in[$i] + 1; $r_time_in += 1; } for($y = 0; $y <= $number1; $y++) { for($i = 0; $i <= ($num_rows - 1); $i++) { if(($inn[$y] <= $new_r_timer_in[$i]) || ($outt[$y] <= $new_r_timer_out[$i])) { $numb[$i] = $numm[$y] + $numm[$y - 1]; } else { $numb[$i] = $numm[$y - 1]; } } } for($i = 0; $i <= ($num_rows - 1); $i++) { echo '<table width="526" border="0"><tr> <td width="169"> <input type="text" name="$r_in[]" value="'.$new_r_timer_in[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td> <td width="169"> <input type="text" name="$r_out[]" value="'.$new_r_timer_out[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td> <td width="169"> <input type="text" name="$r_num[]" value="'.$numb[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td>'; } print("</tr>"); ?> </body> </html> i have "un-commented" the section that had the nested loops and arrays and it outputs something that's definitely wrong Quote Link to comment https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/#findComment-1269912 Share on other sites More sharing options...
web_craftsman Posted September 16, 2011 Share Posted September 16, 2011 You are doing several things a bad way 1) do not use bb-code tags for your code listings 2) Loot at what html you will print by your: loop { print '<table><tr><td>...</td>' } print '</tr>'; It is not normat html. Do not you see? Quote Link to comment https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/#findComment-1269924 Share on other sites More sharing options...
derrick katungi Posted September 16, 2011 Author Share Posted September 16, 2011 the syntax is OK, at least on my side. i have realized that i have to come up with a completely new algorithm so those nested loops with arrays are based on the wrong algorithm. going to come up with another...enjoy the weekend Quote Link to comment https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/#findComment-1269935 Share on other sites More sharing options...
jcbones Posted September 16, 2011 Share Posted September 16, 2011 Need more info. Between the first element of $inn which starts at 7 and ends at 16, and the first element of $outt which starts at 14 and ends at 23. So that would mean you are taking out the last element of $inn. I could help you, but am massively confused. Quote Link to comment https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/#findComment-1269937 Share on other sites More sharing options...
derrick katungi Posted September 19, 2011 Author Share Posted September 19, 2011 use this idea, if your age range(7-8,8-9,9-10,etc) falls between $inn[1] and $outt[1] you get $numm[1] which is between 10 and 16, you get 5. Quote Link to comment https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/#findComment-1270562 Share on other sites More sharing options...
jcbones Posted September 19, 2011 Share Posted September 19, 2011 I think I got the idea: $new_r_timer_in = range(0,10); for($y=0; $y<4;$y++) { if(($inn[$y] <= $new_r_timer_in[$i]) && ($outt[$y] <= $new_r_timer_out[$i])) { echo $numm[$y]; } } Quote Link to comment https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/#findComment-1270765 Share on other sites More sharing options...
xyph Posted September 19, 2011 Share Posted September 19, 2011 Similar with foreach(). This assumes count($min) == count($max) == count($res) and that the array keys for each triplet line up correctly. <?php $age = 15; $min = array(7,10,13,16); $max = array(14,16,21,23); $res = array(3,5,7,9); foreach( $res as $key => $num ) { if( $age >= $min[$key] && $age <= $max[$key] ) echo 'Age '.$age.' is between '.$min[$key].' and '.$max[$key].' so result is '.$num.'<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/247270-nested-loops-with-arrays/#findComment-1270778 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.