Rory_php Posted August 6, 2014 Share Posted August 6, 2014 I am new to this forum and I intend to become involved in its day-to-day exertions by helping and contributing in any way I can. Now, despite the fact that I have programmed with PHP on and off for about 4 years and consider myself to be 'competent' in working with it, I have come across a problem that I can not understand or, therefore, solve. To give some context, I am creating a webpage or a pair of applications, for personal use, for the Telegraph Dream Team (after leaving the Sun for a reason that should be apparent to those who have used it). Firstly, I used Java to extract information from the website and put it into a PHPMyAdmin database. Then, I used the information, in this case, to allow people to submit teams using drop-down boxes, and here lies the problem. I have used a foreach to display all of the 'keepers' and 'defenders' from the database as an option in the drop-down box, which works fine. However, the page only prints out every second drop-down box. For example, it will show the keeper(p1) selection box and then, in this case if a 442 formation is chosen, it will only print the third(p3) and fifth(p5) drop down boxes, namely it only prints every second drop-down menu. I am wondering what is causing this to happen. Is it a known problem with the overuse of the foreach and, therefore, should be avoided or is it something I have done wrong that causes the page to only print every second drop-down menu? Sorry about the misaligned format of the code on the forum. Any information on what is causing this and any help on how to solve it would be greatly appreciated, thank you. elseif (isset($_SESSION['formation']) and isset($_SESSION['teamname'])) { print " <form method='post' action='createteam.php'>"; print "Keeper: <select name='p1'> <option value='none'>Pick a Keeper</option>"; foreach ($k_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; if ($_SESSION['formation'] == '442' or $_SESSION['formation'] == '433' or $_SESSION['formation'] == '451') { print "Defender: <select name='p2'> <option value='none'>Pick a Defender2</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print "Defender: <select name='p3'> <option value='none'>Pick a Defender3</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print "Defender: <select name='p4'> <option value='none'>Pick a Defender4</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print "Defender: <select name='p5'> <option value='none'>Pick a Defender5</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print $_SESSION['formation']; } elseif ($_SESSION['formation'] == '352' or $_SESSION['formation'] == '343') { print "Defender: <select name='p2'> <option value='none'>Pick a Defender2</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print "Defender: <select name='p3'> <option value='none'>Pick a Defender3</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print "Defender: <select name='p4'> <option value='none'>Pick a Defender4</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print $_SESSION['formation']; } elseif ($_SESSION['formation'] == '532') { print "Defender: <select name='p2'> <option value='none'>Pick a Defender2</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print "Defender: <select name='p3'> <option value='none'>Pick a Defender3</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print "Defender: <select name='p4'> <option value='none'>Pick a Defender4</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print "Defender: <select name='p5'> <option value='none'>Pick a Defender5</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print "Defender: <select name='p6'> <option value='none'>Pick a Defender6</option>"; foreach ($d_array as $option) : print "<option value=$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print $_SESSION['formation']; } else { print "You have not picked a formation!"; } print "</br> <input type='submit' value='Submit your team'> </form>"; } Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted August 6, 2014 Solution Share Posted August 6, 2014 (edited) You have left off the opening quote for the value attribute for the <option> tag (highlighted below) in the foreach and also closing </select> tag afterwards foreach ($k_array as $option) : print "<option value='$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print '</select>'; Also your code could be re-factored to a for loop elseif (isset($_SESSION['formation']) and isset($_SESSION['teamname'])) { print " <form method='post' action='createteam.php'>"; print "Keeper: <select name='p[]'> <option value='none'>Pick a Keeper</option>"; foreach ($k_array as $option) : print "<option value='$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print '</select>'; $possible_formations = array(442, 433, 451, 352, 343, 532); if(in_array($_SESSION['formation'], $possible_formations)) { list($defenders, $midfielders, $strikers) = str_split($_SESSION['formation']); echo '<hr>'; for($i = 0; $i < $defenders; $i++) { $x = $i + 2; print "Defender: <select name='p[]'> <option value='none'>Pick a Defender$x</option>"; foreach ($d_array as $option) : print "<option value='$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print '</select>'; } // midfielders... // strikers... print $_SESSION['formation']; } else { print "You have not picked a formation!"; } print "</br> <input type='submit' value='Submit your team'> </form>"; } Edited August 6, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Rory_php Posted August 6, 2014 Author Share Posted August 6, 2014 Thank you for your reply, the problem has now been fixed, it seems that the lack of the closing select tag was the problem. I feel silly making such a simply mistake and it is something I should have seen. Also, thanks for the additional advice - much appreciated. Quote Link to comment 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.