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">
				<tr>
					<td class="colheader">Depot #</td>
					<td class="colheader">Depot Name</td>
					<td class="colheader">Admin Responsibility</td>
				</tr>';
					
	
	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 '								
						</select>
					</td>
				</tr>';
				
		echo "<input type='hidden' name='depotId[]' value = '".$depotData['branch']."'>";
		
		$i++;
		}
	}
	
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

LVL 1
t3chguyAsked:
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.

GaryCommented:
I'm not really sure what your question is?
0
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
0
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>";
print_r($adminResp);
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

0
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
0
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.
0
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.
See http://www.laprbass.com/RAY_temp_t3chguy.php

<?php // RAY_temp_t3chguy.php
error_reporting(E_ALL);
echo '<pre>';

// SHOW HOW TO ACCESS THE POSTED SELECTIONS
if (!empty($_POST))
{
    if (empty($_POST['colors'][0]))
    {
        echo PHP_EOL . "YOU DID NOT CHOOSE";
    }
    else
    {
        foreach ($_POST['colors'] as $color)
        {
            echo PHP_EOL . "YOU CHOOSE $color";
        }
    }
    echo PHP_EOL;

    // SHOW THE POST DATA
    var_dump($_POST);
}

// CONSTRUCT A FORM WITH ARRAYS FOR SELECTIONS
$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>
</select>
<input type="submit" />
</form>
EOD;

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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0

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

OR

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 ...
0
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
PHP

From novice to tech pro — start learning today.