thefortrees Posted June 20, 2007 Share Posted June 20, 2007 Hi all - In this loop, the last element isn't being echoed with the html option the last time through. I put an echo statement before the if-else to debug, and it echoed every value. So, for some reason, the loop is breaking before it gets to the echo HTML the last time through. Any ideas? $temp = array(); $val = array_search($row['lob'], $businessArray); $temp[] = $businessArray[$val]; $count = 0; //for ($i = 0; $i < sizeof($businessArray); $i++){ foreach ($businessArray as $value){ //$value = $businessArray[$i]; if ($value == $row['lob']){ continue; } else{ $temp[] = $value; echo "<option value='" . $temp[$count] . "'>" . $temp[$count]; $count++; } } Quote Link to comment https://forums.phpfreaks.com/topic/56433-solved-isnt-echoing-the-last-element/ Share on other sites More sharing options...
Corona4456 Posted June 20, 2007 Share Posted June 20, 2007 Well you have a $value == $row['lob'] in there which skips printing that one element... could it coincidentally be the last item in the array? Quote Link to comment https://forums.phpfreaks.com/topic/56433-solved-isnt-echoing-the-last-element/#findComment-278723 Share on other sites More sharing options...
thefortrees Posted June 20, 2007 Author Share Posted June 20, 2007 I appreciate your response Corona. The element $row['lob'] is not the last element in the array. In the select menu, $row['lob] is displayed first (this loop is nested in another loop) multiple times. If I remove the conditional continue, then $row['lob'] is repeated twice in the loop.... Quote Link to comment https://forums.phpfreaks.com/topic/56433-solved-isnt-echoing-the-last-element/#findComment-278732 Share on other sites More sharing options...
Corona4456 Posted June 20, 2007 Share Posted June 20, 2007 Ah... well then can I see both loops then? Just out of curiosity. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/56433-solved-isnt-echoing-the-last-element/#findComment-278840 Share on other sites More sharing options...
thefortrees Posted June 20, 2007 Author Share Posted June 20, 2007 Sure thing! It's just a while loop with mysql_fetch_array. //Display each question. while($row = mysql_fetch_array($result)){ echo "<form>" . "<tr><td><input type='text' name='question' value='" . $row['question'] . "'></td>" . "<input type='hidden' name='questions_id' value='" . $row['questions_id'] . "'>" . //Create drop down menu for LoB for each question. "<td><select name='lob'>"; $temp = array(); $val = array_search($row['lob'], $businessArray); $temp[] = $businessArray[$val]; $count = 0; //for ($i = 0; $i < sizeof($businessArray); $i++){ foreach ($businessArray as $value){ //$value = $businessArray[$i]; if ($value == $row['lob']){ continue; } else{ $temp[] = $value; echo "<option value='" . $temp[$count] . "'>" . $temp[$count]; $count++; } } echo "</select></td>" . "<td><h6><input type='submit' name='updateQuestion' value='Update'></h6></td>" . "<td><h6><input type='submit' name='deleteQuestion' value='Delete'></a></h6></td></tr>" . "</form>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/56433-solved-isnt-echoing-the-last-element/#findComment-278850 Share on other sites More sharing options...
Corona4456 Posted June 20, 2007 Share Posted June 20, 2007 Ok... I see the problem now. The problem is you increment $count AFTER you output your option code. Instead of doing that... just do this: echo "<option value='" . $value . "'>" . $value; Since you already have your value just use it. Quote Link to comment https://forums.phpfreaks.com/topic/56433-solved-isnt-echoing-the-last-element/#findComment-278914 Share on other sites More sharing options...
corbin Posted June 21, 2007 Share Posted June 21, 2007 This is kinda random, but isn't <option value='" . $temp[$count] . "'>" . $temp[$count]; unterminating? Won't affect the PHP at all, but HTML will die a little inside. Quote Link to comment https://forums.phpfreaks.com/topic/56433-solved-isnt-echoing-the-last-element/#findComment-278947 Share on other sites More sharing options...
teng84 Posted June 21, 2007 Share Posted June 21, 2007 foreach ($businessArray as $value){ $count++; //$value = $businessArray[$i]; if ($value == $row['lob']){ continue; } else{ $temp[] = $value; echo "<option value='" . $temp[$count] . "'>" . $temp[$count]; } } counter i guess should be at the top of the condition or echo you will not get the last resulr just because the counter is not being updated on the last round of the counter ASTIG!!! :-* Quote Link to comment https://forums.phpfreaks.com/topic/56433-solved-isnt-echoing-the-last-element/#findComment-278949 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.