PHP POST foreach loop

I never really was any good at understanding the concept of arrays, but see myself using them more and more often now.

I have a form that holds a branch number and employee. (depotId) and (adminresp).

When I want to post that information, what is the proper structure to  get the data that I need, which is ultimately, which employee was selected at each branch?

If the information below given isn't enough, just a basic example of how to take two form fields and post them together in a form to be able to get the desired results that match my specifications.  

echo '<form action = "" method = "POST">';

//Get Active Depots 
$getDepots = mysql_query("SELECT branch, name, adminid FROM dw_location WHERE hidden = '0'") or die("Cannot get Depots: " . mysql_error());

if(mysql_num_rows($getDepots) > 0)
	$i = 0;
	echo '<table width = "100%" cellspacing = "0" cellpadding = "0" border = "0">
					<td class="colheader">Depot #</td>
					<td class="colheader">Depot Name</td>
					<td class="colheader">Admin Responsibility</td>
	while($depotData = mysql_fetch_array($getDepots))
		$color_A = 'class="alt1"'; 
		$color_B = 'class="alt2"';

		$row_color = ($i % 2) ? $color_A : $color_B;
		echo '<tr>
					<td ' . $row_color . '>' . $depotData['branch'] . '</td>
					<td ' . $row_color . '>' . $depotData['name'] . '</td>
					<td ' . $row_color . '>
						<select name="adminResp[]" class="fieldclasssm">
							<option value ="">Select Admin</option>';
							//Get Admins
							$getAdmins = mysql_query("SELECT ua_user_group_membership.respid FROM ua_user_group_membership LEFT JOIN pr_resp ON ua_user_group_membership.respid = pr_resp.respid WHERE hidden = '0' AND groupid = '900'") or die("Cannot Get Office Admins: " . mysql_error());

							if(mysql_num_rows($getAdmins) > 0)
								while($adData = mysql_fetch_array($getAdmins))
									echo '<option value = "'.$adData['respid'].'"';  if($depotData['adminid'] == $adData['respid']) { echo " SELECTED"; } echo '>'.getCkname($adData['respid']).'</option>';
								}   echo '								
		echo "<input type='hidden' name='depotId[]' value = '".$depotData['branch']."'>";
echo "<tr><td colspan = '3'>&nbsp;</td></tr>";
echo "<tr><td colspan = '3' align = 'center'><input type='submit' name='updateAdmin' value = 'Update Admin' class= 'btn' /></td></tr>";
echo '</table>';

echo '</form>';

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I'm not really sure what your question is?
t3chguyAuthor Commented:
When that form is posted how can I use a foreach loop to get the selected admin for each depot on the page
Julian HansenCommented:
When the form is posted back the values for the drop downs will be in the array adminResp[]

To access this you do the following

$adminResp = isset($_POST['adminResp'])?$_POST['adminResp']:array();

Open in new window

To see what this array looks like you can do this

echo "<pre>";
echo "</pre>";

Open in new window

To find out how many responses there are
echo count($adminResp);

Open in new window

And to loop through the array
foreach($adminResp as $index => $admin) {
   echo "Admin[$index] = $admin <br/>";

Open in new window

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

t3chguyAuthor Commented:
How can I then incorporate the depot ID from the page as well?  I have it stored in a hidden field called depotId
Julian HansenCommented:
$depotid = $_POST['depotID'];

Open in new window

Please research santitising your POST data - you don't want to be using data straight out of the POST.
Ray PaseurCommented:
Let's try this.  Please install this little script and run it to see how the array of color choices appears in the HTML form and in the PHP action script, where the choices are represented in the $_POST array.  If this makes sense, then you understand the HTTP request that is caused by the HTML form and it should be easier to add the PHP that assembles your form from the data base.

<?php // RAY_temp_t3chguy.php
echo '<pre>';

if (!empty($_POST))
    if (empty($_POST['colors'][0]))
        echo PHP_EOL . "YOU DID NOT CHOOSE";
        foreach ($_POST['colors'] as $color)
            echo PHP_EOL . "YOU CHOOSE $color";
    echo PHP_EOL;


$form = <<<EOD
<form method="post">
<select name="colors[]" multiple>
<option value="">Choose Color(s)</option>
<option value="r">Red</option>
<option value="g">Green</option>
<option value="b">Blue</option>
<input type="submit" />

echo $form;

Open in new window

Also, please lose MySQL_Fetch_Array().  It's the worst possible fetch() function because it returns twice as much data as is needed.  Use the fetch_assoc() or the fetch_object() versions instead.

And since you will have to get off the MySQL extension anyway, bookmark this article so when you start refactoring you will have a ready reference.  It maps the MySQL functions to the newer and more well supported MySQLi and PDO extensions.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Just to clarify - are we talking about having multiple select's on a page with an array name


are we talking about a single select with the multiselect option (allowing for multiple selections from One select).

The original question seems to indicate the former ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.