Echo'd values in dropdowns

Hi experts, I have come here before asking for assistance in this scheduling application that I am creating and received great help. I have run into an issue recently and cannot figure out how to fix it. I am attempting to make an update screen for my application, where users can select a date and crew and echo the submitted schedule. This IS working. The issue comes in that I am echoing names into dropdowns as the first value, where the names are already existing. This is causing that name to appear once as echod, and again in the dropdown.

Is there a way I can select a name based off the echo'd value?

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
					<html>
					<head>
					<meta charset="utf-8">
					<meta name="viewport" content="width=device-width, initial-scale=1">
					<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
					<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
					<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
div.container {	background-color:#ffffb3;length: 250%;margin-right: 100px;width: 300%;}
div.submitbutton {position:relative; left:700px;}
									</style>
									</head>
									<body>
									<div class="container">
<article> 
<?php
//$_SESSION['DATE'];
//$_SESSION['CREW'];
		$ID= $_SESSION["DATE"];
		$CREW=$_SESSION["CREW"];
		//$ID=$_GET["ID"];
		//$CREW=$_GET["CREW"];
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$strID = null;
// DEFAULT RETURN
$result = array();
	$serverName = "XX";
	$userName = "";
	$userPassword = '';
	$dbName = "IS_DGMC";
// GET PARAMS SAFELY
$strID = isset($ID) ? $ID : false;
$strCREW = isset($_SESSION["CREW"]) ? $_SESSION["CREW"] : false;
// ONLY PROCEED IF WE GOT VALID PARAMETERS
if ($strID && $strCREW) {
 	$connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);
  $conn = sqlsrv_connect( $serverName, $connectionInfo);
  if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
  }
  // SET UP QUERY WITH PARAMETERS
// I THINK THIS MAY BE CLOSER TO WHAT YOU WANT
$stmt = "SELECT * FROM [CE_MANNING_PG2] WHERE [DATE] = ? AND [CREW]= ?
";
  
  // ADD PARAMETER VALUES
  $params = array($strID, $strCREW);
  
$query = sqlsrv_query( $conn, $stmt, $params);
if (!$query) trigger_error( sqlsrv_errors(), E_USER_WARNING); 
  

}
// RETURN DATA
?>
<form id="commonClassForThreeSelects" name="commonClassForThreeSelects" method="post" action= "iframe2_save.php">
<table width="300" border="1" id="commonClassForThreeSelects">
  <tr>
    <th width="91"> <div align="center">Area</div></th>
    <th width="20PX"> <div align="center">Person1</div></th>
    <th width="91"> <div align="center">Person2</div></th>
    <th width="91"> <div align="center">Person3</div></th>
    <th width="91"> <div align="center">Person4</div></th>
    <th width="91"> <div align="center">Person5</div></th>
    <th width="91"> <div align="center">Person6</div></th>
    <th width="91"> <div align="center">Person7</div></th>
    <th width="91"> <div align="center">Person8</div></th>



  </tr>

<?php
$k=0;
while($result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
	
?>

	<!--<td align="center"  height="50" width="120PX" nowrap> <?php if ($result["DATE"]!= NULL ){echo $result["DATE"] ->format("Y-m-d") ;}?></td>-->
<?php
$selectedname2[0] = $result['PERSON1'];
$selectedname2[1] = $result['PERSON2'];
$selectedname2[2] = $result['PERSON3'];
$selectedname2[3] =$result['PERSON4'];
$selectedname2[4] =$result['PERSON5'];
$selectedname2[5] =$result['PERSON6'];

  echo "<tr>";
echo "<input type='hidden' name='CREW_".$k."' value='$result[CREW]'/>"; 
echo "<input type='hidden' name='SHIFT_".$k."' value='$result[SHIFT]'/>"; 


echo"<td>";echo "<input type='text' name='AREA".$k."' style='width:120px' value='$result[AREA]'/>"; ECHO"</td>";

	for ($a=0; $a<8; $a++){

	echo"<td>";echo"<select  name='PACKER_".$k."_".$a."'/>";

			$location_query = "select distinct NAME from XX.dbo.CE_EMPLOYEES_CREW_A where POSITION = 'GENERAL' order by NAME";
			$locationQ = sqlsrv_query( $conn, $location_query );
			if( $locationQ === false) {
				die( print_r( sqlsrv_errors(), true) );
			}
			$p=0;
            $Lines[0] = '';
			
	
				echo "<option value='$selectedname2[$a]'>$selectedname2[$a]</option>";
			while( $row = sqlsrv_fetch_array( $locationQ, SQLSRV_FETCH_ASSOC) ) {
					if($row['NAME'] != $selectedname[$a]);
					{
						echo "<option value='".$row['NAME']."'>".$row['NAME']."</option>";			

                  $Lines[$p] = $row['NAME'];
                $p++;
		}}}
			$numLines = sizeof($Lines);
			sqlsrv_free_stmt($locationQ);
	echo"</select>";
  ECHO"</tr>";
  
?>
<?php
$k++;
}
  ECHO"</TABLE>";
