Jump to content

Problem with foreach


Go to solution Solved by Ch0cu3r,

Recommended Posts

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>";
}
Link to comment
https://forums.phpfreaks.com/topic/290318-problem-with-foreach/
Share on other sites

  • Solution

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 by Ch0cu3r

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.