?>

<div class="submitbutton">
<input type="submit" style="height:30px; width:200px" value="submit" id="#q5">
</div>
</form>
<?php
sqlsrv_close($conn);
?>
</div>
</article>
<script>
var $selects = $('select');
    $('select').change(function () {
        $('option:hidden', $selects).each(function () {
            var self = this,
                toShow = true;
            $selects.not($(this).parent()).each(function () {
                if (self.value == this.value) toShow = false;
            })
            if (toShow) $(this).show();
        });
        if (this.value != 0) //to keep default option available
          $selects.not(this).children('option[value=' + this.value + ']').hide();
    });
</script>

</body>
</html>

Open in new window

Thankyou so much for any help provided.
LVL 1
peter OjedaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Julian HansenCommented:
Trying to understand the issue here. I tried out a version of your code (adapted so that it will work here) - not sure what I am looking for though. The code itself has some issues that need to be addressed - for instance

The SESSION variables don't make sense - assigning a date to an ID field is confusing and you have this on lines 25-25
$ID= $_SESSION["DATE"];
$CREW=$_SESSION["CREW"];

Open in new window

And then this on lines 39 and 40
$strID = isset($ID) ? $ID : false;
$strCREW = isset($_SESSION["CREW"]) ? $_SESSION["CREW"] : false;

Open in new window

- What is the $CREW variable for
- where is SESSION set
- what is this page supposed to do

Here is my version of it http://www.marcorpsa.com/ee/t2310.php

How must I change my data to see the problem and what am I looking for?
0
peter OjedaAuthor Commented:
Hi Julien thankyou for your input I am still relatively new to a lot of things so I am sure a lot of my code is either unnecessary or not in the best practice. The session variables on line 25 are coming from the previous page. I am passing these variables into  $ID and $CREW, and am then passing those into strings which is passed into the $params (line 54) for the select query. I am attemping to make a scheduling program, where users can go back and modify a schedule that is set on a previous day. After the select statement I am attempting to echo the names from my sql database into the dropdowns as selected. So if "Bob" is echo'd into the dropdown, Bob will appear selected.
0
Julian HansenCommented:
Ok but if you look at my sample you will see that is what is happening - I need to know what needs to be done extra / differently from what is being done in my adapted sample.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

peter OjedaAuthor Commented:
Hi sorry for the delay yes the function to not be able to select the same name twice is exactly what I was looking for. But if you see in my code I am echoing a name from my sqldb. I have attached some images below. As you can see, the name "Bishop, La" is being echo'd from my db schedule table. The rest of the value of the dropdown are being echo'd from my db table Employee_Names. If possible, I want to just have the employee name selected based off the value echo'd from the schedule db.
ecample1.PNG
example2.PNG
0
Julian HansenCommented:
Looking at your second image I am still not seeing the problem - it looks like the selected person "Bishop, La" is in the drop down once - what am I missing? In a drop down the selected item is shown in the list and the drop down which is what I am seeing in the image.

Can you give me some of your data so I can replicate the issue. I need just enough examples of data in the two tables to reproduce the problem.
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
peter OjedaAuthor Commented:
Hi Julien I'm sorry but I'm going to have to close the question. I found many more issues in my program that I need to fix first. I will come back to this question sometime in the future.
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.